image_optim icon indicating copy to clipboard operation
image_optim copied to clipboard

Don't overwrite original if optimized is larger

Open R167 opened this issue 10 years ago • 10 comments

Currently, the original image gets overwritten if the optimized version is larger (happens in some cases). Changing it so that only the smaller image is preserved when optimize_image! is called.

R167 avatar Aug 05 '15 03:08 R167

Thanks for contribution!

But there is a trick about this: workers are responsible for checking if the size was reduced (check usage of optimized? method in workers) and all but one use it. The only exception currently is jhead, but its task is to rotate jpeg images which have exif orientation set (otherwise further workers will remove exif and the image will not be oriented properly) and it can sometimes make image bigger. Also it is good to get rid of orientation flag and rotate the images so even viewers ignoring the orientation flag will display it properly. So forbidding bigger output images can be added as an option, but not by default.

toy avatar Aug 05 '15 07:08 toy

Cool. Changed it so that it is now an optional parameter which is disabled by default. The main reason that I am adding this, is that some of the images that we were trying to optimize (all of which were pngs) were growing by a couple hundred to thousand bytes.

R167 avatar Aug 05 '15 16:08 R167

It is very strange that png images are growing in size, is it possible for you to share some of them? It looks like a bug.

About the PR: I think it would be more meaningful to call the option replace_bigger or maybe even better to reverse and call it skip_bigger. Also instead of adding it as an option only for this method, it would be better to add it to config so it would be available for optimize_image and for the image_optim script. But first it is better to check why pngs are growing in size.

toy avatar Aug 06 '15 20:08 toy

Well crap, turns out that it was a miscommunication and instead of being a bug with the gem, it was a bug with how I was previously optimizing the images (before switching to image_optim). No one attached the commit SHA to the file compare so I misinterpreted the problem. Sorry about that.

R167 avatar Aug 06 '15 20:08 R167

This pull request can still be useful for someone using image_optim on jpeg files, so if you want you can finish it and add the option to produce only smaller files :).

toy avatar Aug 06 '15 21:08 toy

You've committed lib/.DS_Store. Probably it is better to add .DS_Store to .gitignore, I just have it in my global git ignore file.

toy avatar Aug 08 '15 20:08 toy

Hi Winston, I am sorry for long wait, are you willing to finish this PR?

toy avatar Jul 09 '16 18:07 toy

Do we know if this is still an issue/desirable to fix/merge?

R167 avatar Jul 06 '18 16:07 R167

I don't know if it is an issue, but if it is, it could be cool to have this functionality.

kaspergrubbe avatar Jul 08 '18 18:07 kaspergrubbe

@R167 It is still possible for jpegs to be overwritten with bigger file if original has exif rotation header, becomes bigger and is badly optimised

toy avatar Jul 08 '18 21:07 toy