Skip to main content
micheled87465710
Participant
March 29, 2016
Question

I need to set the maximum compression that does not exceed 370KB.

  • March 29, 2016
  • 1 reply
  • 573 views

Hello, all.

I'm a novice when it comes to scripts in Photoshop, but I'm really coming to the conclusion that this is the only way to automate the actions I need to do, whithout having to do it one-by-one.

Is it possible to write a Script to automatically choose the option (and settings) to "Optimize to file size" in the "Save for Web" dialog?

Is it also possible to save the file in the document's original folder?

I only have a restriction of size (370Kb) for thosefiles, and I would like to save all of them, in each document original folder, with the best possible quality within that size constraint.


I Have this script, but have any problem, do not save in the same folder:



// MAC Finder ou WINDOWS Explorer, on autorise le double clic  et on fait passer Photoshop au 1er plan  

#target photoshop 

app.bringToFront(); 

 

// We shall have to work in pixels 

var regleUnite = app.preferences.rulerUnits; 

app.preferences.rulerUnits = Units.PIXELS; 

 

// Variables declaration 

var sizeMax = 360; // Kb 

sizeMax *=1024; // Conversion to bytes  

var fileName = new File(); 

var qlt = 100; // Quality 

var sizeImg=1000000; // We choose a great value 

         

var docRef = activeDocument; 

 

var docName  = docRef.name; 

var docName  = docName.substring( 0, docName.indexOf('.') );  

var imgWidth = docRef.width; 

var imgHeight = docRef.height; 

 

// The image is resized 

docRef.resizeImage(imgWidth, imgHeight); 

 

var exportOptionsSaveForWeb = new ExportOptionsSaveForWeb(); 

exportOptionsSaveForWeb.format = SaveDocumentType.JPEG; 

exportOptionsSaveForWeb.includeProfile = true; 

 

while (sizeImg > sizeMax) 

    fileName = new File(docRef.path + docName+qlt+".jpg"); 

    exportOptionsSaveForWeb.quality = qlt; 

    docRef.exportDocument (fileName, ExportType.SAVEFORWEB, exportOptionsSaveForWeb);

    qlt-=1; 

    sizeImg = fileName.length; 

    if (sizeImg>sizeMax) 

    { 

        fileName.remove();         

    } 

This topic has been closed for replies.

1 reply

MR74270
Inspiring
March 29, 2016

// Enables double clicking from the Macintosh Finder or Windows Explorer 

#target photoshop

app.bringToFront();

// We shall work in pixels

var rulerPreference = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

// The user chooses the largest dimension and the maximum size for his Web image

var maxDimension = prompt("Which is (in pixels) the largest dimension\n (height or width) that you need ?", "");

var maxSize = prompt("Which is (in Kb) the maximum\n size of the optimized file ?", "");

maxSize *=1024; // Kb to Bytes

var inputFolder = Folder.selectDialog("Select the folder where your image is : ");

var selectedFile = inputFolder.openDlg("Select the image to optimize : " , "Select:*.jpg");

var docRef = app.open (selectedFile);

var imgWidth = docRef.width;

var imgHeight = docRef.height;

// Portrait or Landscape ?

if (imgWidth<imgHeight)

{

    imgWidth = Math.round(maxDimension * imgWidth / imgHeight);

}

else

{

    imgWidth = maxDimension;

}

var outputFolder = Folder(docRef.path+'/');

var nameSaveRef = new File( outputFolder + '/_' + docRef.name); 

var newFileRef = new File( nameSaveRef );

// maximum initial % quality

var qlty = 100; 

 

// resize the image to the right width 

docRef.resizeImage(UnitValue(imgWidth,"px"),null,100,ResampleMethod.BICUBICSHARPER); 

 

// Perform the first SaveForWeb Operation

alert("Click on \"OK\" and wait about 15 seconds. You will receive another message when your image will be ready.") 

ExportWeb(newFileRef, qlty); 

 

// Keep trying to save the file with maximum quality under maxSize     

while (newFileRef.length > maxSize) 

    qlty -= 1; 

    newFileRef = new File( newFileRef );    

    newFileRef.remove(); 

    ExportWeb(newFileRef, qlty);  // Perform a new SaveForWeb Operation, with slightly lower qlty 

}

 

// close the original file without saving 

activeDocument.close(SaveOptions.DONOTSAVECHANGES);

open(newFileRef);

alert("Your image is ready. The best quality rate is " + qlty + " %");

// Restore user's preference

app.preferences.rulerUnits=rulerPreference; 

 

// SaveForWeb Export, with the desired constraints and parameters 

function ExportWeb(file, qlty) 

      var options = new ExportOptionsSaveForWeb(); 

      options.quality = qlty;   // Start with highest quality (biggest file). 

      options.format = SaveDocumentType.JPEG;   // Save Format for the file 

      docRef.exportDocument(File(file), ExportType.SAVEFORWEB, options); 

}

When I wrote this code, somebody asked me to limit not only the size of the optimized picture, but the largest dimention also. This is the new code :

!

micheled87465710
Participant
March 30, 2016

ok. Thank you.

but I need a script to be included in action.

without the possibility to choose the folder.

Thank you so much

MR74270
Inspiring
March 30, 2016

I do do not understand why you want to launch a script from an action. It is possible that I do not understand because of my poor English.

What I can do for you : optimize for the web all the photos included in a top folder (and its sub-folders) with a simple recursive script. Without modification of width and height of the original images, optimize them for the Web (370 Kb maximum), in the folders where original pictures are. If this can help, no problem. If you need something different, you must be more precise (for me, of course) .