Quick question for the folks in the room -- I've g...
# cfml-general
q
Quick question for the folks in the room -- I've got a requirement from one of my vendors where I need to compress images for to no more than 900kb (this is going out a serial device in the back-end). I know I can just take a stab at width x height and just assume it's fine, but do you know of any image manipulation libraries that will let you specify the max size like you can in a photoshop or other image app?
p
Because of the vagaries of image compression (like the massive difference between compressing a flat logo versus a complex photograph), you'll probably just have to save with sensible defaults, then check to see if it fits the requirements. If it doesn't try saving again with higher compression ratios or smaller sizes until it passes. It might be possible to guesstimate the next ratio from how far over the threshold it is with a bit of testing with different examples.
j
ImageMagick can do this I believe... and I'd imagine there is a Java wrapper for it...
q
Patrick -- The issue is this will be for user provided content, so it won't be predictable.
Jim -- There is a java wrapper for ImageMagik, wasn't sure if that was what people use these days for something like this.
m
I know of a team that uses ImageMagik, but they have to use CFExecute to run it properly for their needs.
p
I've never seen a command to do that in ImageMagick (but would love to be proven wrong...) It will scale images and you can set the quality, but I don't think you can just give it a target file size. You'd have to either set the target width/height and let it figure out the compression, or set the compression and file size and let it handle the width. You also might need to keep an eye out on maximum width/height in case somebody uploads an efficiently compressed file that exceeds sensible limits. I've had that bite me when converting user uploaded PDFs that ended up being like 20,000x20,000 pixels in size with ImageMagick or MacOS.
Seems like it should still work looking at the docs: https://imagemagick.org/script/defines.php Restrict the maximum JPEG file size, for example -define jpeg:extent=400KB. The JPEG encoder will search for the highest compression quality level that results in an output file that does not exceed the value. The -quality option also will be respected starting with version 6.9.2-5. Between 6.9.1-0 and 6.9.2-4, add -quality 100 in order for the jpeg:extent to work properly. Prior to 6.9.1-0, the -quality setting was ignored.
👍 1
You'd have to see if whatever java wrapper you found supported that, or like @Mark Takata (Adobe) said - run it via cfexecute.
q
Thanks. I'll explore that a bit more then. Thanks!
im4java seems to have done the trick. This will be a "good enough" solution for the project.
👍 1