Compress Uploaded Images Using ImageMagic or PHP GD (Admin Choice)

frm

Well-known member
My suggestion is to use ImageMagic or PHP GD, whichever is selected by the admin, to compress images to their choice (say 40% to 80%) as well as resize them to a maximum width (which will obviously resize their height to keep the same ratio too).

This will save some people enormous disk space and could also run retroactively for older photos uploaded (possibly on the cron).

In order to not be a resource hog, it would also queue up all photos as well as run a cron job to process X images not compressed every X minutes/hours (with admin able to execute it, like all other cron jobs, at non-peak hours to get ahead on compressing). This is so that someone couldn't upload 100 images and all resources would fly over to ImageMagic/GD to compress and resize. Rather, they'd upload at 100% and slowly, but surely, all compress.

Using this method wouldn't rely on a 3rd party API (such as Smush It) which could change at a moment's notice, keeping XF on their feet to maintain API calls, and be easier to maintain going forward with the development of XF.
 
Upvote 15

frm

Well-known member
we already compress to 85% quality when we are resizing or otherwise saving the image.
Is this template editable, or hard coded? Eg. can I already customise compression quality and settings, or it will need (this) suggestion?
If this is a hardcoded 85%, the easiest solution seems to be to a config.php variable for the compression/quality rate we wish to use moving forward.

I understand that we already have 85% for prior uploads (so there's no going back to 100% for them), but I would like to set the compression to 0 or quality 100 (and much lower than 85% on subsequent forums). The idea of the cron could then be dropped and older forums could set the compression at a different rate after compressing them even further.

I think this would be a happy medium and a quicker implementation.
 

Chromaniac

Well-known member
Based on past posts (i have tinkered with this setting before)... the values are hardcoded in /src/XF/Image/Imagick.php and /src/XF/Image/Gd.php files.
 

frm

Well-known member
Based on past posts (i have tinkered with this setting before)... the values are hardcoded in /src/XF/Image/Imagick.php and /src/XF/Image/Gd.php files.
Seems to me that it could rationally be made default (as is) to 85% unless something such as $config['imageCompression'] = 65; is specifically set in config.php to change it to 65% to save even more space (or in my case, 100).
 

frm

Well-known member
Based on past posts (i have tinkered with this setting before)... the values are hardcoded in /src/XF/Image/Imagick.php and /src/XF/Image/Gd.php files.
Seems to me that it could rationally be made default (as is) to 85% unless something such as $config['imageCompression'] = 65; is specifically set in config.php to change it to 65% to save even more space (or in my case, 100).
Would I need to make a separate suggestion—supposing it's hardcoded—for this @Chris D or would the votes of Smush It + this imply that it'd be a popular implementation and a happy medium for XF core?
 

Chromaniac

Well-known member
tbh, compression through this route is probably going to generate worse images than using dedicated image optimizers like mozjpeg. plus this only comes into effect if the image is resized on upload (image dimensions are larger than configured). images that are under set limits are not compressed using this setting at all i believe.
 

Chromaniac

Well-known member
it is easy to check if images are compressed on just uploads. from what i have seen, file size matches after upload :) Imagick.php has two mentions of 85 quality. i imagine the other one is for quality of thumbnails which are also saved on every upload.
 
Top