ElkArte Community

Extending Elk => Addons => Topic started by: Spuds on April 17, 2014, 12:51:57 pm

Title: [ADDON] Resize attached images
Post by: Spuds on April 17, 2014, 12:51:57 pm
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
Title: Re: [ADDON] Resize attached images
Post by: radu81 on January 21, 2016, 05:26:06 am
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
Title: Re: [ADDON] Resize attached images
Post by: Spuds on January 21, 2016, 10:43:21 am
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.


Title: Re: [ADDON] Resize attached images
Post by: radu81 on January 21, 2016, 12:21:12 pm
In SMF I use this (http://custom.simplemachines.org/mods/index.php?mod=2206) mod and I agree that the quality at 75% is not so good and I always used 80% :
(http://i.imgur.com/lMB8WYu.jpg)

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
Title: Re: [ADDON] Resize attached images
Post by: Spuds on January 21, 2016, 01:01:38 pm
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
Title: Re: [ADDON] Resize attached images
Post by: radu81 on January 21, 2016, 02:06:37 pm
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
Title: Re: [ADDON] Resize attached images
Post by: Spuds on January 21, 2016, 02:29:39 pm
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. 
Title: Re: [ADDON] Resize attached images
Post by: radu81 on January 21, 2016, 03:43:31 pm
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
Title: Re: [ADDON] Resize attached images
Post by: badmonkey on January 30, 2017, 04:56:00 pm
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!!
Title: Re: [ADDON] Resize attached images
Post by: Spuds on January 30, 2017, 08:32:27 pm
Are you trying to use it on 1.1 ?
Title: Re: [ADDON] Resize attached images
Post by: badmonkey on January 30, 2017, 08:40:07 pm

Yes.  It's worth mentioning it worked perfectly on the same site, same version on the other server. 
Title: Re: [ADDON] Resize attached images
Post by: ahrasis on January 31, 2017, 05:25:34 am
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).
Title: Re: [ADDON] Resize attached images
Post by: badmonkey on January 31, 2017, 07:05:03 am
As far as I know it is not enabled.  Network console on FF indicates http 1.1.  Please correct if that isn't.  Thanks!
Title: Re: [ADDON] Resize attached images
Post by: Spuds on January 31, 2017, 12:20:45 pm
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 )
Title: Re: [ADDON] Resize attached images
Post by: badmonkey on January 31, 2017, 01:53:46 pm
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. 
Title: Re: [ADDON] Resize attached images
Post by: Spuds on January 31, 2017, 04:46:46 pm
Grrrr ... namespace issue.  for ElkArte 1.1 ONLY, give this  Air_Resize.subs.php  (13.36 KB) a try. 

I'm note sure why you are not seeing the problem on your other 1.1 sites, since it should not work.  Maybe you don't have it enabled or have not triggered it due to the size limit or  ?  But really the existing mod should not work!
Title: Re: [ADDON] Resize attached images
Post by: badmonkey on January 31, 2017, 07:08:56 pm
Most interesting.  The images get resized, for what that's worth.  If there's something strange, it's in the monkey cage. 

I'll try it tonight.  Thanks spuds!
Title: Re: [ADDON] Resize attached images
Post by: badmonkey on January 31, 2017, 09:11:04 pm
That works!!  Thanks a million spuds.  You da man.  Sorry to be so troublesome. 
Title: Re: [ADDON] Resize attached images
Post by: ahrasis on February 01, 2017, 12:05:38 am
Good job. So now it's clearly not a new server fault after all. Though I however found it a little bit surprising when the same working forum only found error in the new server, this discussion is now out of topic. I hope there is a board where a discussion relating to a server can be made.
Title: Re: [ADDON] Resize attached images
Post by: shaitan on February 06, 2017, 02:28:51 pm
French translation in attachment.
Title: Re: [ADDON] Resize attached images
Post by: Spuds on February 10, 2017, 09:17:23 am
I've updated the package http://addons.elkarte.net/utility/Attachment-Resize.html to version 1.0.2

It includes the above translation
Adds support for 1.1
Fixes a bug when allowing to convert from PNG to JPG, it was not updating the mime_type value in the DB
Title: Re: [ADDON] Resize attached images
Post by: radu81 on March 07, 2017, 05:30:52 pm
Quote from: Spuds – Fixes a bug when allowing to convert from PNG to JPG, it was not updating the mime_type value in the DB
this never worked for me, I just updated the addon and I upload a png file, but is not converted into jpg
Title: Re: [ADDON] Resize attached images
Post by: Spuds on March 07, 2017, 07:26:48 pm
It would only convert it to JPG if it was required in order to obtain the file size limit.  So if its already a small enough file, then it should not change the encoding.
Title: Re: [ADDON] Resize attached images
Post by: radu81 on March 07, 2017, 07:40:25 pm
in the test I did, the original size of png image is 1,1 MB and once resized became 550 Kb (1200 px width) and the transparency disappeared, which makes me think is converted to jpg, but the file extension is still png. I thought it also change the file extension from png to jpg
Title: Re: [ADDON] Resize attached images
Post by: Spuds on March 07, 2017, 08:14:15 pm
It will
 
No idea on the transparency or the encoding, there could still be a bugs ! ... Then again without the specific image and settings I'm kind of left choking my chicken :D
Title: Re: [ADDON] Resize attached images
Post by: radu81 on March 08, 2017, 04:00:12 am
I should stop making tests late night  :-[
I was wrong about the image, the trasparency is kept in png file, I was looking to a file edited by me  :-X . I attach here a zip containing the original, the resized file, and the settings I use.
Title: Re: [ADDON] Resize attached images
Post by: Spuds on March 08, 2017, 08:37:07 am
Cool .. I'll do some testing!

ETA:

Looking at your attachment settings, you have "Max size per attachment" set to 0, which means no limit.   This means there is no practical size limit imposed on an individual file.

The original PNG image was (1818 x 827) which is larger than the (1200 x 1200) limit you have set.  As such the file was proportionally resized to (1200 x 546) which saves some file size as expected (1Mb -> 541Kb)

At this point the program would check if 541Kb is at or under the individual size limit, and since you have no limit set, it passes that check and the program is done.

If however, and only for an example, you set a size limit of 400Kb, then the program would take that 541Kb oversize PNG and re-encode it as a JPG to see if it would then be <= 400Kb, and if so save it as that (a JPG). 

So again the Allow non-JPEG images to be reformatted to JPEG format when resizing."  setting will allow the system to reformat non-JPEG images into the JPEG format when necessary.  The system will try to maintain the existing format unless the resulting image is still in excess of the maximum file size allowed.  If it can keep a PNG as a PNG, it will do so (since its lossless)\

Now I'm going to actually test :D
Title: Re: [ADDON] Resize attached images
Post by: radu81 on March 08, 2017, 05:00:49 pm
Quote from: Spuds – At this point the program would check if 541Kb is at or under the individual size limit, and since you have no limit set, it passes that check and the program is done.

If however, and only for an example, you set a size limit of 400Kb, then the program would take that 541Kb oversize PNG and re-encode it as a JPG to see if it would then be <= 400Kb, and if so save it as that (a JPG). 
Now it's clear to me how it works, I misunderstood this part. I thought if I set a limit ( let's say 400 kb per image) I won't be able to upload an image bigger than 400 KB (original dimension). That's why I've set this field to 0.


Quote from: Spuds – So again the Allow non-JPEG images to be reformatted to JPEG format when resizing."  setting will allow the system to reformat non-JPEG images into the JPEG format when necessary.  The system will try to maintain the existing format unless the resulting image is still in excess of the maximum file size allowed.  If it can keep a PNG as a PNG, it will do so (since its lossless)\

Now I'm going to actually test :D
I just test it, and in fact it works as you described. I've set a 400kb limit per image and the PNG file is saved as jpg. I attach it here

Converting a png to jpg remove transparency, so the transparent part is now black. Is there a way to change this behaviour by changing transparent to white?
Title: Re: [ADDON] Resize attached images
Post by: Spuds on March 08, 2017, 06:26:05 pm
For Imagick only ....

in Graphics.subs.php
Code: (find) [Select]
			// Set jpeg image quality to 80
if ($default_formats[$preferred_format] === 'jpeg')
{
$imagick->setImageCompression(Imagick::COMPRESSION_JPEG);
$imagick->setImageCompressionQuality(80);
}
Code: (replace) [Select]
			// Set jpeg image quality to 80
if ($default_formats[$preferred_format] === 'jpeg')
{
$imagick->borderImage('white', 0, 0);
$imagick->setImageCompression(Imagick::COMPRESSION_JPEG);
$imagick->setImageCompressionQuality(80);
}

So just adding in that
$imagick->borderImage('white', 0, 0);
line should do the "trick"

Also note that 400 (or whatever size) you use is only enforced after the upload, as is the max size for all attachments.  The addon updates the attachment form to allow it to accept a file as large as your server setting will allow and the group of files to the max as well (post_max_size and file_max_size values or whatever they are).   This allows someone to upload their 5000 x 5000 x 6Mb cellphone pic and yet be resized (and possibly re-encoded) to fit to the ACP values.  Capisce  :)
Title: Re: [ADDON] Resize attached images
Post by: Jorin on March 09, 2017, 01:09:57 am
There seems to be two mistakes in the english language files:

Code: [Select]
$txt['helpattachment_image_enabled_size'] = 'or large than %s KB.';

Missing "r" - should be "or larger..."

Code: [Select]
$txt['help_attachment_image_height'] = 'This allows you to set a maximum width for attachment images. Pictures smaller than the maximum will not be affected, larger images will be resized proportionately. This allows you to accept a larger image on upload and have it resized to save space. The maximum filesize parameter is still enforced.';

Should be "'This allows you to set a maximum height for attachment images." or not?

Below is the german translation.

Can it be there's a little bug with the help texts? When I click on one of those small "?" icons a window opens without text.  :o
Title: Re: [ADDON] Resize attached images
Post by: radu81 on March 09, 2017, 05:06:07 am
Thanks Spuds, that works perfectly, attached the result. What do you think about the quality? For me is good enough, we are talking about a 68 KB jpg file, 1200 px width.

Yep, it was clear enough from your previous message  8)
Title: Re: [ADDON] Resize attached images
Post by: radu81 on March 09, 2017, 05:29:23 am
I think I found a bug:  ;D
The precedent tests were made editing an existing topic, so I had no problems attaching the 1,1MB PNG image, but if I try to reply to a topic attaching the same image I get: Aston_Martin_Car_PNG_Car_Clipart-99.png : (1040 KB) Your file is too large. The maximum attachment size allowed is 500 KB.
I don't think this is normal, if I can update a post with an image I should also be able to upload the same image to a new post. Now I remember why I set that limit to 0. ;)


Title: Re: [ADDON] Resize attached images
Post by: Jorin on March 09, 2017, 06:56:10 am
Are you talking about the general file size limit for an attachment?

If so, I don't have the bug. My limit is set to 20 mb though.  O:-)
Title: Re: [ADDON] Resize attached images
Post by: Spuds on March 09, 2017, 09:11:09 am
QuoteWhat do you think about the quality?
Certainly showing artifacts.  In that same code where you made the edit, you could try changing the 80% to 85% and see how you like that.  Will be slightly larger but should help with the blocking going on.

QuoteI don't think this is normal,
Sounds like a bug as well ... will check !

QuoteBelow is the german translation.

Can it be there's a little bug with the help texts?
Those work for me, but will check again.  Thanks for the translation and spell checking  ;)


Title: Re: [ADDON] Resize attached images
Post by: Spuds on March 09, 2017, 09:31:38 am
Quote from: radu81 – The precedent tests were made editing an existing topic, so I had no problems attaching the 1,1MB PNG image, but if I try to reply to a topic attaching the same image I get: Aston_Martin_Car_PNG_Car_Clipart-99.png : (1040 KB) Your file is too large. The maximum attachment size allowed is 500 KB.
I have not been able to duplicate this so far ... when you go to reply, what does it say in the attachment area, there should be a line like .... (Restrictions: 20 per post, maximum total size 8,192 KB, maximum individual size 4,096 KB) The size values should be your server max limits (as determined by the addon), not the limits set in the admin panel.  If the addon is off then it would show the admin panel limits.

Also I tried 85% on the jpeg quality, not much change in file size or quality ....
Title: Re: [ADDON] Resize attached images
Post by: radu81 on March 09, 2017, 10:02:29 am
When editing an existing message I see:
Allowed file types: doc, gif, jpg, pdf, txt, zip, png
Restrictions: 20 per post, 18 remaining, maximum total size 1,048,576 KB, 1,048,389 KB available, maximum individual size 524,288 KB

when posting a new reply
Allowed file types: doc, gif, jpg, pdf, txt, zip, png
Restrictions: 20 per post, maximum total size 10,000 KB, maximum individual size 500 KB
Title: Re: [ADDON] Resize attached images
Post by: Spuds on March 09, 2017, 11:06:49 am
What values are in your phpinfo() ( can use admin-> main -> support and credits and select more detailed next to php info for:

upload_max_filesize
post_max_size

Also to improve the jpeg quality, add
Code: [Select]
				$imagick->setSamplingFactors(array('1x1', '1x1', '1x1'));
right after the borderImage line that you added to get the white background.  Let me know what you think of that.  Yes the image is a bit larger (88.25kb) but it greatly reduced the artifacts IMO.
Title: Re: [ADDON] Resize attached images
Post by: radu81 on March 09, 2017, 12:45:21 pm
Upload_max_filesize 512 MB 
Post_max_size 1024
There is a phpinfo.php in the root of my domain.

I'll try the edit you suggest, I'm on mobile right now.

Anyway, please have  a look at my precedent post, is clear that in edit mode use the hosting values (which is OK) while in posting a new reply it uses the smf values. They are different
Title: Re: [ADDON] Resize attached images
Post by: Spuds on March 09, 2017, 01:53:52 pm
Strange.

When you get a chance, open up Air_Resize.subs.php, find the function ipb_air_prepost()  (its the first one).

Enter a
Code: [Select]
var_dump($function_name);
inside the if statement, right before the "// Showing the post, or attempting to post?" comment.

Then try the modify, reply, and new topic actions and let me know what get pushed out at the top of the page.   They should all say string 'action_index' (length=12)  You can then remove that var_dump line, I'm just wanting to see if another mod is causing a problem.
Title: Re: [ADDON] Resize attached images
Post by: radu81 on March 09, 2017, 03:48:28 pm
I did the edit you suggested, and I am getting:
- in edit mode: string(12) "action_index" (Restrictions: 20 per post, 18 remaining, maximum total size 1,048,576 KB, 1,048,389 KB available, maximum individual size 524,288 KB)
- in reply mode: string(12) "action_post2" (Restrictions: 20 per post, maximum total size 10,000 KB, maximum individual size 500 KB)
- in new topic: string(12) "action_index" (Restrictions: 20 per post, maximum total size 1,048,576 KB, maximum individual size 524,288 KB)

In edit mode and new topic I am able to add PNG > 500 kb and they get converted to jpg. Only in reply mode it keeps the elkarte settings for single attachment.

Addons installed:
Attachment Image Resize 1.0.2
Fancy Box 4 ElkArte 1.0.1 (unistalled, and installed the 1.0.2 version)
Google Analytics tracking 0.0.1
Google Member Map 1.0.2
ImageCache 1.0
Inline Attachments 1.0
No Frills Snow for ElkArte 1.0
NoFollow 0.0.2
Quick Quote 0.2
Remove Last Edit By 1.0.0
SimpleAds 1.0.2
SimplePortal 1.0.0 Beta 1
Smush.it! 0.3

Thanks for your help Spuds.

EDIT: I unistalled ILA addon but nothing changed. Still testing...

I think I found something useful:
I am using the quick reply (simple editor, no smileys or editor buttons). I was not using the "Reply button" but the "Preview" button, so when trying to add a new reply:
- using Preview button: string(12) "action_post2" (Restrictions: 20 per post, maximum total size 10,000 KB, maximum individual size 500 KB)
-using Reply button: string(12) "action_index" (Restrictions: 20 per post, maximum total size 1,048,576 KB, maximum individual size 524,288 KB)

So the problem is only when I use the "Preview" button. Topic edit, new topic and topic reply using the "Reply" button works fine  8)
Title: Re: [ADDON] Resize attached images
Post by: Spuds on March 09, 2017, 04:09:05 pm
Its that action_post2 that is causing the issue, the addon only changes the limits for action_index ... just curious that you are seeing a action_post2  :-\   I'll have to install some of those addons and see if I can see where that is happening.

for now you can test by changing the if statement in the ipb_air_prepost (same on you got the debug info from) to
Code: [Select]
		if ($function_name === 'action_index' || $function_name === 'action_post2')

That should fix the form issue and allow you to send in a larger file, but I'm not sure if there will be side effect like it does not resize / reencode the image as it should.  I'll try to do some testing tonight.
Title: Re: [ADDON] Resize attached images
Post by: radu81 on March 09, 2017, 04:13:13 pm
sorry Spuds I just edited the precedent post (probably while you were replying)
Title: Re: [ADDON] Resize attached images
Post by: radu81 on March 09, 2017, 04:42:35 pm
Finaly solved, the problem was... me :D since I never use the Reply button, instead I use the Preview button which brings me to full editor, but as you show there are different functions, action_index for Reply and action_post2 for Preview. Don't ask why I use the Preview button! I don't know, probably because is always there near to my mouse cursor :)

Quote from: Spuds –
Code: [Select]
		if ($function_name === 'action_index' || $function_name === 'action_post2')

That should fix the form issue and allow you to send in a larger file, but I'm not sure if there will be side effect like it does not resize / reencode the image as it should.  I'll try to do some testing tonight.
That did the trick, with this edit I am able to upload PNG files and they get resized and converted to JPG :)

Title: Re: [ADDON] Resize attached images
Post by: Jorin on May 09, 2017, 04:02:28 am
Is there a way (or make it an option in the addon settings please) to open images not in the same tab/window, but in a different?
Title: Re: [ADDON] Resize attached images
Post by: radu81 on May 09, 2017, 06:04:48 am
@Jorin‍ I think your question is related to fancybox addon and not to resize attached images addon
Title: Re: [ADDON] Resize attached images
Post by: Jorin on May 09, 2017, 06:28:56 am
I don't use the fancybox addon, just this one. When I click on a large picture it opens in the same window as the forum. I wish it would open in a new window instead. So it's not this addon?  :-[
Title: Re: [ADDON] Resize attached images
Post by: Spuds on May 09, 2017, 07:29:53 am
How an image is displayed is not a function of this addon.  If you are referring to the expand in place I think thats a function of the Max Displayed width setting, setting that to something low would I think force it to display in a new tab or window (which depends on how you have your browser set up and can't be controlled by the site)
Title: Re: [ADDON] Resize attached images
Post by: Jorin on May 09, 2017, 07:42:53 am
Hm, okay. Can someone please split my post?  O:-)

Max width and height is set to 0 (no limit). Images like these (http://forum-alternative-antriebe.de/index.php/topic,6784.msg111440.html#msg111440) are opened not under the post itself, but as a new page in the same window. What I wish when clicking on the thumbnail is that the images should always open in a new window (keeping the forum opened), no matter how wide or high.

I think that some users may close the image and the forum too by mistake.

I know the browser has such an option. But doesn't have the forum too?

Second problem: Some images are opened under the post, some or opened as a new page. How can I change that?

I never understood these settings.  O:-)
Title: Re: [ADDON] Resize attached images
Post by: Spuds on May 09, 2017, 11:42:33 am
I believe this is they way those values work.

Leaving those at 0,0  means that the system will always try to expand the image under the post, even if that overflows the width of your page, that is the behavior I see on your site.

Setting that to, for example,  800,800 means that images 800,800 and below will expand under the post and images > 800,800 will instead open a new tab/window.

Setting that to say 1,1 should effectively force all images to open in a new window / tab  (there is no way to force it to open a new window if the user has their browser set to open new windows as a tab)
Title: Re: [ADDON] Resize attached images
Post by: Jorin on May 10, 2017, 01:12:55 am
Quote from: Spuds – Leaving those at 0,0  means that the system will always try to expand the image under the post, even if that overflows the width of your page, that is the behavior I see on your site.

With my browser here at work these images are opened in the same window, not under the post. Hmmm...

Thanks for the explanation. I will check it at home later.
Title: Re: [ADDON] Resize attached images
Post by: emanuele on May 10, 2017, 07:36:43 am
/me feels Jorin has some weird browsers at work. :P j/k ;)
Title: Re: [ADDON] Resize attached images
Post by: Jorin on May 10, 2017, 07:47:34 am
Seems so! lol
Title: Re: [ADDON] Resize attached images
Post by: radu81 on July 04, 2017, 11:38:37 am
Quote from: Spuds – Cool .. I'll do some testing!

ETA:

Looking at your attachment settings, you have "Max size per attachment" set to 0, which means no limit.  This means there is no practical size limit imposed on an individual file.

The original PNG image was (1818 x 827) which is larger than the (1200 x 1200) limit you have set.  As such the file was proportionally resized to (1200 x 546) which saves some file size as expected (1Mb -> 541Kb)

At this point the program would check if 541Kb is at or under the individual size limit, and since you have no limit set, it passes that check and the program is done.

If however, and only for an example, you set a size limit of 400Kb, then the program would take that 541Kb oversize PNG and re-encode it as a JPG to see if it would then be <= 400Kb, and if so save it as that (a JPG). 

So again the Allow non-JPEG images to be reformatted to JPEG format when resizing."  setting will allow the system to reformat non-JPEG images into the JPEG format when necessary.  The system will try to maintain the existing format unless the resulting image is still in excess of the maximum file size allowed.  If it can keep a PNG as a PNG, it will do so (since its lossless)\

Now I'm going to actually test :D

What do you think about the idea to split the field "Max size per attachment" in two fields:
1. Max file per attachment - checks only if the original attached file is bigger than the "Max file file per attachment". Let's say I can set this to 5 MB, so photos bigger than 5 MB won't be uploaded
2 Resize PNG for file bigger than:
let's say I can set this to 400 KB, so the addon can resize or convert to jpg only files bigger than 400 KB.

With the actual settings the images are resized correctly but I cannot post .zip or .pdf files bigger than 400 kb.
Title: Re: [ADDON] Resize attached images
Post by: radu81 on August 16, 2017, 06:02:35 pm
Quote from: radu81 – With the actual settings the images are resized correctly but I cannot post .zip or .pdf files bigger than 400 kb.
 
 sorry if I insist on this, ore there any news?
Title: Re: [ADDON] Resize attached images
Post by: kucing on September 27, 2017, 09:01:11 pm
How about adding an option force all images to JPEG?

If I change the attachment size now, will it affect past attachments? or just the newly added one?
Title: Re: [ADDON] Resize attached images
Post by: radu81 on September 28, 2017, 02:20:34 am
Just the newly attachments added
Title: Re: [ADDON] Resize attached images
Post by: Spuds on September 28, 2017, 08:29:37 am
I feel that an option to force everything to JPG will result in problems.  Converting a graphic that was intended to be a PNG to a JPG can sometimes really make it look quite awful.   Thats why the current system will only do that (when enabled) if the PNG image can't be made to fit inside the set limits.

Quoteelk, ? in avatar setting ?,  can auto-convert resized img to PNG.
CAN that be temporarily be altered to convert to JPG?
OT: If you don't have "Use PNG for resized avatars" enabled then they will be saved as JPG (assuming that the image has to be resized).
Title: Re: [ADDON] Resize attached images
Post by: badmonkey on September 28, 2017, 08:38:55 am

Can't say for sure the Elk version behaves this way. Using smf2.1 old attachments would get resized once they were viewed. Google bots resized every attachment on the forum!  Lol
Title: Re: [ADDON] Resize attached images
Post by: Spuds on November 26, 2017, 05:27:08 pm
Updated package to support ElkArte 1.1
Title: Re: [ADDON] Resize attached images
Post by: hartiberlin on January 20, 2018, 07:46:00 pm
Quote from: badmonkey –
Quote from: radu81 –
Quote from: kucing – or just the newly added one?
Just the newly attachments added

Can't say for sure the Elk version behaves this way. Using smf2.1 old attachments would get resized once they were viewed. Google bots resized every attachment on the forum!  Lol
Can this plugin be updated , that it also resizes and reduces the filesize for already uploaded pictures ?
Or is there any PHP Script with which I can do this manually , e.g. resize all image attachments to 1024x768 at quality 60 in Jpeg format to save server space ?
Many thanks.
Title: Re: [ADDON] Resize attached images
Post by: radu81 on January 22, 2018, 07:44:40 pm
Can't answer to your question, just a note, don't use a 60% quality, it's too low as quality, you will notice the differences. I'd suggest you to use something about 80-85%, a normal jpg file resized to 1024 X 768 at 80% will be about 100-120 kb, which is already a  good result. 
Title: Re: [ADDON] Resize attached images
Post by: hartiberlin on January 23, 2018, 09:34:07 am
Yes, I don´t want the attachments bigger than 100 Kbytes each...
Title: Re: [ADDON] Resize attached images
Post by: hartiberlin on January 24, 2018, 02:57:37 pm
Can this plugin be updated , that it also resizes and reduces the filesize for already uploaded pictures ?
Title: Re: [ADDON] Resize attached images
Post by: forumovod on September 15, 2020, 02:23:34 am
Russian localization.
Title: Re: [ADDON] Resize attached images
Post by: radu81 on September 20, 2020, 06:20:58 am
Still using this add-on and running well, I have a suggestion for it:
My users are posting screenshots from phones, and most of them are .png files, which are taking more space than a .jpg file.
Here an example:
- the png file 909 Kb
- the jpg file I converted on my PC is only 172 Kb (converted with command "mogrify -format jpg *.png")
Since it's an image without transparency imho it has no sense to be saved as PNG. What I suggest, obviously if it's possible: if the png file does not contain transparency then save it as JPG, if has transparency leave it as PNG.

Title: Re: [ADDON] Resize attached images
Post by: badmonkey on September 20, 2020, 09:28:23 am
Isn't that the Allow non-JPEG images to be reformatted to JPEG format when resizing setting?
Title: Re: [ADDON] Resize attached images
Post by: radu81 on September 20, 2020, 03:12:04 pm
Allow non-JPEG images to be reformatted to JPEG format when resizing.
 Selecting this option will allow the system to reformat non-JPEG images into the JPEG format when necessary. The system will try to maintain the existing format unless the resulting image is still in excess of the maximum file size allowed.


I just checked, I have that enabled, but is useless in my case because I have set high values for:
Max attachment size per post 9600 KB
Max size per attachment 5000 KB
Max number of attachments per post 15
Title: Re: [ADDON] Resize attached images
Post by: Spuds on September 23, 2020, 11:19:06 am
Quote from: radu81 – Allow non-JPEG images to be reformatted to JPEG format when resizing.
 Selecting this option will allow the system to reformat non-JPEG images into the JPEG format when necessary. The system will try to maintain the existing format unless the resulting image is still in excess of the maximum file size allowed.


I just checked, I have that enabled, but is useless in my case because I have set high values for:
Max attachment size per post 9600 KB
Max size per attachment 5000 KB
Max number of attachments per post 15
So I'm assuming that the addon was triggered by WxH image pixel size limits?  I like the idea of checking alpha channel info but would likely need another ACP option since right now it did what you specified (e.g. it falls within the constraints you set)
Title: Re: [ADDON] Resize attached images
Post by: Spuds on September 23, 2020, 08:29:51 pm
Here is something to test .... for 1.1 only .... Replace your existing file with this one (keep the old one safe in case you need to back out)

This will identify non transparent PNG files and if you have the checkbox to allow format conversion enabled, then it will change those non transparent PNG to a JPG (assuming the PNG was to big WxH or KB)

Sadly the only way to check for transparency is to examine every pixel in an image to see if it any have alpha value < 1.  This indicates that it has some level of transparency.  You can't simply trust how the file was saved (simple header read) since you can save a PNG that has no transparency with the transparency flag, thats just the way things work.

Should work with either GD or Imagick
Title: Re: [ADDON] Resize attached images
Post by: radu81 on September 24, 2020, 09:24:51 am
Thanks for your support Spuds! This seem to work fine on my forum:
- uploaded PNG screenshot from mobile without transparency (BMW car), the image is saved as jpg (900 Kb in PNG -> 97 Kb in jpg format, size 1200 x 900). This is great, I just saved 0,8 MB with only 1 png image!  8)
- uploaded a small PNG file with transparency, the file was saved correctly as png with transparency (400 x 335 px, 30,9 KB -> same size, same dimension)
- uploaded a big PNG file with transparency (3500 × 2284 px, 982 KB -> saved as PNG with transparency 1200 x 783 px, 281 KB)

I only tested with Imagick. Thanks again!
Title: Re: [ADDON] Resize attached images
Post by: Spuds on September 24, 2020, 10:44:17 am
Cool, thanks for testing.  I'll pack this up and update the addon soon-ish
Title: Re: [ADDON] Resize attached images
Post by: forumovod on April 26, 2021, 11:38:25 am
Will there be an update of this addon for the version ElkArte 1.1.7
Title: Re: [ADDON] Resize attached images
Post by: radu81 on April 26, 2021, 11:56:52 am
nothing to report for now, it works with 1.1.7
Title: Re: [ADDON] Resize attached images
Post by: Spuds on November 06, 2022, 09:23:44 am
Minor update to version 1.0.7


To get the addon you can do one of two things: