Can I convert PNG or jpeg to 25% size of original image by using php GD library? I can't install extra php extension.
-
May this help you: http://stackoverflow.com/questions/15288967/php-gd-library-resize-photo http://code.runnable.com/UnF-tFdudNt1AABt/how-to-resize-an-image-using-gd-library-for-php Hope it helps... let me know if it does ;) – Twinfriends Oct 19 '16 at 12:49
-
1Above URL is resizing image. I want image optimisation. – Arshid KV Oct 19 '16 at 13:14
-
What is meaning of *Optimise*? Do you mean change size of image or change image resulation? – Mohammad Oct 26 '16 at 11:21
-
1Reduce image file size without changing dimensions (with , height). – Arshid KV Oct 26 '16 at 13:54
-
Possible duplicate of [Which is the best PHP method to reduce the image size without losing quality](http://stackoverflow.com/questions/11418594/which-is-the-best-php-method-to-reduce-the-image-size-without-losing-quality) – Mohammad Oct 29 '16 at 20:40
1 Answers
If you're wanting to reduce the file size to 25% of that of the original image, you may need some trial-and-error, which can be costly from a computational and memory standpoint, as in GD the only way to do this is as follows:
$old_bytes=filesize($original_file_path);
$image=imagecreatefromjpeg($original_file_path);
imagejpeg($image, $new_file_path, $quality_guess);
$new_bytes=filesize($new_file_path);
$ratio=$old_bytes/$new_bytes;
The tricky thing is: how accurate do you want to get? You will have to encapsulate the above code, or something like it, in some sort of loop and carry it out multiple times, and then make the subjective judgment call of deciding when to stop. Ideally you want $ratio
close to 4.
If your source images are all roughly similar in terms of quality settings, you might find it much better to manually test things out for a single file, and just decide on a fixed quality setting that achieves the reduction in filesize that you're looking for.
In my experience though, it is generally a bad idea to aim for consistent filesize or consistent reduction in filesize, so you might want to rethink your end goal here. Usually I approach image compression based on quality, i.e. I take a sample of images and then I compress them with different quality settings (75 is often a good starting point) and send them around to a few different users on different devices (after looking at them myself on different devices) and I pick the lowest quality setting at which no one can notice a decrease in quality. Often it is surprisingly low, but it will also be heavily dependent on the specifics of the image. Certain images are just more compressable than others, based on the structure of their content.
So if you're aiming for a fixed size-reduction, you might find that much of the time you're compressing a lot less than you could (i.e. you could lower the quality even more without users noticing) but you might also find that some of the time you are actually reducing quality to terrible levels. Like, imagine one user who leaves the quality settings on 100, and then another user that had mindfully optimized the quality settings based on perception. If your code reduces both of them to 25% file size, you'll probably produce far fewer savings than you could have on that originally-100-quality image, but you're probably going to end up with a visibly terrible image on that carefully-optimized image.
So...yes, you could do what you want, but it will be computationally intensive and I am having trouble imagining a scenario where it would be a good idea.

- 516
- 6
- 19