I’m currently trying to speed up the websites we develop. The part I’m working on now is to optimise the images so that they are as small (filesize, not dimensions) as possible without losing quality.
Our customers can upload their own images to the website through our custom CMS, but images aren’t being compressed or optimised at all. My superior explained this is because the customers can upload their own images, and these images could be optimised beforehand through Photoshop or tools like it. If you optimise already optimised images, the quality would get worse. …right?
We’re trying to find a solution that won’t require us to install a module or anything. We already use imagejpeg()
, imagepng()
and imagegif()
, but we don’t use the $quality
parameter because of reasons previously explained. I’ve seen some snippets, but they all use imagejpg()
and the like.
That being said, is there a sure-fire way of optimising images without the risk of optimising previously optimised images? Or would it be no problem at all to use imagejpeg()
, imagepng()
and imagegif()
, even if it would mean optimising already optimised images?
Thank you!
4
Answers
Going from your title, I am going to assume compression
So, lets say a normal jpg of 800×600 is uploaded by your customers.
The customers jpg is 272kb because it has full details and everything.
You need to set tresholds for filesizes at dimensions what is acceptable.
Like:
and that way you set up parameters for what is acceptable as an upper limit of file size. If the dimensions match, and the filesize is bigger, then its not optimised.
But without knowing more details what exactly is understood about optimising, this is the only thing I can think of.
“If you optimise already optimised images, the quality would get worse. ”
No if you use a method without loose.
I don’t know for method directly in php but if you are on linux server you can use jpegtran or jpegoptim ( with –strip-all) for jpeg and OptiPNG or PNGOUT for png.
If you are using a low number of images to compress, you might find an external service, such as: https://tinypng.com/developers might be of assistance.
I’ve used their on-line tools for reducing filesize on both JPG and PNG file manually but they do appaear to offer a free API service for the first 500 images per month.
Apologies if this would be better as a comment than an answer, I’m fairly new to stackoverflow and haven’t got enough points yet, but felt this may be a handy alternative solution.
Saving a JPEG with the same or higher quality setting will not result in a noticeable loss in quality. Just re-save with your desired quality setting. If the file ends up larger, just discard it and keep the original. Remove metadata using jpegran or jpegoptim before you optimize so it doesn’t affect the file size when you compare to the original.
PNG and GIF wont’t lose any quality unless you reduce the number of colors. Just use one of the optimizers Gyncoca mentioned.