Skip to main content
Wate_R
Participant
October 26, 2015
Answered

How to limit the JPG file size within 300 Kb?

  • October 26, 2015
  • 2 replies
  • 4824 views

For example

Limite the size of the JPG file  that use SaveForWeb function maked.

This topic has been closed for replies.
Correct answer dominique filiol

Sorry

I made an error in the function EnregWeb

Replace by

function EnregWeb(filename, qualit)

{

      var options = new ExportOptionsSaveForWeb();

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

      options.format = SaveDocumentType.JPEG;   // Or it'll default to GIF.

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

}

2 replies

Participating Frequently
October 27, 2015

Bonjour

//SauveWebJPG_Poids.jsx

//Sauve l image a un poids inferieur a 300 ko

// R :

//Il faut que le dernier format d enreregistrement soit sur Images

//et non sur HTML et images

var docRef = activeDocument;

var outputFolder = docRef.path;

var blog = new Folder(outputFolder  +"/blog");

blog.create();

var outputFolder= new Folder(outputFolder +"/blog");

Nom = docRef.name;

NamesaveRef = new File( outputFolder + "/" + Nom );

var NewfileRef = new File( NamesaveRef )

// Test du POIDS

var poidsMax =  300000; // pour 300 ko maximum

var qualit = 100;

var x = 1;//pour accelerer le script mettre x = 5

EnregWeb(NewfileRef, qualit);

    

while (NewfileRef.length > poidsMax )

{

        qualit = qualit - x;

        NewfileRef = new File( NewfileRef );   

        NewfileRef.remove();

        EnregWeb(NewfileRef, qualit);

            if (qualit<=0)

            {

        alert("La taille de l'image est telle qu'elle ne peut être sauvegardée avec un poids de 300 ko.");

            break           

            }

}

var poidsFichier = NewfileRef.length /1000;

poidsFichier = Math.round(poidsFichier);

// fermer l original sans enregistrer

activeDocument.close(SaveOptions.DONOTSAVECHANGES);

function EnregWeb(filename, qualit)

{

      var options = new ExportOptionsSaveForWeb();

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

      options.format = SaveDocumentType.SGIRGB//JPEG;   // Or it'll default to GIF.

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

}

You can try this

But this risk to be a little long................

dominique filiolCorrect answer
Participating Frequently
October 27, 2015

Sorry

I made an error in the function EnregWeb

Replace by

function EnregWeb(filename, qualit)

{

      var options = new ExportOptionsSaveForWeb();

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

      options.format = SaveDocumentType.JPEG;   // Or it'll default to GIF.

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

}

Wate_R
Wate_RAuthor
Participant
October 27, 2015

oh my god,cool,Thank you very much

Chuck Uebele
Community Expert
Community Expert
October 26, 2015

I don't think that can be done via scripting. You normally have to set the quality, and PS then calculates what the file size will be. It doesn't work even without scripting to set the file size first.

Wate_R
Wate_RAuthor
Participant
October 27, 2015

I got it,Thank you very much