Don't overwrite original if optimized is larger
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.
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.
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.
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.
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.
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 :).
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.
Hi Winston, I am sorry for long wait, are you willing to finish this PR?
Do we know if this is still an issue/desirable to fix/merge?
I don't know if it is an issue, but if it is, it could be cool to have this functionality.
@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