Skip to content

Instantly share code, notes, and snippets.

@solepixel
Created August 29, 2015 13:23
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 solepixel/96bf89feb24ed1f09f04 to your computer and use it in GitHub Desktop.
Save solepixel/96bf89feb24ed1f09f04 to your computer and use it in GitHub Desktop.
Image thumbnail creator
<?php
/*
JPEG / GIF / PNG Resizer / Image Viewer
Parameters (passed via URL):
src = path / url of jpeg or png image file
percent = if this is defined, image is resized by it's
value in percent (i.e. 50 to divide by 50 percent)
w = image width
h = image height
thumb: set = displays thumbnail.
not set = displays original image.
force: by default this script will constrain the image proportions. If you wish to
force with width/height settings, set &force in the URL
width and height and thumb MUST be set.
Requires the PHP GD Extension
Originally By: Michael John G. Lopez - www.sydel.net
Modified By: Brian DiChiara - www.briandichiara.com
Modifications:
Supports Transparent PNG and GIF
Does not resize images smaller than specified w/h
Supports Remote files (http://www.notyoursite.com/image.jpg)
Usage Example:
<img src="image.php?src=myimage.jpg&thumb&w=200&h=200" border="0" alt="my image" />
*/
function getExtension($f){
$pos = strrpos($f, '.');
if(!$pos) {
return '';
}
$str = substr($f, $pos + 1, strlen($f));
return strtolower($str);
}
$img = $_GET['src'];
$ext = strtolower(getExtension($_GET['src']));
$percent = (isset($_GET['percent']) && !empty($_GET['percent'])) ? $_GET['percent'] : false;
//$constrain = (isset($_GET['constrain'])) ? ($_GET['constrain'] == 0 || $_GET['constrain'] == "false" || $_GET['constrain'] == "0") ? false : true : true;
$force = (isset($_GET['force']) && isset($_GET['w']) && isset($_GET['h']) && !empty($_GET['w']) && !empty($_GET['h'])) ? true : false;
$remote = @file ($img);
if($remote){
$fp = @fopen ($img, 'rb');
$data = '';
while (!feof ($fp)){
$data .= fgets($fp, 4096);
}
$src = @imagecreatefromstring($data);
$sw = @imagesx($src);
$sh = @imagesy($src);
} else {
$dims = @getimagesize($img); // get width/height of original image
$sw = $dims[0]; //returns width
$sh = $dims[1]; //returns height
}
$w = (isset($_GET['w']) && !empty($_GET['w'])) ? $_GET['w'] : 0; //desired maximum width
$h = (isset($_GET['h']) && !empty($_GET['h'])) ? $_GET['h'] : 0; //desired maximum height
$resize = false; //assume no resize is needed
if ($percent> 0) {
// calculate resized height and width if percent is defined
$percent = $percent * 0.01;
$w = $sw * $percent;
$h = $sh * $percent;
} else {
$resize = (($sh> $h && isset($h) && $h != 0) || ($sw> $w && isset($w) && $w != 0)) ? true : $resize;
$resize = ($force) ? true : $resize;
if(($resize && isset($_GET['thumb']))){
if (isset ($w) && (!isset ($h) || $h == 0)) {
// autocompute height if only width is set
$h = (100 / ($sw / $w)) * .01;
$h = @round ($sh * $h);
} elseif (isset ($h) && (!isset ($w) || $w == 0)) {
// autocompute width if only height is set
$w = (100 / ($sh / $h)) * .01;
$w = @round ($sw * $w);
} elseif (isset ($h) && isset ($w) && !$force) {
// get the smaller resulting image dimension if both height
// and width are set and $constrain is also set
$hx = (100 / ($sw / $w)) * .01;
$hx = @round ($sh * $hx);
$wx = (100 / ($sh / $h)) * .01;
$wx = @round ($sw * $wx);
if ($hx <$h) {
$h = (100 / ($sw / $w)) * .01;
$h = @round ($sh * $h);
} else {
$w = (100 / ($sh / $h)) * .01;
$w = @round ($sw * $w);
}
}
} else {
$w = $sw;
$h = $sh;
}
}
switch ($ext){
case "jpg" : $type = "jpeg"; break;
case "bmp" : $type = "x-ms-bmp"; break;
case "png" : $type = "x-png"; break;
case "tif" : $type = "x-tiff"; break;
case "ico" : $type = "x-icon"; break;
default : $type = $ext; break;
}
if((!$resize || !isset($_GET['thumb'])) && !remote){
//display contents
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-Length: '.filesize($img));
header('Content-type: image/' . $type);
readfile($img);
} else {
$im = ($remote) ? $src : false;
switch ($ext){
case "jpg" :
case "jpeg" : $im = @imagecreatefromjpeg($img); break;
case "wbmp" : $im = @imagecreatefromwbmp($img); break;
case "png" : $im = @imagecreatefrompng($img); break;
case "gif" : $im = @imagecreatefromgif($img); break;
default : $im = false; break;
}
header('Content-type: image/' . $type);
if (!$im) {
// just return the contents of the actual image.
readfile ($img);
} else {
// Create the resized image destination
$thumb = @imagecreatetruecolor ($w, $h);
//preserve alpha opacity - thanks to Martin Schmidt
$colorcount = @imagecolorstotal($im);
@imagetruecolortopalette($thumb,true,$colorcount);
@imagepalettecopy($thumb,$im);
$transparentcolor = @imagecolortransparent($im);
@imagefill($thumb,0,0,$transparentcolor);
@imagecolortransparent($thumb,$transparentcolor);
// Copy from image source, resize it, and paste to image destination
@imagecopyresampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh);
// Output resized image
switch ($ext){
case "wbmp" : @imagewbmp ($thumb); break;
case "png" : @imagepng ($thumb); break;
case "gif" : @imagegif ($thumb); break;
default : @imagejpeg ($thumb); break;
}
imagedestroy($thumb);
imagedestroy($im);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment