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

btmgreg

Active member
Would 100% use. I'm very conscious of how much space I'm consuming. I always try and remember to optimise images before uploading, but somehow usually forget.

Please add this!
 

Chris D

XenForo developer
Staff member
You mention resizing, but we already resize images based on the attachment options.

So is this suggestion just for customising the quality/compression?
 

frm

Well-known member
So is this suggestion just for customising the quality/compression?
I didn't realize that was an option.

So, yes. My suggestion is compression with ImageMagic/GD (whichever the admin uses) and able to set their own compression rates (as opposed to the other suggestion that uses a 3rd party API).
 

Chris D

XenForo developer
Staff member
Well, that’s what the maximum dimensions option is for. If an uploaded image exceeds the dimensions specified we resize it to within those dimensions.

And FWIW in a lot of cases it reduces the need for any compression - we already compress to 85% quality when we are resizing or otherwise saving the image.
 

Faust

Active member
Well, that’s what the maximum dimensions option is for. If an uploaded image exceeds the dimensions specified we resize it to within those dimensions

Usually when someone is trying to upload an image which is bigger than the amount set, they having an error. And user has to compress is the image itself.
 

btmgreg

Active member
Usually when someone is trying to upload an image which is bigger than the amount set, they having an error. And user has to compress is the image itself.
Which, makes sense with the setting (maximum file size) - but that should be considered before compressing. So the original file size makes no odds when the new file size should be considerably smaller once optimised.
 

Faust

Active member
Which, makes sense with the setting (maximum file size) - but that should be considered before compressing. So the original file size makes no odds when the new file size should be considerably smaller once optimised.

I do use TH image optimisation addon, which does compress a bit more the photo once has been uploaded.
 

Chris D

XenForo developer
Staff member
Usually when someone is trying to upload an image which is bigger than the amount set, they having an error. And user has to compress is the image itself.
That’s not correct.

Though there is a config setting that provides a hard limit on image pixel size in order to prevent memory errors on lower end servers.

Check the config page in the manual. If you are hitting it regularly you may need to change the config value but otherwise if an image exceeds the dimensions the image is resized. As mentioned if it hits the file size limit then it will be rejected but we consider the file size limit after resizing and there’s usually no situation where an image would exceed even a conservative limit if it has been resized to a reasonable dimensions.
 

frm

Well-known member
So this whole time I've been uploading 100% and XF has been compressing to 85%? I want the ability to upload at 100% because I want to preserve every pixel.

Therefore, this suggestion should be implemented so that there can be 100% quality, 85% quality, or you could run some insights and see how far down some pictures can get (i.e. Image1.jpg is 400kb, it could be 375kb, so you decrease quality and run the cron job to get it reduced further).

Knowing this, I might as well upload DNG/RAW files now...
 

Chris D

XenForo developer
Staff member
So this whole time I've been uploading 100% and XF has been compressing to 85%? I want the ability to upload at 100% because I want to preserve every pixel.

Therefore, this suggestion should be implemented so that there can be 100% quality, 85% quality, or you could run some insights and see how far down some pictures can get (i.e. Image1.jpg is 400kb, it could be 375kb, so you decrease quality and run the cron job to get it reduced further).

Knowing this, I might as well upload DNG/RAW files now...
Clearly that's not what you want because otherwise you wouldn't have suggested configurable compression, surely?

I have never witnessed this feature doing its job. I did a quick test on XF 2.1 but got an error:

  • Uploaded image: 6000 x 7000
  • Maximum attachment image dimensions: 5120 x 2880
As I said, there's a config setting that provides a hard limit. You will need to increase it if you wish to handle large images.
 

frm

Well-known member
Clearly that's not what you want because otherwise you wouldn't have suggested configurable compression, surely?
I plan on running over 10 forums by the end of the year.

My first forum, I want 100%. This is because of a lesson learned from WWII with the preservation of 35mm+ prints from the battlefield. They were copied and copied until they were faded. The restoration process to get them back to 'print' quality is quite costly. Therefore, I want to preserve the history of this place (geo-forum) now, for the future on this one forum.

Other forums with pictures of guns, memes, movie posters, (just examples) etc., I don't care about and would like them compressed (but until it's core, like this suggestion, it will save as is because I have the disk space to store them until a 2.3/2.4 update that'll "Smush It" with ImageMagic[/GD] and don't like the idea of relying on an add on and especially not Smush It).

I mainly created this suggestion because nobody else would with the Smush It suggestion that relies on a 3rd-party API call that can disappear at any time or have some new ToS or limitations that require users to upgrade their accounts. But, doing it server-side with ImageMagic/GD allows for XF to maintain it and have more control over its direction. Perhaps nobody understood that ImageMagic or GD has compression abilities? I don't know and someone challenged me to make it, so I did.
 

btmgreg

Active member
Long story cut short - it would be nice to have the same features and intent as resmush in core. If it is already in core, then brilliant. Maybe this could be something an addon creator will or could make at some point.
 

Chromaniac

Well-known member
Honestly, i would be happy with a convert all images to webp as an option sometime next year when most Apple device users would have updated to stable builds of the upcoming iOS/macOS builds. Webp basically resolves most of the requirements here. It would reduce size of images significantly and is also suitable for all kinds of images. And then switch from webp to avif once apple adds support for it in the future.

Right now you have multiple image formats which require different kind of optimizers for best results (oxipng for pngs, mozjpeg for jpegs and so on).
 

Faust

Active member
That’s not correct.

Though there is a config setting that provides a hard limit on image pixel size in order to prevent memory errors on lower end servers.

Check the config page in the manual. If you are hitting it regularly you may need to change the config value but otherwise if an image exceeds the dimensions the image is resized. As mentioned if it hits the file size limit then it will be rejected but we consider the file size limit after resizing and there’s usually no situation where an image would exceed even a conservative limit if it has been resized to a reasonable dimensions.

I should have notice that I did not set any dimensions limits. Only the limit of how big the image can be. Which is 5mb in my case.
 

Chromaniac

Well-known member
I imagine it is a good time to ask this question that has bugged me in the past. XenForo has a dedicated phrase named uploaded_file_is_too_large which has the content The uploaded file is too large. (>5MB). Since upload size is a configurable option, I am not sure why this exists. I have seen this error message while uploading images that are much smaller than 5MB while my server was configured to support attachments of 8MB.
 

Chris D

XenForo developer
Staff member
XenForo has a dedicated phrase named uploaded_file_is_too_large which has the content The uploaded file is too large. (>5MB).
That is our phrase but we do not stipulate any file sizes in it. You've either customised the phrase or you have an add-on which is appending additional information to the error message.
 
Top