Skip to content

Instantly share code, notes, and snippets.

@axd
Last active October 25, 2016 22:52
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 axd/5578003 to your computer and use it in GitHub Desktop.
Save axd/5578003 to your computer and use it in GitHub Desktop.
<?php
if(isset($_GET['img'])) $image = getimagesize($_GET['img']);
else if ($image === false) trigger_error('Error: The URL given is an invalid image.', E_USER_ERROR);
else trigger_error('Error: The $img variable is no set.', E_USER_ERROR);
$qlty = isset($_GET['cqlty']) && $_GET['cqlty'] <= 100 ? $_GET['cqlty'] : 80;
$mime = explode('/', $image['mime']);
$ext = strtolower($mime[1]);
list($width, $height) = $image;
switch($ext){
case('jpg'): case('jpeg'):
$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:
trigger_error('Error: The image format is not supported.', E_USER_ERROR);
break;
}
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['cut']) && (!isset($_GET['cwidth']) && !isset($_GET['cheight']))){
$new_width = $new_height = $_GET['cwidth'] = $_GET['cheight'] = min($width, $height);
}
if(isset($_GET['cheight']) || isset($_GET['cwidth'])){
if(isset($_GET['cut'])){
if(is_numeric($_GET['cut'])) $new_width = $new_height = $_GET['cut']; // If a number is given, make it square
$w_centre = round($width / 2);
$h_centre = round($height / 2);
$w_half = round($new_width / 2);
$h_half = round($new_height / 2);
$x1 = max(0, ($w_centre - $w_half));
$y1 = max(0, ($h_centre - $h_half));
$x2 = min($width, ($w_centre + $w_half));
$y2 = min($height, ($h_centre + $h_half));
$image_resized = imagecreatetruecolor($x2 - $x1, $y2 - $y1);
if($ext == 'png' || $ext == 'gif') imagealphablending($image_resized, false);
imagecopy($image_resized, $buffer, 0, 0, $x1, $y1, $width, $height);
}
else{
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagealphablending($image_resized, false);
imagecopyresampled($image_resized, $buffer, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
}
}
else $image_resized = $buffer;
if($ext == 'png' || $ext == 'gif') imagesavealpha($image_resized, true);
$function = 'image'.$ext;
header('Content-type: '.$image['mime']);
if($ext == 'gif') $function($image_resized);
else $function($image_resized, null, $qlty);
if(is_resource($buffer)) imagedestroy($buffer);
if(is_resource($image_resized)) imagedestroy($image_resized);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment