<?php // demo/EE_image_thumbnail.php
error_reporting(E_ALL);
// RESIZE AN IMAGE TO FIT INSIDE A DEFINED TRANSPARENT SPACE
// SAMPLE USE CASES:
// <img src="http://www.iconoun.com/demo/EE_image_thumbnail.php?w=300&h=187&img=images/image_600x374.jpg" />
// <img src="http://www.iconoun.com/demo/EE_image_thumbnail.php?w=150&h=300&img=images/image_600x374.png" />
// ACQUIRE THE URL ARGUMENTS - MAY NEED SOME SANITY TESTS?
$thumb_w = $_GET["w"];
$thumb_h = $_GET["h"];
$image_url = $_GET["img"];
// CREATE THE THUMBNAIL IMAGE RESOURCE
$thumb = imageCreateTrueColor($thumb_w, $thumb_h);
// FILL THE THUMBNAIL WITH TRANSPARENT
imageSaveAlpha($thumb, TRUE);
$empty = imageColorAllocateAlpha($thumb,0x00,0x00,0x00,127);
imageFill($thumb, 0, 0, $empty);
// TRY TO 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 EXTENSION
$ext = explode('.', $image_url);
$ext = end($ext);
$ext = strtoupper($ext);
$ext = trim($ext);
// USING THE EXTENSION, ACQUIRE THE IMAGE RESOURCE
switch($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 // DESTINATION IMAGE
, $image // SOURCE IMAGE
, $thumb_w_offset // DESTINATION X-OFFSET
, $thumb_h_offset // DESTINATION Y-OFFSET
, 0 // SOURCE X-OFFSET
, 0 // SOURCE Y-OFFSET
, $thumb_w_resize // DESTINATION WIDTH
, $thumb_h_resize // DESTINATION HEIGHT
, $image_w // SOURCE WIDTH
, $image_h // SOURCE HEIGHT
)
;
// SHARPEN THE THUMBNAIL SEE php.net/imageconvolution#104006
$sharpenMatrix = array
( array( -1.2, -1.0, -1.2 )
, array( -1.0, 20.0, -1.0 )
, array( -1.2, -1.0, -1.2 )
)
;
$divisor = array_sum(array_map('array_sum', $sharpenMatrix));
$offset = 0;
imageConvolution($thumb, $sharpenMatrix, $divisor, $offset);
// SAVE THE THUMBNAIL IMAGE IN A "THUMBS" DIRECTORY
$bname = basename($image_url);
imagePNG($thumb, "thumbs/$bname");
// SHOW THE NEW THUMB IMAGE
header('Content-type: image/png');
imagePNG($thumb);
// RELEASE THE MEMORY USED BY THE IMAGE RESOURCES
imageDestroy($thumb);
imageDestroy($image);
Summary
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (3)
Commented:
This is an excellent and very useful script. Nicely documented...thank you. Are the functions used above part of a "standard" PHP deployment on the bigger shared hosts like GoDaddy and Dreamhost?
Author
Commented:Commented:
Another great Article!
Thank you for putting it together.
Vic
(Yes vote above.)