Solved

Image Resize Cache

Posted on 2014-04-19
6
245 Views
Last Modified: 2014-04-28
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

0
Comment
Question by:movieprodw
  • 3
  • 3
6 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40010737
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
 
LVL 1

Author Comment

by:movieprodw
ID: 40010833
Hello Ray,

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

Matt
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40011289
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:movieprodw
ID: 40023232
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40023760
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
 
LVL 1

Author Closing Comment

by:movieprodw
ID: 40028020
Thank you
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question