Skip to content

Instantly share code, notes, and snippets.

@bubbobne
Created November 30, 2016 12:53
Show Gist options
  • Save bubbobne/86b413064c6e6ada0bf941573594ae5e to your computer and use it in GitHub Desktop.
Save bubbobne/86b413064c6e6ada0bf941573594ae5e to your computer and use it in GitHub Desktop.
<?php
/**
*Crea thumb.
*/
function make_thumb($src, $dest,$type, $desired_width) {
try{
if($type=="png"){
$source_image =imagecreatefrompng($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
$desired_height = floor($height * ($desired_width / $width));
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
imagepng($virtual_image, $dest);}
else{
$source_image =imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
$desired_height = floor($height * ($desired_width / $width));
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
imagejpeg($virtual_image, $dest);
}
} catch (Exception $e) {
die('Caught exception: '.$e->getMessage());
}
};
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment