Copy link to clipboard
Copied
Dear everyone.
I want to limit file size after save for web. I have this code:
//SaveforWebSP.jsx
//Save a fixed width image with a size constraint in its original folder
var docRef = activeDocument;
var outputFolder = docRef.path;
NamesaveRef = new File( outputFolder + "/reference.jpg" );
var NewfileRef = new File( NamesaveRef )
// quality/size constraints
var w = 300; // new file width
var MaxSz = 61440; // max. 60Kb
var Qlt = 100; // initial quality 100
var x = 1; // decreasing step
// resize the image to the right width
docRef.resizeImage(UnitValue(w,"px"),null,100,ResampleMethod.BICUBICSHARPER);
// Perform the first SaveForWeb Operation
ExpWeb(NewfileRef, Qlt);
// Keep trying to save the file with max. Qlt, but under MaxSz
while (NewfileRef.length > MaxSz)
{
Qlt = Qlt - x;
NewfileRef = new File( NewfileRef );
NewfileRef.remove();
ExpWeb(NewfileRef, Qlt); // Perform a new SaveForWeb Operation, with slightly lower Qlt
if (Qlt <= 50) {
alert("The file can't be saved with the desired size AND quality.");
break // break the loop whenever the quality is as low as 50 (this shouldn't need to happen)
}
}
var FileSz = NewfileRef.length/1024;
FileSz = Math.round(FileSz);
// close the original file without saving
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
// SaveForWeb Export, with the desired constraints and parameters
function ExpWeb(FileNm, Qlt)
{
var options = new ExportOptionsSaveForWeb();
options.quality = Qlt; // Start with highest quality (biggest file).
options.format = SaveDocumentType.JPEG; // Save Format for the file
docRef.exportDocument(File(FileNm), ExportType.SAVEFORWEB, options);
}
The Problem is when I automate with multiple file size, all new files are overwritten with the name "reference.jpg".
Please help me modify this code. Thank you
@An28289043i2fn – try the following minor edit:
//SaveforWebSP.jsx
//Save a fixed width image with a size constraint in its original folder
var docRef = activeDocument;
var docRefName = docRef.name.replace(/\.[^\.]+$/, '');
var outputFolder = docRef.path;
NamesaveRef = new File( outputFolder + "/" + docRefName + ".jpg" );
var NewfileRef = new File( NamesaveRef )
// quality/size constraints
var w = 300; // new file width
var MaxSz = 61440; // max. 60Kb
var Qlt = 100; // initial quality 100
var
...
Copy link to clipboard
Copied
I think it is better when new files keep their original names, then I can save them in different folders.
Copy link to clipboard
Copied
@An28289043i2fn – try the following minor edit:
//SaveforWebSP.jsx
//Save a fixed width image with a size constraint in its original folder
var docRef = activeDocument;
var docRefName = docRef.name.replace(/\.[^\.]+$/, '');
var outputFolder = docRef.path;
NamesaveRef = new File( outputFolder + "/" + docRefName + ".jpg" );
var NewfileRef = new File( NamesaveRef )
// quality/size constraints
var w = 300; // new file width
var MaxSz = 61440; // max. 60Kb
var Qlt = 100; // initial quality 100
var x = 1; // decreasing step
// resize the image to the right width
docRef.resizeImage(UnitValue(w,"px"),null,100,ResampleMethod.BICUBICSHARPER);
// Perform the first SaveForWeb Operation
ExpWeb(NewfileRef, Qlt);
// Keep trying to save the file with max. Qlt, but under MaxSz
while (NewfileRef.length > MaxSz)
{
Qlt = Qlt - x;
NewfileRef = new File( NewfileRef );
NewfileRef.remove();
ExpWeb(NewfileRef, Qlt); // Perform a new SaveForWeb Operation, with slightly lower Qlt
if (Qlt <= 50) {
alert("The file can't be saved with the desired size AND quality.");
break // break the loop whenever the quality is as low as 50 (this shouldn't need to happen)
}
}
var FileSz = NewfileRef.length/1024;
FileSz = Math.round(FileSz);
// close the original file without saving
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
// SaveForWeb Export, with the desired constraints and parameters
function ExpWeb(FileNm, Qlt)
{
var options = new ExportOptionsSaveForWeb();
options.quality = Qlt; // Start with highest quality (biggest file).
options.format = SaveDocumentType.JPEG; // Save Format for the file
docRef.exportDocument(File(FileNm), ExportType.SAVEFORWEB, options);
}
Copy link to clipboard
Copied