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.
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
Tidak ada komentar:
Posting Komentar