Compress image size and quality using PHP [ Very very simple way ]

Advertisemen

You I don't give you bla bla bla bla....
So,
Here is the function I created, It doesn't just change the sizes also reduce the quality of image. I am using it my new website and app so I know it works very well.


<?php

function compress_image($source_url, $destination_url, $quality)
{
   
$info = getimagesize($source_url);

   
if ($info['mime'] == 'image/jpeg')
       $image = imagecreatefromjpeg($source_url);
   
elseif ($info['mime'] == 'image/gif')
       $image = imagecreatefromgif($source_url);
   
elseif ($info['mime'] == 'image/png')
       $image = imagecreatefrompng($source_url);

   
$percent = 0.2;
   
list($width, $height) = getimagesize($source_url);
   
$new_width = $width * $percent;
   
$new_height = $height * $percent;
   
$image_p = imagecreatetruecolor($new_width, $new_height);
   
imagecopyresampled($image_p, $image, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);

   
//save file
   
imagejpeg($image_p, $destination_url, $quality);

   
//return destination file
   
return $destination_url;
}
?>


Few things to keep in mind while you are using this function. $percentage = 0.2, in the function above determines the sizes of image so if you bigger, go higher and vice versa, range between 0 - 1.

When you call this function :
$source_url = $_FILES['files']['tmp_name'];
$destination_url = the location where you want to save your file in server i.e. uploads/...../...
$quality = this is range between 0 - 100 so if you want better quality go higher and vice versa.

Call this function when you are uploading images not when you are extracting from the server.

If you have any problem understanding this, feel free to comment below.

Thanks




Advertisemen

Disclaimer: Gambar, artikel ataupun video yang ada di web ini terkadang berasal dari berbagai sumber media lain. Hak Cipta sepenuhnya dipegang oleh sumber tersebut. Jika ada masalah terkait hal ini, Anda dapat menghubungi kami disini.

Tidak ada komentar:

Posting Komentar

© Copyright 2017 Tutorial Unity 3D