How to reduce the image size without losing quality in PHP [closed]
Asked Answered
A

4

49

I am trying to develop an image-based web site. I am really confused about the best image type for faster page loading speeds and best compression practices. Please advise me on the best way to compress image sizes.

Adachi answered 10/7, 2012 at 17:22 Comment(1)
Jpeg, gif and png images are already compressed. Can you clarify?Boraginaceous
G
12

I'd go for jpeg. Read this post regarding image size reduction and after deciding on the technique, use ImageMagick

Hope this helps

Geraldine answered 10/7, 2012 at 17:26 Comment(0)
R
119

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.

Rutharuthann answered 16/10, 2013 at 13:34 Comment(6)
this function convert the source images to JPEG, ignore the gif animations and png transparencyStephine
to overcome png transparency issue mentioned by @nikmauro, i used the imagepng instead of imagejpeg. using the imagejpeg for jpg files, ofcourse just modifying if statement to use appropriate function. however to save transparency i also had to add (before output/save) : imagealphablending($image, false); imagesavealpha($image, true); I also had to devide the $quality by 10 to work as imagepng does not go from 0 - 100 but rather 0 - 9Piracy
Good one there. But one thing that bothers me is why 90 for the compression? Is there a min value which when passed, the image is going to loose its "originality"?Translucid
I'm not sure why but the image gets rotated when I use this code, how would I prevent this from happening. It always seems to be rotated 90 degrees anti clockwise.Lucarne
sorted, I had to read the exif data from the original image and adjust accordingly.Lucarne
Great! This is working but I'm getting 1 issue with it. While I'm uploading image thru the mobile camera then it sometimes rotates that image 90 degrees. What to do about that??Amathiste
G
12

I'd go for jpeg. Read this post regarding image size reduction and after deciding on the technique, use ImageMagick

Hope this helps

Geraldine answered 10/7, 2012 at 17:26 Comment(0)
E
10

You can resize and then use imagejpeg()

Don't pass 100 as the quality for imagejpeg() - anything over 90 is generally overkill and just gets you a bigger JPEG. For a thumbnail, try 75 and work downwards until the quality/size tradeoff is acceptable.

imagejpeg($tn, $save, 75);
Eckardt answered 10/7, 2012 at 17:26 Comment(1)
how to move the resized image to particular folder?Anting
P
4

well I think I have something interesting for you... https://github.com/whizzzkid/phpimageresize. I wrote it for the exact same purpose. Highly customizable, and does it in a great way.

Porky answered 5/1, 2013 at 22:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.