Skip to content

Instantly share code, notes, and snippets.

@alfredbez
Last active December 25, 2015 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfredbez/7039179 to your computer and use it in GitHub Desktop.
Save alfredbez/7039179 to your computer and use it in GitHub Desktop.
Optimize Image with php
<?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