Skip to main content
[ADDON] Resize attached images Started by Spuds · · Read 41748 times 0 Members and 1 Guest are viewing this topic. previous topic - next topic

[ADDON] Resize attached images

Attachment Image Resizing V1.0.7

License
o This ElkArte Addon is subject to the terms of the Mozilla Public License version 1.1 (the "License"). You can obtain a copy of the License at http://mozilla.org/MPL/1.1/

Introduction
This addon when enabled, will allow a user to post images (up to the maximum allowed in your php.ini) and then resize them to maximum WxH dimensions and/or size constraints.   As an example it will allow a member to upload a 3MB image of 3264x2448 and it will be resized it to, for example, 1280x1024.    This saves space on your server and bandwidth serving the attachments.  If the resized image is still > the the mas allowed max size (as set in the admin control panel)  it will be rejected as normal.

Automatically resizes attached images that are larger than a defined  width X height bounding or those that are larger than the allowed maximum size KB.

Will resize attachment imagess (.jpg, .png, .gif, or .bmp) to fit within the bounds specified.  The image format will be maintained unless it is unable to fit the resized image within the max allowed file size specified.  In this case, if the optional change format is enabled, the system will convert the image to JPEG for better compression.

Features:
o The ability to set a max width and height for attached images, images are resized proportionately
o The ability to allow converting image formats only if its required to maintain the file within the specified max file size

Repo / Download
http://addons.elkarte.net/utility/Attachment-Resize.html
Last Edit: November 06, 2022, 09:30:09 am by Spuds

Re: [ADDON] Resize attached images

Reply #1

I like this addon, I use it on my elkarte forum and I always used an alternative mod on SMF. Imo it's important to resize images to save space and bandwith, mobile phones nowadays take 3-4MB picture.

Today I did some tests, I uploaded 5.5MB image and I set the images to be resized at 1024px width. The image was reduced to 587 kb, I did the same test on my SMF forum and the image was reduced to 131KB. I take a look at both pictures and I cannot visible differences in terms of quality. I also did a test from photoshop with "Convert to web" and the image was about 170 kb. I am wondering how come I have this differences in size? I also updated the mod to the lastest version.

I am not sure about this, but I noticed that images were not being reduced so much in size since I installed Imagemagick. (imagemagick was installed to use the feature to rotate attachments)

Quoteo The ability to allow converting image formats only if its required to maintain the file within the specified max file size
To be honest I did not find this setting in elk. I attach a screen with my settings

Re: [ADDON] Resize attached images

Reply #2

There are a couple of things here.   First it has nothing to do with the addon :D

PART 1
In SMF the default JPG compression used by GD/GD2  was not set, which means it used the default quality of 75.  75 is to aggressive, you start to loose to much information at that level.  As such Elk changed (set) that value 80, which you could still argue is hair to aggressive, but that's what I picked at the time.

So SMF may compress an image more, but at the expense of quality, and since these days no one is uploading 640x480 images quality becomes more interesting.

PART 2
In Elk, we also utilize Imagick, over GDx, when its available.   In honor of not setting proper defaults, we did not set any quality level for JPG compression when using Imagick :P ... It defaults to 87 (AFAIK), so much larger images than SMF at 75 and large than ELK w/o Imagick support.   This is a bug which should be fixed in 1.0.7 / 1.1

In Graphics.subs.php find
Code: (find) [Select]
			// Set the input and output image size
$src_width = empty($src_width) ? $imagick->getImageWidth() : $src_width;
$src_height = empty($src_height) ? $imagick->getImageHeight() : $src_height;
$dest_width = empty($max_width) ? $src_width : $max_width;
$dest_height = empty($max_height) ? $src_height : $max_height;

and add after that
Code: [Select]
			// Set jpeg image quality to 80
if ($default_formats[$preferred_format] === 'jpeg')
{
$imagick->setImageCompression(Imagick::COMPRESSION_JPEG);
$imagick->setImageCompressionQuality(80);
}

Please add that and then try your test again and let us know how it works.



Re: [ADDON] Resize attached images

Reply #3

In SMF I use this mod and I agree that the quality at 75% is not so good and I always used 80% :


I made the edit you suggested and now I am getting a 201 Kb image, which is perfect for me.   ;)

Thank you for your fast fix
sorry for my bad english

Re: [ADDON] Resize attached images

Reply #4

Good, glad it improved it.  Past that you are looking at differences between choices / decisions inside of Imagick vs GD.

You can also run the smush.it addon, that one is lossless and can get ~10% reduction on top what this addon does ... that one however is not realtime like this addon, but instead runs as a scheduled task, like once a day or every 8 hours or whatever.  NOTE although its still called smush.it it uses another service since smush.it was dropped (an never really supported anyway) by Yahoo

Re: [ADDON] Resize attached images

Reply #5

Quote from: Spuds – Past that you are looking at differences between choices / decisions inside of Imagick vs GD.
sorry but I did not understand this part
Quote from: Spuds – is not realtime like this addon, but instead runs as a scheduled task, like once a day or every 8 hours or whatever.
that make sense now to me.  I tried in the past your old addon for SMF and I did not see any difference and uninstalled quicly. I thought is realtime  ;D I will try it on elkarte
Quote from: Spuds – NOTE although its still called smush.it it uses another service since smush.it was dropped
I've read about it in another topic here  ;)

Thanks for all your help
sorry for my bad english

Re: [ADDON] Resize attached images

Reply #6

Quotesorry but I did not understand this part
Just saying that a jpeg quality of 80% is not necessarily treated equally by all programs, its not really a standard, at least how I understand things.  Since you are getting a slightly large file with IM vs GD at 80%, it likely means IM is being less aggressive in size in favor of somewhat better quality. 

Re: [ADDON] Resize attached images

Reply #7

Thanks and yes I agree, SMF which is using GD resized the same image, same quality percentage at 131kb, while imagick did it to 200 kb
sorry for my bad english

Re: [ADDON] Resize attached images

Reply #8

Hey guys!  I'm in the middle of setting up a new server  and migrating over.  Currently there is a test site mostly operational on the new machine.  Gotta iron out all the bugs first!  haha!  This mod is crucial for my sites. 

The issue I'm having is outlined here:  http://www.elkarte.net/community/index.php?topic=4227.msg30388#msg30388

Basically posting and attachments work if this mod is uninstalled or disabled.  However, with AIR enabled this error occurs:

Code: [Select]

FastCGI sent in stderr: "PHP message: PHP Fatal error:  Class 'Attachment_Error_Context' not found in /var/www/clients/client0/web1/web/sources/subs/Air_Resize.subs.php on line 126" while reading response header from upstream,


Any help is (desperately) appreciated!!

Re: [ADDON] Resize attached images

Reply #9

Are you trying to use it on 1.1 ?

 

Re: [ADDON] Resize attached images

Reply #10


Yes.  It's worth mentioning it worked perfectly on the same site, same version on the other server. 

Re: [ADDON] Resize attached images

Reply #11

Did you enable http2? It could be the cause. Try to disable it (disabling it is tedious as you have to remove from all vhost).

Re: [ADDON] Resize attached images

Reply #12

As far as I know it is not enabled.  Network console on FF indicates http 1.1.  Please correct if that isn't.  Thanks!

Re: [ADDON] Resize attached images

Reply #13

Humm .... I have not tried AIR on 1.1 just yet, as a start, open up sources/subs/Air_Resize.subs.php

Code: (find) [Select]
		if ($function_name === 'action_post2' && Attachment_Error_Context::context()->hasErrors())
Code: (replace) [Select]
		if ($function_name === 'action_post2' && AttachmentErrorContext::context()->hasErrors())
And see if that fixes it ... I think its just a name change ( Attachment_Error_Context => AttachmentErrorContext )

Re: [ADDON] Resize attached images

Reply #14

Unfortunately no.  Replaced three other occurrences in the same file.  Also a no go.  Same php error reflecting the name change.  The strange thing is the mod works on the old server with 1.1b4 on other sites.