• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Save for web with an original name under file size

New Here ,
Feb 24, 2023 Feb 24, 2023

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

TOPICS
Actions and scripting , Windows

Views

989

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 24, 2023 Feb 24, 2023

@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
...

Votes

Translate

Translate
Adobe
New Here ,
Feb 24, 2023 Feb 24, 2023

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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 24, 2023 Feb 24, 2023

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);
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 25, 2023 Feb 25, 2023

Copy link to clipboard

Copied

LATEST

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines