Skip to content

Instantly share code, notes, and snippets.

@scumola
Created July 10, 2012 17:55
Show Gist options
  • Select an option

  • Save scumola/3085132 to your computer and use it in GitHub Desktop.

Select an option

Save scumola/3085132 to your computer and use it in GitHub Desktop.
php file that will resize images on the fly
<?php
#error_reporting(E_ALL);
#ini_set('display_errors', '1');
$src = $_GET["src"];
$wmax = $_GET["wmax"];
$hmax = $_GET["hmax"];
$quality = $_GET["quality"];
$bgcol = $_GET["bgcol"];
#$src = str_replace("images.bidboxr.com","127.0.0.1",$src);
#$src = str_replace("badcheese.dyndns.org","127.0.0.1",$src);
#$src = str_replace("localhost","127.0.0.1",$src);
if (preg_match('/\.gif$/i', $src)) {
$source = imagecreatefromgif($src);
} elseif (preg_match('/\.jp(|e)g$/i', $src)) {
$source = imagecreatefromjpeg($src);
} elseif (preg_match('/\.png$/i', $src)) {
$source = imagecreatefrompng($src);
} else {
$info = getimagesize($src);
$mime_type = image_type_to_mime_type($info[2]);
switch($mime_type) {
case "image/gif":
$source = imagecreatefromgif($src);
break;
case "image/jpeg":
case "image/jpg":
$source = imagecreatefromjpeg($src);
break;
case "image/png":
$source = imagecreatefrompng($src);
break;
case "image/xbm":
$source = imagecreatefromxbm($src);
break;
default:
if (!$source) {
$source = imagecreatefromjpeg("./notfound.jpg");
}
}
if (!$source) {
$source = imagecreatefromjpeg("./notfound.jpg");
}
}
Header ("Content-type: image/jpeg");
Header ("Last-Modified: " . gmdate("D, d M Y H:i:s",mktime (0,0,0,1,1,2000)) . " GMT");
Header ("Expires: Mon, 26 Jul 2040 05:00:00 GMT");
#Header ("Cache-Control: max-age=10000000, s-maxage=1000000, proxy-revalidate, must-revalidate");
Header ("Cache-Control: max-age=1000000, s-maxage=1000000");
$orig_w=imagesx($source);
$orig_h=imagesy($source);
if ($orig_w>$wmax || $orig_h>$hmax) {
$thumb_w=$wmax;
$thumb_h=$hmax;
if ($thumb_w/$orig_w*$orig_h>$thumb_h)
$thumb_w=round($thumb_h*$orig_w/$orig_h);
else
$thumb_h=round($thumb_w*$orig_h/$orig_w);
} else {
$thumb_w=$orig_w;
$thumb_h=$orig_h;
}
if (!@$bgcol) {
$thumb=imagecreatetruecolor($thumb_w,$thumb_h);
imagecopyresampled($thumb,$source,
0,0,0,0,$thumb_w,$thumb_h,$orig_w,$orig_h);
} else {
$thumb=imagecreatetruecolor($wmax,$hmax);
imagefilledrectangle($thumb,0,0,$wmax-1,$hmax-1,intval($bgcol,16));
imagecopyresampled($thumb,$source,
round(($wmax-$thumb_w)/2),round(($hmax-$thumb_h)/2),
0,0,$thumb_w,$thumb_h,$orig_w,$orig_h);
}
if (!@$quality) $quality=90;
imagejpeg($thumb,"",$quality);
imagedestroy($thumb);
?>
@scumola
Copy link
Author

scumola commented Jul 10, 2012

Need to fix the bgcol stuff - it's not working at this moment. For now, just leave that part out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment