Solved

Image Resize Cache

Posted on 2014-04-19
6
241 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
Hello Ray,

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

Matt
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:movieprodw
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
Thank you
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now