Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 582
user1431849 Avatar asked Jul 10 '12 17:07

user1431849


People also ask

How do I reduce image size but keep quality?

Compress the image: If it still exceeds the maximum size or weight of 2 mb you can go one step further by compressing it. To compress an image, many tools offer a sliding scale. Moving to the left of the scale will reduce the image's file size, but also its quality.

Does reducing image size reduce quality?

The most common side effect of scaling an image larger than its original dimensions is that the image may appear to be very fuzzy or pixelated. Scaling images smaller than the original dimensions does not affect quality as much, but can have other side effects.

How do I lower the filesize of an image?

The primary way to reduce the file size of an image is by increasing the amount of compression. In most image editing applications this is done by the selections you make in the “Save As” or “Export As” dialog box when saving a PNG, JPG, or GIF.

Does reducing file size reduce quality?

Many people think that lowering the resolution of an image also lowers the file size of the image, allowing it to download faster over the web. But while it's true that smaller files sizes download faster, the resolution of your image has nothing to do with its file size.


1 Answers

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.

like image 176
Vengat Owen Avatar answered Oct 08 '22 02:10

Vengat Owen