1 Reply Latest reply on Jan 30, 2016 11:23 PM by matias.kiviniemi

    Lower quality until certain file size using 'save for web'

    karlosk42549650

      Hey everyone,

       

      I will try to describe the task I am currently trying to automate in Photoshop CS6.

      I would like to open a .tiff image (/raw/test.tiff), resize the longest side to 500px and save in one folder with original name (/500/test.jpg), and then resize the longest side to 350px and save to different folder (/350/test.jpg)


      The tricky part is that the images in the 350 folder need to be less than 50kb in size. When I do this manually, I use the 'save for web' dialog and lower the quality until the filesize is lower than 50kb.

      Is there anyway to automate this? Is there a script that can compare quality and filesize?

      I am currently using an action that uses 'automate>fit image' for the first parts, but i have to manually do the final 'save for web' and change the directory.

       

      Any help would be greatly appreciated!

      Thanks

        • 1. Re: Lower quality until certain file size using 'save for web'
          matias.kiviniemi Level 3

          Should be possible in some way like below (filling in the blanks/fixing the bugs is left as exercise to the reader)

           

          var jpg_options = new JPEGSaveOptions()

          var file_original = new File('path to original')

          var file_target = new File('path to target')

          var w, h = // do some math to calculate what size you need to resize to

          var doc = app.open(file_original)

          doc.resizeImage(w, h)


          for (var quality=12; i>=0; i--) {

            jpg_options.quality = quality

            doc.saveAs(file_target, jpg_options)

            if (file_target.length <= 50*1024) {

               break

            }

          }

          doc.close()

           

          Two potential issues:

          • What if setting quality=0 does not satisfy limit
          • I'm not sure if File-objects are "realtime", the .length property updates the size after you save. If this happens, you need to create file_target after saving