How to reduce the image size without losing quality in PHP
I'd go for jpeg
. Read this post regarding image size reduction and after deciding on the technique, use ImageMagick
Hope this helps
php resize image without loss quality
imagejpeg uses 75 quality by default. So you need to define it explicitly.
imagejpeg($thumb, $filename."_prev.jpg", 100);
Also, use imagecopyresampled
imagecopyresampled() copies a rectangular portion of one image to
another image, smoothly interpolating pixel values so that, in
particular, reducing the size of an image still retains a great deal
of clarity.
How do I reduce image size in php?
First of all hello welcome. Please search before asking questions first.
If you are looking to reduce the size using coding itself, you can follow this code in php.
<?php
function compress($source, $destination, $quality) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
imagejpeg($image, $destination, $quality);
return $destination;
}
$source_img = 'source.jpg';
$destination_img = 'destination .jpg';
$d = compress($source_img, $destination_img, 90);
?>
$d = compress($source_img, $destination_img, 90);
This is just a php function that passes the source image ( i.e., $source_img
), destination image ( $destination_img
) and quality for the image that will take to compress ( i.e., 90 ).
$info = getimagesize($source);
The getimagesize()
function is used to find the size of any given image file and return the dimensions along with the file type.
Referrer : Which is the best PHP method to reduce the image size without losing quality
PHP: How to compress images without losing visible quality (automatically)?
TinyPNG uses pngquant.
Pngquant has option to set desired quality, similar to JPEG. You can run something like:
<?php system('pngquant --quality=85 image.png'); ?>
Pngquant website has example code showing how to use pngquant from PHP.
For JPEG you can apply lossless jpegcrush.
JpegMini (commercial) and jpeg-archive (free) are lossy and can can automatically find a minimal good quality for a JPEG.
In PHP you can roughly estimate how much JPEG was compressed by observing how much file size changes after re-compression. File size of JPEG recompressed at same or higher quality will not change much (but will lose visual quality).
If you recompress JPEG and see file size halved, then keep the recompressed version. If you see only 10-20% drop in file size, then keep the original.
If you're compressing yourself, use MozJPEG (here's an online version).
How can i reduce uploaded image size without losing quality(much)
You can check this repository, I think it’s great with custom configuration like how much quality you want to retain.
https://github.com/spatie/image-optimizer
Edit: Or if you want to make it simple, you can use this paid api: https://kraken.io/
Related Topics
How to Send Emails via Cron Job Usng PHP MySQL
Friendship System with Laravel:Many to Many Relationship
Laravel - HTMLspecialchars() Expects Parameter 1 to Be String, Object Given
How to Run the PHP Code Asynchronous
Check Whether the String Is a Unix Timestamp
Codeigniter PHP Framework - Need to Get Query String
PHP Form Send Email to Multiple Recipients
Can PHP Namespaces Contain Variables
Generating Xml Document in PHP (Escape Characters)
Post and Get at the Same Time in PHP
What Can Use for Datetime::Diff() for PHP 5.2
Create Programmatically a Variable Product and Two New Attributes in Woocommerce