Copy link to clipboard
Copied
So I frequently need to batch export a number of various artboards to JPGs for web use. The maximum size that the files can be is 100kb, and while some of the artboards are small (and really should be exported at maximum quality so that they don't get too pixelated), some of the artboards are larger and need to be imported at about 40% quality.
Up until now I have been manually adjusting the quality of each of the files in order to remain within 100kb, but I'm wondering if there is any sort of script out there that can help me with this. Any ideas?
Copy link to clipboard
Copied
Hi @Kailene, you say "The maximum size that the files can be is 100kb" but this is not my experience at all—I export jpeg all the time that are much larger than this.
So perhaps there is some problem causing this strange limitation for you? Could you make a test document that has several artboards that each contain the same placed image, but each artboard is a different size, eg. 100px square, 500px square, 1000px square, 2000px square. Then try exporting each artboard as jpg and see what you get. If you still have strange things happening, then you could share your example document here and we can test it too.
Or maybe someone more knowledgable than me will have an answer!
- Mark
Copy link to clipboard
Copied
Hello! I think my question may have been worded in a way that was difficult to understand. The client needs all my files to be below 100kbs. So I need all the files to be exported at 100kb or below. If I set all the exports to 40% quality, that's fine... but the really small files end up getting super distorted. I have to export about 60 files/artboards like this every two weeks, and I end up exporting each artboard individually so that they all fall under 100kb while losing as little quality as possible.
I think a script for this might be impossible, but it's annoyingly time consuming.
Copy link to clipboard
Copied
Ah ha! Got it now. Thanks for clarifying!
Copy link to clipboard
Copied
Okay @Kailene I've written a little script that exports each artboard as JPEG, and keeps adjusting the quality until the size is as close to the target size, but not over. It uses a binary search, so minimises the amount of "test" exporting, but it can still be quite slow.
Let me know if it works for you. I've only quickly tested on my system here, so let me know if you find a bug.
- Mark
/**
* @file Export JPG With File Size.js
*
* Exports every artboard of a document, and attempts
* to keep them below a target size, eg. 100 KB.
*
* Limitations:
* - quite slow due to having to actually do real exports
* during a binary search for the right quality.
*
* @author m1b
* @version 2024-09-13
*/
(function () {
var settings = {
targetFileSizeKB: 100,
scale: 100,
}
if (0 === app.documents.length)
return alert('Please open a document and try again.');
var doc = app.activeDocument,
folder = new Folder(doc.path + '/JPG'),
exportOptions,
messages = ['Export JPG To Size'],
targetSize = settings.targetFileSizeKB * 1000;
if (!doc.path.exists)
// we need the path to export the artboards
return alert('Please save this document and try again.');
if (!folder.exists)
folder.create();
for (var i = 0, f; i < doc.artboards.length; i++) {
var name = doc.artboards[i].name,
path = folder + '/' + name + '.jpg';
var minQuality = 1,
maxQuality = 100,
quality = maxQuality,
bestQuality = minQuality;
while (minQuality <= maxQuality) {
// export with current quality
exportArtboard(File(path), i, quality);
var fileSize = File(path).length;
if (fileSize <= targetSize) {
// narrow down the search
minQuality = quality + 1;
// this is our best so far
bestQuality = quality;
}
else {
// narrow down the search
maxQuality = quality - 1;
}
// test midpoint of the current range
quality = Math.floor((minQuality + maxQuality) / 2);
}
// do the final export at best quality
exportArtboard(File(path), i, bestQuality);
var fileSize = Math.round(File(path).length);
messages.push('Artboard ' + i + ': ' + name + ' ('
+ ((0 === quality && fileSize > targetSize) ? 'Failed. ' : 'Quality: ' + quality)
+ ' Size: ' + (Math.round(fileSize / 1000, 2)) + 'K).');
}
// show the results
alert(messages.join('\n'));
/**
* Export artboard `index` as JPG to file `f`.
* @param {File} f - the file to export to.
* @param {Number} artboardIndex - the artboard index.
* @param {Number} quality - the JPG quality settings (1..100).
*/
function exportArtboard(f, artboardIndex, quality) {
if (undefined == exportOptions) {
exportOptions = new ExportOptionsJPEG();
exportOptions.horizontalScale = settings.scale;
exportOptions.verticalScale = settings.scale;
exportOptions.antiAliasing = true;
exportOptions.artBoardClipping = true;
exportOptions.saveAsHTML = false;
}
exportOptions.qualitySetting = quality;
doc.artboards.setActiveArtboardIndex(artboardIndex);
exportOptions.saveMultipleArtboards = true;
exportOptions.artboardRange = artboardIndex + 1;
doc.exportFile(f, ExportType.JPEG, exportOptions);
};
})();