Last active
December 25, 2015 20:59
-
-
Save alfredbez/7039179 to your computer and use it in GitHub Desktop.
Optimize Image with php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function image_optimize ($source_url, $destination_url, $quality) { | |
$info = getimagesize($source_url); | |
if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url); | |
elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url); | |
else return 'error: only jpeg and png are supported'; | |
// Enable interlancing | |
imageinterlace($image, true); | |
$png_quality = intval((($quality / 10)-10)*(-0.9)); | |
//save file | |
if ($info['mime'] == 'image/jpeg') imagejpeg($image, $destination_url, $quality); | |
elseif ($info['mime'] == 'image/png') imagepng($image, $destination_url, $png_quality); | |
//return destination file | |
return $destination_url; | |
} | |
image_optimize('original/file.jpg','klein/file.jpg',75); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment