Image Resize Cache

Hello,

I am trying to use this code:
https://gist.github.com/davejamesmiller/3236415/449d296ac6393578d192174cb98cec0ac03f3843

And I am getting the errors:
Invalid filename: te-st.jpg
http://69.64.82.8/admin/uploads/resize.php?size=main_image&file=te-st.jpg

And:
File doesn't exist
http://69.64.82.8/admin/uploads/resize.php?size=main_image&file=test.jpg

But both files exist:
http://69.64.82.8/admin/uploads/test.jpg
http://69.64.82.8/admin/uploads/te-st.jpg

Any idea what is going on?

<?php
// Resize images on-the-fly and then cache them
// Very similar to the imagecache module in Drupal
// by Dave Miller
 
chdir(dirname(__FILE__));
 
$size = $_GET['size'];
$file = $_GET['file'];
 
$original = "$file";
$target = "$size/$file";
 
// Check the size is valid
switch ($size) {
case 'main_image':
$thumbWidth = 792;
$thumbHeight = null;
break;
 
case 'secondary_image':
$thumbWidth = 375;
$thumbHeight = null;
break;
 
default:
die('Invalid image size');
}

// Check the filename is safe & check file type
if (preg_match('#^[a-z0-9\-\.]+(@2x)?\.(jpg|jpeg|png)$#i', $file, $matches) && strpos($file, '..') === false) {
$retina = $matches[1];
$extension = $matches[2];
} else {
die("Invalid filename: $file");
}

// Double the size for retina devices
if ($retina) {
if ($thumbWidth) $thumbWidth *= 2;
if ($thumbHeight) $thumbHeight *= 2;
$original = str_replace('@2x', '', $original);
}

// Check the original file exists
if (!is_file($original)) {
die('File doesn\'t exist');
}
 
// Make sure the directory exists
if (!is_dir($size)) {
mkdir($size);
if (!is_dir($size)) {
die('Cannot create directory');
}
chmod($size, 0777);
}
 
// Make sure the file doesn't exist already
if (!file_exists($target)) {
 
// Make sure we have enough memory
ini_set('memory_limit', 128*1024*1024);
 
// Get the current size & file type
list($width, $height, $type) = getimagesize($original);
 
// Load the image
switch ($type) {
case IMAGETYPE_GIF:
$image = imagecreatefromgif($original);
break;
 
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($original);
break;
 
case IMAGETYPE_PNG:
$image = imagecreatefrompng($original);
break;
 
default:
die("Invalid image type (#{$type} = " . image_type_to_extension($type) . ")");
}
 
// Calculate height automatically if not given
if ($thumbHeight === null) {
$thumbHeight = round($height * $thumbWidth / $width);
}
 
// Ratio to resize by
$widthProportion = $thumbWidth / $width;
$heightProportion = $thumbHeight / $height;
$proportion = max($widthProportion, $heightProportion);
 
// Area of original image that will be used
$origWidth = floor($thumbWidth / $proportion);
$origHeight = floor($thumbHeight / $proportion);
 
// Co-ordinates of original image to use
$x1 = floor($width - $origWidth) / 2;
$y1 = floor($height - $origHeight) / 2;
 
// Resize the image
$thumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($thumbImage, $image, 0, 0, $x1, $y1, $thumbWidth, $thumbHeight, $origWidth, $origHeight);
 
// Save the new image
switch ($type)
{
case IMAGETYPE_GIF:
imagegif($thumbImage, $target);
break;
 
case IMAGETYPE_JPEG:
imagejpeg($thumbImage, $target, 90);
break;
 
case IMAGETYPE_PNG:
imagepng($thumbImage, $target);
break;
 
default:
throw new LogicException;
}
 
// Make sure it's writable
chmod($target, 0666);
 
// Close the files
imagedestroy($image);
imagedestroy($thumbImage);
}
 
// Send the file header
$data = getimagesize($original);
if (!$data) {
die("Cannot get mime type");
} else {
header('Content-Type: ' . $data['mime']);
}
 
// Send the file to the browser
readfile($target);

Open in new window

LVL 1
movieprodwAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Going forward you might want to look askance at any code that uses die() when it should instead trigger_error().  In the instant case the code is probably bad because the REGEX is probably bad.  But there are many other things that would steer me away from this code.  Failure to indent the control structures is a fairly strong signal of amateurish code.  

If you can give us a plain-language specification for how you want to resize the images I'll be glad to give you some code that works.  It may take a little back-and-forth dialog to clarify all of the requirements, but it's not rocket science and should not be much work to get it right.
0
movieprodwAuthor Commented:
Hello Ray,

I was just looking for a way to resize and cache the images on the fly.

Matt
0
Ray PaseurCommented:
The word "cache" is a term of art and it's not something you do with images on a server; they may get cached in the browser after they are sent as part of a web page.  If you have a different meaning for cache and want to tell us about it, we can probably help.  If you want to generate a collection of thumbnail images and save them in an image library, that would make sense to me.  The imagePNG() function can write an image file into a directory on the server just as easily as it can send the image file to the client browser.  You just have to give it a file path as the second argument to the function.

This is my teaching example showing how to resize an image.  The comments show how to call it.

Original image here:
http://www.iconoun.com/demo/images/image_600x374.png

Example of script in action here:
http://iconoun.com/demo/image_thumbnail.php?i=images/image_600x374.jpg&w=300&h=187

<?php // demo/image_thumbnail.php
error_reporting(E_ALL);


/**
 * RESIZE AN IMAGE TO FIT INSIDE A DEFINED TRANSPARENT SPACE
 * USE CASE: <img src="image_thumbnail.php?i=your.jpg&w=150&h=100" />
 */


// ACQUIRE THE ARGUMENTS - MAY WANT SOME SANITY TESTS?
$thumb_w   = !empty($_GET["w"]) ? $_GET['w'] : 600;
$thumb_h   = !empty($_GET["h"]) ? $_GET['h'] : 400;
$image_url = !empty($_GET["i"]) ? $_GET['i'] : trigger_error("Missing GET argument 'i' for Image URL", E_USER_ERROR);;

// CREATE THE THUMBNAIL IMAGE RESOURCE AND FILL IN TRANSPARENT
$thumb = imagecreatetruecolor($thumb_w, $thumb_h);
imagesavealpha($thumb, TRUE);
$empty = imagecolorallocatealpha($thumb,0x00,0x00,0x00,127);
imagefill($thumb, 0, 0, $empty);

// GET ORIGINAL IMAGE DIMENSIONS
$array = getimagesize($image_url);
if ($array)
{
    list($image_w, $image_h) = $array;
}
else
{
    trigger_error("NO IMAGE $image_url", E_USER_ERROR);
}

// ACQUIRE THE ORIGINAL IMAGE
$image_ext = explode('.', $image_url);
$image_ext = end($image_ext);
$image_ext = trim(strtoupper($image_ext));
switch($image_ext)
{
    case 'JPG' :
    case 'JPEG' :
        $image = imagecreatefromjpeg($image_url);
        break;

    case 'PNG' :
        $image = imagecreatefrompng($image_url);
        break;

    default : trigger_error("UNKNOWN IMAGE TYPE: $image_url", E_USER_ERROR);
}

// GET THE LESSER OF THE RATIO OF THUMBNAIL H OR W DIMENSIONS
$ratio_w = ($thumb_w / $image_w);
$ratio_h = ($thumb_h / $image_h);
$ratio   = ($ratio_w < $ratio_h) ? $ratio_w : $ratio_h;

// COMPUTE THUMBNAIL IMAGE DIMENSIONS
$thumb_w_resize = $image_w * $ratio;
$thumb_h_resize = $image_h * $ratio;

// COMPUTE THUMBNAIL IMAGE CENTERING OFFSETS
$thumb_w_offset = ($thumb_w - $thumb_w_resize) / 2.0;
$thumb_h_offset = ($thumb_h - $thumb_h_resize) / 2.0;

// COPY THE IMAGE TO THE CENTER OF THE THUMBNAIL
imagecopyresampled
( $thumb
, $image
, $thumb_w_offset
, $thumb_h_offset
, 0
, 0
, $thumb_w_resize
, $thumb_h_resize
, $image_w
, $image_h
)
;

// SHOW THE NEW THUMB IMAGE
header('Content-type: image/png');
imagepng($thumb);

// RELEASE THE MEMORY USED BY THE RESOURCES
imagedestroy($thumb);
imagedestroy($image);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

movieprodwAuthor Commented:
This looks great.

So from what I can break down
- Check if the image exists first
-- If exists then display, Stop
- If does not exist then create file
- Save file
- Display file

Kind of surprised this is not a common script, what are the drawbacks?
0
Ray PaseurCommented:
I don't see any drawbacks at all and It's certainly not uncommon.  It's just that this is the sort of thing that needs a little customization for whatever web site or framework is in play.  It sounds to me like you understand the principles and can apply them.
0
movieprodwAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.