Skip to content

Instantly share code, notes, and snippets.

@kfriend
Last active August 11, 2016 00:20
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 kfriend/6706218 to your computer and use it in GitHub Desktop.
Save kfriend/6706218 to your computer and use it in GitHub Desktop.
PHP: Quick and dirty JIT image resizing (i.e. thumbnails) with built-in caching
<?php
$storagePath = './img';
$cachePath = $storagePath.'/cache';
// Maximum requestable dimensions
$maxWidth = 1000;
$maxHeight = 1000;
$forceJpeg = true;
$defaultQuality = 100;
// Valid image types
$imageTypes = array(
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
);
$upscale = !empty($_GET['upscale']);
$quality = !empty($_GET['quality']) ? ltrim(preg_replace('/[^\d]/', '', $_GET['quality']), '0') : $defaultQuality;
$width = !empty($_GET['width']) ? ltrim(preg_replace('/[^\d]/', '', $_GET['width']), '0') : $maxWidth;
$height = !empty($_GET['height']) ? ltrim(preg_replace('/[^\d]/', '', $_GET['height']), '0') : $maxHeight;
$imageName = !empty($_GET['image']) ? pathinfo($_GET['image'], PATHINFO_BASENAME) : '';
$extension = strtolower(pathinfo($imageName, PATHINFO_EXTENSION));
$imagePath = $storagePath.'/'.$imageName;
$quality = min($quality, 100);
if (
!$width or
!$height or
!$imageName or
!file_exists($imagePath) or
!in_array($extension, array_keys($imageTypes))
)
{
error_404();
}
$mime = $imageTypes[$extension];
$imagePath = $storagePath.'/'.$imageName;
if (!file_get_contents($imagePath)) error_404();
$funcSlug = ($extension == 'jpg') ? 'jpeg' : $extension;
$image = @call_user_func('imagecreatefrom'.$funcSlug, $imagePath);
if (!$image) error_404();
$currentWidth = imagesx($image);
$currentHeight = imagesy($image);
if (($currentWidth / $currentHeight) >= 1)
{
$scale = min($currentWidth, $width, $maxWidth) / $currentWidth;
}
else
{
$scale = min($currentHeight, $height, $maxHeight) / $currentHeight;
}
$width = $width * $scale;
$height = $height * $scale;
$hashedName = md5("{$imageName}@{$width}x{$height}");
$hashedPath = $cachePath.'/'.$hashedName;
if (file_exists($hashedPath))
{
return_image($hashedPath, $mime);
}
// Don't want to scale images up unless requested
if ($scale >= 1 and !$upscale)
{
return_image($imagePath, $mime);
}
$newWidth = $currentWidth * $scale;
$newHeight = $currentHeight * $scale;
$newImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled(
$newImage,
$image,
0, 0, 0, 0,
$newWidth,
$newHeight,
$currentWidth,
$currentHeight
);
imagedestroy($image);
if (!$newImage) error_404();
if ($forceJpeg)
{
$funcSlug = $extension = 'jpeg';
$mime = $imageTypes[$extension];
}
call_user_func('image'.$funcSlug, $newImage, $hashedPath, $quality);
return_image($hashedPath, $mime);
function error_404()
{
header('HTTP/1.0 404 Not Found');
echo 'Error';
exit;
}
function return_image($image, $mime)
{
header('Content-type: ' . $mime);
echo file_get_contents($image);
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment