How to Reduce the Image Size Without Losing Quality in PHP

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



Leave a reply



Submit