Skip to content

Instantly share code, notes, and snippets.

@Stanton
Created September 7, 2011 14:58
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 Stanton/1200794 to your computer and use it in GitHub Desktop.
Save Stanton/1200794 to your computer and use it in GitHub Desktop.
image.php
<?php
/**
* image Generator -- see trac ticket #778 and #795
*
* Resizes an image
*
* For proper documentation see the phpdoc for the imageGenerator function.
*/
// If there isn't a directory for cached images, make one.
$cache_dir = '../../../var/cache/images';
if (!is_dir($cache_dir)) {
mkdir($cache_dir, 0777, true);
}
$dst_w = (int)$_GET['w'];
$dst_h = (int)$_GET['h'];
$crop = (float)$_GET['crop'];
$stretch = (bool)$_GET['stretch'];
$filename = '../../images/' . basename($_GET['filename']);
if (!file_exists($filename)) {
$filename = '../../images/1col.png';
}
// Find the current width and height of the image, also image_type for later.
list($width, $height, $image_type) = getimagesize($filename);
header("Content-type: " . image_type_to_mime_type($image_type));
$cached_filename = "{$cache_dir}/{$dst_w}-{$dst_h}-{$crop}-{$stretch}-" . basename($filename);
// DISABLE IMAGE CACHING DURING TESTING
// If the cached file already exists, spit it out...
if (file_exists($cached_filename)) {
readfile($cached_filename);
exit;
}
// So here is where we actually generate the image!
$generated_image = imagecreatetruecolor($dst_w, $dst_h);
switch ($image_type) {
// If it's an image type we can't handle, it will end up being a blank rectangle
case IMAGETYPE_PNG:
$source_image = imagecreatefrompng($filename);
$save_function_name = 'imagepng';
//// I know it's a bit of a hack but it's quicker to code a variable function than running another switch
break;
case IMAGETYPE_GIF:
$source_image = imagecreatefromgif($filename);
$save_function_name = 'imagegif';
break;
case IMAGETYPE_JPEG:
$source_image = imagecreatefromjpeg($filename);
$save_function_name = 'imagejpeg';
break;
}
// Work out rect co-ordinates to copy from source image into our destination image:
// The destination rect coordinates will ALWAYS be (0, 0, $dst_w, $dst_h)
if($stretch) {
// stretches the image to fit. No Sigismund mad maths required (as ane fule kno.)
$src_x = 0;
$src_y = 0;
$src_w = $width;
$src_h = $height;
}
else {
// Typically, I've left the toughest bit til last...
$x_factor = $width / $dst_w; // x-axis scaling factor
$y_factor = $height / $dst_h; // y-axis scaling factor
if ($x_factor > $y_factor) { // destination image is narrower than source; crop horizontally
// Because we're cropping horizontally, we need the entire vertical range.
$src_y = 0;
$src_h = $height;
// How wide do we want?
$src_w = $dst_w * $y_factor; // I think?
// Then the crop parameter:
$src_x = ($width - $src_w) * 0.5 * ($crop + 1);
}
else {
// the same code except swapping x for y and w for h.
$src_x = 0;
$src_w = $width;
$src_h = $dst_h * $x_factor;
$src_y = ($height - $src_h) * 0.5 * ($crop + 1);
}
}
// resize whole image, no matter what for first run...
imagecopyresampled($generated_image, $source_image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
$save_function_name($generated_image, $cached_filename);
imagedestroy($generated_image);
imagedestroy($source_image);
readfile($cached_filename);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment