Extendscript Photoshop: Is there a way to save out jpeg with specific KB file sizes for web?

社会主义新天地 提交于 2019-12-11 19:26:19

问题


I'm trying to utilize the following. what this does is saves out Jpeg accordingly to less then a a certain size set. I was curious if anyone knew a way to specifically direct a filename to get a filesize that is equal to the following or as close as possible.

Filename_160x600.png = Filename_160x600.jpg 39kb

Filename_300x600.png = Filename_300x600.jpg 59kb

Filename_1500x513.png = Filename_1500x513.jpg 150kb

         saveJPG(
         {
         path: activeDocument.path,
         maxSize: 50 //size in kbs
         })

        function saveJPG(_data)
    {
    if (_data.path == undefined) return false;
    _data.name = _data.name == undefined ? activeDocument.name : _data.name;
    _data.quality == undefined && _data.quality = 75;

    if (!new Folder(_data.path).exists)


{
        alert("Output path doesn't exist!"); //you can add a function to create a path if needed
        return false
    }

    var options = new ExportOptionsSaveForWeb(),
        jpgFile = new File(_data.path + '/' + getName(_data.name) + '.jpg');

    options.format = SaveDocumentType.JPEG;
    options.quality = _data.quality;
    activeDocument.exportDocument(jpgFile, ExportType.SAVEFORWEB, options);
    if (_data.maxSize != undefined)
    {
        var ms = _data.maxSize * 1000;
        if (jpgFile.length > ms)
        {
            if (!jpgFile.remove())
            {
                alert('Save file is locked, please make sure it\'s not opened anywhere');
                return
            };
            saveJPG(
            {
                path: _data.path,
                name: _data.name,
                maxSize: _data.maxSize,
                quality: _data.quality - 2
            });
        }
    };

来源:https://stackoverflow.com/questions/52341077/extendscript-photoshop-is-there-a-way-to-save-out-jpeg-with-specific-kb-file-si

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!