Skip to main content
Participant
March 19, 2019
Answered

PNGs skalieren nicht richtig beim Import

  • March 19, 2019
  • 2 replies
  • 440 views

Hey,
ich würde gerne mehrere Logos (PNGs mit transparenten BG) die alle eine Größe zwischen 100x100 und 300x300 px aufweisen in ein Dokument mit der Größe von 100x100px importieren. Dabei möchte ich, dass sich die Bilder beim importieren automatisch an die Größe des Dokuments anpassen.

Die Option "Bild beim Platzieren skalieren" habe ich aktiviert. Aber wenn ich die Logos in das Dokument importiere sind sie alles andere aber nicht auf die Größe des Dokumentes skaliert.

Weiß jemand eine Lösung?

This topic has been closed for replies.
Correct answer JJMack

Here the code to size the logo to the document width or height for best results.  You can customize the size via the var LogoSize default is 100%

/* ==========================================================

// 2019 John J. McAssey (JJMack)

// ======================================================= */

// This script is supplied as is. It is provided as freeware.

// The author accepts no liability for any problems arising from its use.

/*

<javascriptresource>

<about>$$$/JavaScripts/PlaceVectorLogo/About=JJMack's PlaceVectorLogo ^r^rCopyright 2019 Mouseprints.net^r^rPhotoshop Script^rCustomize var LogoSize</about>

<category>JJMack's Script</category>

</javascriptresource>

*/

#target photoshop; 

app.bringToFront(); 

var logoFile = File.openDialog("Select Vector Logo" , "Select:*.svg;*.ai");

if (logoFile != undefined ) {

   var LogoSize = 100;                                   // Canvas Fill Percenttage

   //placeLogo(logoFile, LogoSize);                      // Place Logo onto the document

   app.activeDocument.suspendHistory('placeLogo','placeLogo(logoFile,LogoSize)' );

}

else alert('no Vector Logo file selected');

///////////////////////////////////////////////////////////////////////////////////////////////////////////

function placeLogo(Image,Size){ 

   if(!documents.length) return;                         // if no document return

   var fileObj = new File(Image);                        // the passed file

   if(!fileObj.exists){                                  // If file does not exits tell user

      alert(fileObj.name  + " does not exist!");         // Alert user 

      return;                                            // return

      }

   try{ 

      var doc = app.activeDocument;                      // set Doc object to active document

      app.displayDialogs = DialogModes.NO;               // Dialog off

      var strtRulerUnits = app.preferences.rulerUnits;   // Save Users ruler units

      var strtTypeUnits = app.preferences.typeUnits;     // Save Users Type units

      app.preferences.rulerUnits = Units.PIXELS;         // work with pixels

      app.preferences.typeUnits = TypeUnits.PIXELS;      // work with pixels

      var layers = app.activeDocument.layers;            // get layers

      app.activeDocument.activeLayer = layers[0];        // Target Top Layer

      placeFile(fileObj);                                // Place in file the Logo Vector file

      activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER); // Insure Place did not scale layer 

      var docWidth =  app.activeDocument.width.value;    // get Canvas width

      var docHeight =  app.activeDocument.height.value;  // get canvas Hieght

      var docAspectRatio = docWidth/docHeight;           // Canvas Aspect Ratio

      var LB = app.activeDocument.activeLayer.bounds;    // Get Active layers bounds

      var LWidth = (LB[2].value) - (LB[0].value);        // Area width

      var LHeight = (LB[3].value) - (LB[1].value);       // Area height

      var sobjAspectRatio =  LWidth/LHeight              // Logo Aspect Ratio

      if (sobjAspectRatio<docAspectRatio) {              // layer's Aspect Ratio less the Canvas area Aspect Ratio

         var percentageChange=((docHeight/LHeight)*Size);// resize to canvas area height

         app.activeDocument.activeLayer.resize(percentageChange,percentageChange,AnchorPosition.MIDDLECENTER);

         }

      else {

         var percentageChange = ((docWidth/LWidth)*Size);// Resize to canvas area width

         app.activeDocument.activeLayer.resize(percentageChange,percentageChange,AnchorPosition.MIDDLECENTER);

         }

      app.activeDocument.selection.selectAll();          // select all

      align('AdCH');                                     // align to horizontal centers

      align('AdCV');                                     // align to vertical centers

      app.activeDocument.selection.deselect();

      }

   catch(e) { alert(e + ': on line ' + e.line); }        // inform user of error 

   finally{ 

      app.preferences.rulerUnits = strtRulerUnits;       // Restore user ruler units 

      app.preferences.typeUnits = strtTypeUnits;         // Restore user type units   

      }; 

};

///////////////////////////////////////////////////////////////////////////////////////////////////////////

function placeFile(placeFile) { 

    var desc21 = new ActionDescriptor(); 

    desc21.putPath( charIDToTypeID('null'), new File(placeFile) ); 

    desc21.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') ); 

    var desc22 = new ActionDescriptor(); 

    desc22.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 ); 

    desc22.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 ); 

    desc21.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc22 ); 

    executeAction( charIDToTypeID('Plc '), desc21, DialogModes.NO ); 

}; 

function align(method) {

   var desc = new ActionDescriptor();

   var ref = new ActionReference();

   ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

   desc.putReference( charIDToTypeID( "null" ), ref );

   desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );

   try{executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );}

   catch(e){}

}

2 replies

JJMack
Community Expert
Community Expert
March 19, 2019

You would need to script that. You could point the script to the logo file. The script could then place the logo in and adjust the smart object layer. transform for the documents canvas.  Place will trim Transparent border off . The smart object layer bounds will be for the logo matter. The smart object layer can be aligned to the document center onces transformed.. You could look at my place watermark to get some idea to do that. You would need to replace the hard coder file with a file dialog which would not be hard to do. I'm just a hacker and would like to do something like that. PlaceWatermark.jsx script

IMO you should not scale small PNG logo up in size. Scaling down in size though will work well.

JJMack
JJMack
Community Expert
Community Expert
March 19, 2019

Here is a script that should work for  vector logo files.  You could easily add png file support.  However, I would not recommend doing so.

The script does not actually look at Aspects Ratios. The logo will be resized for the document height so wide aspect ratio logo may be resized too wide. It would be east to change the script to resize to less than document height.

You could  add code to resize for the width or height for which ever would work for best fit. If you want that code consider adding it yourself. Your intro into Photoshop scripting.

/* ==========================================================

// 2019 John J. McAssey (JJMack)

// ======================================================= */

// This script is supplied as is. It is provided as freeware.

// The author accepts no liability for any problems arising from its use.

#target photoshop; 

app.bringToFront(); 

var logoFile = File.openDialog("Select Vector Logo" , "Select:*.svg;*.ai");

if (logoFile != undefined ) {

   var LogoSize = 100; // percent of document height to resize Logo to

   //placeLogo(logoFile, LogoSize); // Place Logo onto the document

   app.activeDocument.suspendHistory('placeLogo','placeLogo(logoFile,LogoSize)' );

   }

else alert('no Vector Logo file selected');

///////////////////////////////////////////////////////////////////////////////////////////////////////////

function placeLogo(Image,Size){ 

if(!documents.length) return;  // if no document return

   try{ 

      var doc = app.activeDocument; // set Doc object to active document

      app.displayDialogs = DialogModes.NO; // Dialog off

      var strtRulerUnits = app.preferences.rulerUnits; // Save Users ruler units

      var strtTypeUnits = app.preferences.typeUnits; // Save Users Type units

      app.preferences.rulerUnits = Units.PIXELS; // work with pixels

      app.preferences.typeUnits = TypeUnits.PIXELS; // work with pixels

      var fileObj = new File(Image);                 // the passed file

      if(!fileObj.exists){  // If file does not exits tell user

         alert(fileObj.name  + " does not exist!"); 

         return; 

         } 

      var layers = app.activeDocument.layers; // get layers

      app.activeDocument.activeLayer = layers[0]; // Target Top Layer

      placeFile(fileObj); // Place in file the Watermark png file

      activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER); // Insure Place did not scale layer 

      var SB = activeDocument.activeLayer.bounds; // get layers bounds

      var layerHeight = SB[3] - SB[1]; // get layers height 

      var resizePercent = (100/layerHeight)*(Size/100*doc.height.value); // Percent to resize by

      activeDocument.activeLayer.resize(resizePercent ,resizePercent,AnchorPosition.MIDDLECENTER);  // Resize width and height by percentage

      app.activeDocument.selection.selectAll(); // select all

      align('AdCH'); // align to horizontal centers

      align('AdCV'); // align to vertical centers

      app.activeDocument.selection.deselect();

      }

   catch(e) { alert(e + ': on line ' + e.line); } // inform user of error 

   finally{ 

      app.preferences.rulerUnits = strtRulerUnits; // Restore user ruler units 

      app.preferences.typeUnits = strtTypeUnits; // Restore user type units   

      }; 

};

///////////////////////////////////////////////////////////////////////////////////////////////////////////

function placeFile(placeFile) { 

    var desc21 = new ActionDescriptor(); 

    desc21.putPath( charIDToTypeID('null'), new File(placeFile) ); 

    desc21.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') ); 

    var desc22 = new ActionDescriptor(); 

    desc22.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 ); 

    desc22.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 ); 

    desc21.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc22 ); 

    executeAction( charIDToTypeID('Plc '), desc21, DialogModes.NO ); 

}; 

function align(method) {

   var desc = new ActionDescriptor();

   var ref = new ActionReference();

   ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

   desc.putReference( charIDToTypeID( "null" ), ref );

   desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );

   try{executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );}

   catch(e){}

}

JJMack
JJMack
Community Expert
JJMackCommunity ExpertCorrect answer
Community Expert
March 20, 2019

Here the code to size the logo to the document width or height for best results.  You can customize the size via the var LogoSize default is 100%

/* ==========================================================

// 2019 John J. McAssey (JJMack)

// ======================================================= */

// This script is supplied as is. It is provided as freeware.

// The author accepts no liability for any problems arising from its use.

/*

<javascriptresource>

<about>$$$/JavaScripts/PlaceVectorLogo/About=JJMack's PlaceVectorLogo ^r^rCopyright 2019 Mouseprints.net^r^rPhotoshop Script^rCustomize var LogoSize</about>

<category>JJMack's Script</category>

</javascriptresource>

*/

#target photoshop; 

app.bringToFront(); 

var logoFile = File.openDialog("Select Vector Logo" , "Select:*.svg;*.ai");

if (logoFile != undefined ) {

   var LogoSize = 100;                                   // Canvas Fill Percenttage

   //placeLogo(logoFile, LogoSize);                      // Place Logo onto the document

   app.activeDocument.suspendHistory('placeLogo','placeLogo(logoFile,LogoSize)' );

}

else alert('no Vector Logo file selected');

///////////////////////////////////////////////////////////////////////////////////////////////////////////

function placeLogo(Image,Size){ 

   if(!documents.length) return;                         // if no document return

   var fileObj = new File(Image);                        // the passed file

   if(!fileObj.exists){                                  // If file does not exits tell user

      alert(fileObj.name  + " does not exist!");         // Alert user 

      return;                                            // return

      }

   try{ 

      var doc = app.activeDocument;                      // set Doc object to active document

      app.displayDialogs = DialogModes.NO;               // Dialog off

      var strtRulerUnits = app.preferences.rulerUnits;   // Save Users ruler units

      var strtTypeUnits = app.preferences.typeUnits;     // Save Users Type units

      app.preferences.rulerUnits = Units.PIXELS;         // work with pixels

      app.preferences.typeUnits = TypeUnits.PIXELS;      // work with pixels

      var layers = app.activeDocument.layers;            // get layers

      app.activeDocument.activeLayer = layers[0];        // Target Top Layer

      placeFile(fileObj);                                // Place in file the Logo Vector file

      activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER); // Insure Place did not scale layer 

      var docWidth =  app.activeDocument.width.value;    // get Canvas width

      var docHeight =  app.activeDocument.height.value;  // get canvas Hieght

      var docAspectRatio = docWidth/docHeight;           // Canvas Aspect Ratio

      var LB = app.activeDocument.activeLayer.bounds;    // Get Active layers bounds

      var LWidth = (LB[2].value) - (LB[0].value);        // Area width

      var LHeight = (LB[3].value) - (LB[1].value);       // Area height

      var sobjAspectRatio =  LWidth/LHeight              // Logo Aspect Ratio

      if (sobjAspectRatio<docAspectRatio) {              // layer's Aspect Ratio less the Canvas area Aspect Ratio

         var percentageChange=((docHeight/LHeight)*Size);// resize to canvas area height

         app.activeDocument.activeLayer.resize(percentageChange,percentageChange,AnchorPosition.MIDDLECENTER);

         }

      else {

         var percentageChange = ((docWidth/LWidth)*Size);// Resize to canvas area width

         app.activeDocument.activeLayer.resize(percentageChange,percentageChange,AnchorPosition.MIDDLECENTER);

         }

      app.activeDocument.selection.selectAll();          // select all

      align('AdCH');                                     // align to horizontal centers

      align('AdCV');                                     // align to vertical centers

      app.activeDocument.selection.deselect();

      }

   catch(e) { alert(e + ': on line ' + e.line); }        // inform user of error 

   finally{ 

      app.preferences.rulerUnits = strtRulerUnits;       // Restore user ruler units 

      app.preferences.typeUnits = strtTypeUnits;         // Restore user type units   

      }; 

};

///////////////////////////////////////////////////////////////////////////////////////////////////////////

function placeFile(placeFile) { 

    var desc21 = new ActionDescriptor(); 

    desc21.putPath( charIDToTypeID('null'), new File(placeFile) ); 

    desc21.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') ); 

    var desc22 = new ActionDescriptor(); 

    desc22.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 ); 

    desc22.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 ); 

    desc21.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc22 ); 

    executeAction( charIDToTypeID('Plc '), desc21, DialogModes.NO ); 

}; 

function align(method) {

   var desc = new ActionDescriptor();

   var ref = new ActionReference();

   ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

   desc.putReference( charIDToTypeID( "null" ), ref );

   desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );

   try{executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );}

   catch(e){}

}

JJMack
davescm
Community Expert
Community Expert
March 19, 2019

Hi

You should be able to do this with Preferences >General>Resize Image during Place.  Then import using "Place Embedded".  Note however that, whilst that will resize the imported document to the size of the new document, it will not change the aspect ratio, so rectangular images will remain rectangular.

Dave

JJMack
Community Expert
Community Expert
March 19, 2019

That will only scale logo smart Object layer that are  larger than canvas size to fit downsized onto the document canvas. Most likely aligned to the documents top left point.   Place will resize logo png files based on the png files print resolution compared to the Document Print Resolition.  If you first make your png file have a 1 DPI print resolution Place may resize you logo size larger than canvas size when creating the smart object layer which your Preference will then sacle down in size to fit one canvas.  However Logo scaled up in size from png raster images may not fair well.   If you had Ai file like SVG files place would treat them as  vector objects and import them using vector graphics when sizing them up in size. The Smart object layer actually has no vectors its file object .svg or .ai file has those.

JJMack