16 Replies Latest reply on Jan 31, 2017 7:29 AM by wyf51999

    How to limit the JPG file size within 300 Kb?

    Wate.R

      For example

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

        • 1. Re: How to limit the JPG file size within 300 Kb?
          Chuck Uebele Adobe Community Professional & MVP

          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.

          • 2. Re: How to limit the JPG file size within 300 Kb?
            Wate.R Level 1

            I got it,Thank you very much

            • 3. Re: How to limit the JPG file size within 300 Kb?
              dominique filiol Level 1

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

              • 4. Re: How to limit the JPG file size within 300 Kb?
                dominique filiol Level 1

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

                 

                }

                • 5. Re: How to limit the JPG file size within 300 Kb?
                  Wate.R Level 1

                  oh my god,cool,Thank you very much

                  • 6. Re: How to limit the JPG file size within 300 Kb?
                    dominique filiol Level 1

                     

                     

                    It's faster with saveAs and JPEGSaveOptions : quality = 12 and not 100 !

                    • 7. Re: How to limit the JPG file size within 300 Kb?
                      Wate.R Level 1

                      Sorry,I have a problem.

                      I tested and found that when the original file format is PSD, then output file format is also PSD,not jpg,

                      How to modify the code?

                      Waiting for your reply

                      thank you

                      • 8. Re: How to limit the JPG file size within 300 Kb?
                        dominique filiol Level 1

                        Bonjour

                         

                        line 15

                        put

                         

                         

                        Name = docRef.name;

                        Name = Nom.split (".")

                        Name = Name [0] + ".jpg";

                        • 9. Re: How to limit the JPG file size within 300 Kb?
                          wyf51999 Level 1

                          Thanks for your script, it works great.

                          Is it possible to use "percent" (of width/hight), instead of "quality"? I searched and could not find it.

                          • 10. Re: How to limit the JPG file size within 300 Kb?
                            dominique filiol Level 1

                            Bonjour !

                             

                             

                            I do not understand your question

                            What does "percent" mean?
                            (Of width / hight) - it's a constant!


                            What do you want to do?

                            • 11. Re: How to limit the JPG file size within 300 Kb?
                              wyf51999 Level 1

                              My bad. I mean, inside the Saveforweb window, lower right, there's a "percent" input box, which will change both width and hight.

                              Sometimes original file is considerabaly big, when I try to export a small jpg file,  I don't want to get a very large dimension with a very low quality, I prefer small dimension with 100% quality. At least, I often upload photos to forums or image hosting sites, they often have dimension limits, instead of quality limits.

                              • 12. Re: How to limit the JPG file size within 300 Kb?
                                dominique filiol Level 1

                                Bonjour

                                 

                                So for a given weight you want a variation of size and not of quality!

                                Maximum quality?
                                Fixed weight? 300kb ?
                                And as long as the fixed weight is not reached the size decreases - without limit?

                                • 13. Re: How to limit the JPG file size within 300 Kb?
                                  wyf51999 Level 1

                                  Yeah, just the way as we decrease "percent" value in SaveForWeb window.

                                  • 14. Re: How to limit the JPG file size within 300 Kb?
                                    dominique filiol Level 1
                                    //SauveWebJPG_Poids_pourcentage.jsx  SaveWeb%.jsx

                                    //MODIFIER les lignes 13 37 et 84 au besoin

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

                                     

                                    //Il faut que le dernier format d enreregistrement soit sur Images
                                    //et non sur HTML et images

                                     

                                    //var poidsMax =  300000; // pour 300 ko maximum

                                       

                                       

                                       

                                    var pourcent = 0.9 // reglages a tester

                                       

                                    var docRef = activeDocument;

                                     

                                    var outputFolder = docRef.path;

                                     

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

                                     

                                    blog.create();

                                     

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

                                     

                                    Nom = docRef.name;

                                     

                                    Nom = Nom.split (".")

                                     

                                    Nom = Nom [0] + ".jpg"; 

                                     

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

                                     

                                    var NewfileRef = new File( NamesaveRef )

                                     

                                    // Test du POIDS

                                     

                                    var poidsMax =  300000; // pour 300 ko maximum - a modifier

                                     

                                    var qualit = 100;

                                     

                                    EnregWeb(NewfileRef, qualit);

                                     

                                     

                                    while (NewfileRef.length > poidsMax )

                                     

                                    {
                                            Pourcentage();
                                            NewfileRef = new File( NewfileRef );  
                                            NewfileRef.remove();
                                            EnregWeb(NewfileRef, qualit);
                                    }

                                     

                                     

                                    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.JPEG;   // Or it'll default to GIF.
                                      docRef.exportDocument(File( filename), ExportType.SAVEFORWEB, options);

                                    }

                                    function Pourcentage()

                                    {

                                     

                                      var largeur = docRef.width.value;

                                      var hauteur = docRef.height.value;

                                     

                                      docRef.resizeImage(largeur*pourcent, hauteur*pourcent, 72, ResampleMethod.BICUBIC);

                                     

                                      pourcent = pourcent -0.00000000001; //Pas sur - a modifier pour accelerer

                                    }

                                    • 15. Re: How to limit the JPG file size within 300 Kb?
                                      wyf51999 Level 1

                                      Thank you so much.

                                      I tested it several times, the result is beyond my understanding --- with "0.00000000001" in the last line, I suppose the result file size should be most proximate to my limit filesize and calculation counts should be huge, but it's not, it just jumps to a much small filesize with only a few times of caculation.

                                      Instead, with "0.02", result filesize is much bigger/closer to my limit filesize.

                                      • 16. Re: How to limit the JPG file size within 300 Kb?
                                        wyf51999 Level 1

                                        Just another thought:

                                        I've used another script for a long time, which is also from this forum, but I can not remember from whom. I just made a little change.

                                        Now I blend this code with yours, it works even better.

                                        In one word, it's to save as jpeg file, in the same folder of the source image, with appending a random suffix to the file name, and now it also limits filesize.

                                        code:

                                        ===========

                                        #target photoshop

                                        main();

                                        function main(){

                                        if(!documents.length) return;

                                        var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');

                                        try{

                                        var savePath = activeDocument.path;

                                        }catch(e){

                                            alert("You must save this document first!");

                                            }

                                        var fileList= savePath.getFiles(Name +"*.jpg").sort().reverse();

                                        var Suffix = 0;

                                        if(fileList.length){

                                            Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/));

                                        }

                                        Suffix= zeroPad(Suffix + 1, 2);

                                        var saveFile = File(savePath + "/" + Name + "_" + Suffix + ".jpg");

                                         

                                         

                                         

                                         

                                        var poidsMax =  2000000;   // file size you want to achieve, for example 2000k

                                        var qualit = 12

                                        var x = 1;;

                                        SaveJPEG(saveFile, qualit);

                                        //EnregWeb(NewfileRef, qualit);

                                          

                                        while (saveFile.length > poidsMax )

                                        {

                                                qualit = qualit - x;

                                                saveFile = new File( saveFile ); 

                                                saveFile.remove();

                                                SaveJPEG(saveFile, 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         

                                                    }

                                        }

                                         

                                         

                                        }

                                         

                                         

                                        function SaveJPEG(saveFile, jpegQuality){

                                        jpgSaveOptions = new JPEGSaveOptions();

                                        jpgSaveOptions.embedColorProfile = true;

                                        jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;

                                        jpgSaveOptions.matte = MatteType.NONE;

                                        jpgSaveOptions.quality = jpegQuality;

                                        activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);

                                        };

                                        function zeroPad(n, s) {

                                           n = n.toString();

                                           while (n.length < s)  n = '0' + n;

                                           return n;

                                        };

                                        ===========

                                        Sorry, I know little about coding, and I know nothing about french......

                                        It's not perfect ---- if in photoshop we create a new file and paste from clipboard, there'll be a warning, since there's no original file name, I can accept this. But if we're editing a raw file, it gives warning, too, I wish this problem could be fixed. Could you please give me an advise?