Skip to content

Instantly share code, notes, and snippets.

@serdna
Created March 27, 2012 21:55
Show Gist options
  • Save serdna/2220727 to your computer and use it in GitHub Desktop.
Save serdna/2220727 to your computer and use it in GitHub Desktop.
PicHandler, resize images on the fly
<?
if(!isset($_GET['img'])) die('No URL image given');
$image = getimagesize($_GET['img']);
$qlty = isset($_GET['cqlty']) ? ($_GET['cqlty'] <= 100 ? $_GET['cqlty'] : 80) : 80;
if ($image === false) die;
$mime = explode('/', $image['mime']);
$ext = $mime[1];
switch($ext){
case('jpg'): case('jpeg'): case('jpe'):
$buffer = imagecreatefromjpeg($_GET['img']);
break;
case('png'):
$buffer = imagecreatefrompng($_GET['img']);
$qlty = ($qlty * 0.1 == 10) ? 9 : ceil($qlty * 0.1);
break;
case('gif'):
$buffer = imagecreatefromgif($_GET['img']);
break;
default:
return false;
break;
}
$width = $image[0];
$height = $image[1];
if(isset($_GET['cwidth'])){
$new_width = $_GET['cwidth'];
$new_height = !isset($_GET['cheight']) ? $height * ($new_width/$width) : $_GET['cheight'];
}
if(isset($_GET['cheight'])){
$new_height = $_GET['cheight'];
$new_width = !isset($_GET['cwidth']) ? $width * ($new_height/$height) : $_GET['cwidth'];
}
if(isset($_GET['cheight']) || isset($_GET['cwidth'])){
$function = 'image'.$ext;
if(isset($_GET['cut'])){
if(isset($_GET['cwidth'])){
$image_resized = imagecreatetruecolor($_GET['cwidth'], ($new_height < $_GET['cwidth'] ? $new_height : $_GET['cwidth']));
}
if(isset($_GET['cheight'])){
$image_resized = imagecreatetruecolor($new_width, ($new_width > $_GET['cheight'] ? $new_width : $_GET['cheight']));
}
}
else $image_resized = imagecreatetruecolor($new_width, $new_height);
imagealphablending($image_resized, false);
if(isset($_GET['cut']) && isset($_GET['cheight'])) imagecopyresampled($image_resized, $buffer, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
else imagecopyresampled($image_resized, $buffer, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//ImageAlphaBlending($image_resized, true);
imagesavealpha($image_resized, true);
header('Content-type: '.$image['mime']);
if($ext == 'gif') $function($image_resized);
else $function($image_resized, null, $qlty);
imagedestroy($buffer);
imagedestroy($image_resized);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment