Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

create thumbnails

Posted on 2014-03-14
12
Medium Priority
?
553 Views
Last Modified: 2014-03-15
Hi,

I am trying to create thumbnails, It worked perfectly in my previous version : 5.3

Now the php is upgraded to 5.4

Script is creating images for png or gif, but coming to Jpeg it is throwing error.

For you reference Link :

Working
https://www.culturalresourcenetwork.com/members/showpic.php?img=uploadedimages/consultant/4067958.png&mh=70&mw=120

Not working
https://www.culturalresourcenetwork.com/members/showpic.php?img=uploadedimages/gallery/2192_EADFFKZBNM_1.jpg&mh=75&mw=110

PhpInfo

http://www.culturalresourcenetwork.com/phpinfo.php

Can you please let me know the Issue with Jpeg Image ::

Image Mode is RGB only .. Working fine .. in php 5.3

For Your reference attaching File also.

Ed. Note: Code moved to Code snippet, here:
<?php
// This is "On the Fly Thumbnailer with Caching Option" by Farhan.
// Based on Marcello Colaruotolo (1.5.1) which builds upon Nathan Welch (1.5)
// and Roberto Ghizzi. With improvements by @Quest WebDesign, http://atQuest.nl/
// and Martijn Loeffen (browser-caching and thumbnail-cache-dir)
//
// Scales product images dynamically, resulting in smaller file sizes, and keeps
// proper image ratio.
//
// Used in conjunction with modified tep_image in html_output.php (see: readme.txt).
//
// CONFIGURATION SETTINGS
//
// Server cache directory. Set the value below to true to generate resampled thumbnails
// resulting in smoother-looking images. Not supported in GD ver. < 2.01
$use_resampling = true;
//
// Create True Color Thumbnails? Better quality overall but set to false if you
// have GD version < 2.01 or if creating transparent thumbnails.
$use_truecolor = true;
//
// Output GIFs as JPEGS? Set this option to true if you have GD version > 1.6
// and want to output GIF thumbnails as JPGs instead of GIFs or PNGs. Note that your
// GIF transparencies will not be retained in the thumbnail if you output them
// as JPGs. If you have GD Library < 1.6 with GIF create support, GIFs will
// be output as GIFs. Set the "matte" color below if setting this option to true.
$gif_as_jpeg = false;
//
// Cache Images on the server? Set to true if you want to save requested thumbnails
// on disk. This will add to disk space but will save your processor from having to
// create the thumbnail for every visitor.
$tn_server_cache = false;
//
// Thumbnail Path. If server-caching is enabled, specify a sub-directory
// where the thumbnails should be kept. Use '' for the default images-directory,
// which is /catalog/images/
// Note: Make sure this path actually exists as a subdirectory and is writeable!
$tn_path = ''; // The default is 'thumbnails/', should be chmod 777
//
// Cache Images in Browser-Cache? Set to true if you want browsers to be able to
// cache viewed thumbnails in their own cache. This will save bandwidth for every
// visitor that views the same thumbnail again.
$tn_browser_cache = true; // The default is true
//
// Send a 404 http response when an image is not found
// If set to false, will show a small error-image (as in version < 2.0.0)
$use404 = true; // The default is true
//
// Define RGB Color Value for background matte color if outputting GIFs as JPEGs
// Example: white is r=255, b=255, g=255; black is r=0, b=0, g=0; red is r=255, b=0, g=0;
$r = 255; // Red color value (0-255)
$g = 255; // Green color value (0-255)
$b = 255; // Blue color value (0-255)
//
// Allow the creation of thumbnail images that are larger than the original images:
$allow_larger = false; // The default is false.
// If allow_larger is set to false, you can opt to output the original image:
// Better leave it true if you want pixel_trans_* to work as expected
$show_original = true; // The default is true.
//
// END CONFIGURATION SETTINGS

// Note: In order to manually debug this script, you might want to comment
// some header() lines -- otherwise no output is shown.

// reverse strrchr(), taken from http://nl2.php.net/manual/en/function.strrchr.php
 	$img_src="../".trim($_GET["img"]);
	if(file_exists($img_src))
	{
	
	$current_size = getimagesize($img_src); 
	$current_img_width = $current_size[0]; 
	$current_img_height = $current_size[1]; 
	
      
         // Proportionally resize the image to the
         // max sizes specified above
    	  $max_width=$_GET["mw"];
		 $max_height=$_GET["mh"];
		 $width=$current_img_width;
		 $height=$current_img_height;
         $x_ratio = $max_width / $width;
         $y_ratio = $max_height / $height;
         if( ($width <= $max_width) && ($height <= $max_height) )
         {
               $tn_width = $width;
               $tn_height = $height;
         }
         elseif (($x_ratio * $height) < $max_height)
         {
               $tn_height = ceil($x_ratio * $height);
               $tn_width = $max_width;
         }
         else
         {
               $tn_width = ceil($y_ratio * $width);
               $tn_height = $max_height;
         }
	$new_img_width = $tn_width; 
    $new_img_height =$tn_height;
    $_GET["w"]=$new_img_width;
    $_GET["h"]=$new_img_height;
    
	}
function reverse_strrchr($haystack, $needle)
{
	return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
} 

function modify_tn_path($file)
{
	global $tn_path;
	
	if ($tn_path=='') return $file;
	else{
		// append the thumbnail-path to the path
		$pathSep = strstr(PHP_OS, "WIN") ? "\\" : "/";;
		$path = reverse_strrchr($file,$pathSep);
		if ($path===false) return $tn_path . $file;
		else return str_replace($path,$path . $tn_path,$file);
	}
}

function http_headers($file='')
{
	//
	// This function supports the use of browser-caching (optional)
	//
	// A 304 (Not Modified) will be sent when the thumbnail has not changed
	//       since the time it was last cached by the client
	// A 200 (OK) will be sent when the thumbnail was not cached by the client
	//       or when the thumbnail was cached but changed afterwards
	// A 404 (Not Found) will be sent when the thumbnail is not found (optional)
	global $use404, $tn_browser_cache;
	
	if (isset($_SERVER["SERVER_PROTOCOL"]) && $_SERVER["SERVER_PROTOCOL"] == "HTTP/1.1") 
		$httpProtocol = "HTTP/1.1";
	else
		$httpProtocol = "HTTP/1.0";
	
	if (file_exists($file))
	{
		
		if (isset ($_SERVER["HTTP_CACHE_CONTROL"])) {
			$tn_browser_cache = strtolower($_SERVER["HTTP_CACHE_CONTROL"]) == "no-cache" ? false : $tn_browser_cache ;
		}
		
		//Build our entity tag, which is "inode-lastmodtime-filesize"
		$lastModified = filemtime($file);
		$lastModifiedGMT = $lastModified - date('Z');
		$lastModifiedHttpFormat = gmstrftime("%a, %d %b %Y %T %Z", $lastModified);
		// Don't use inode in eTag when you have multiple webservers, instead I use a dummy value (1fa44b7)
		$eTag = '"1fa44b7-' . dechex(filesize($file)) . "-" . dechex($lastModifiedGMT) . '"';
	
		if ($tn_browser_cache){
		
			$lastModifiedFromHttp = "xxx";
			if (isset ($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
				$lastModifiedFromHttp = ($_SERVER["HTTP_IF_MODIFIED_SINCE"] === "") ? "xxx" : $_SERVER["HTTP_IF_MODIFIED_SINCE"] ;
			}
			
			// Read sent eTag by browser
			$foundETag = "";
			if (isset ($_SERVER["HTTP_IF_NONE_MATCH"])) {
				$foundETag = stripslashes($_SERVER["HTTP_IF_NONE_MATCH"]);
			}
			
			// Last Modification Time
			if ($lastModifiedFromHttp == $lastModifiedHttpFormat) {
				$sameLastModified = true;
			}
			elseif (strpos($lastModifiedFromHttp,$lastModifiedHttpFormat) !== false){
				$sameLastModified = true;
			}
			else {
				$sameLastModified = false;
			}
			
			if (($eTag == $foundETag) && $sameLastModified){
				// same eTag and Last Modification Time (e.g. with Firefox)
				$is304 = true;
			}
			else
				// no eTag supplied, but Last Modification Time is unchanged (e.g. with IE 6.0)
				$is304 = (($foundETag == "") && $sameLastModified);

			if ($is304)
			{
				//
				// They already have an up to date copy so tell them
				if ($lastModifiedGMT > 946080000) {        // 946080000 = Dec 24, 1999 4PM
					// only send if valid eTag
					header("ETag: " . $eTag);
				}
				header("Status: 304 Not Modified");
				header($httpProtocol . " 304 Not Modified");
				header("Connection: close");
				exit();
			}
		}

		//
		// We have to send them the whole page
		header('Pragma: ');
		header('Expires: ');
		if ($tn_browser_cache){
			if ($lastModifiedGMT > 946080000) {        // 946080000 = Dec 24, 1999 4PM
				header('ETag: ' . $eTag);
			}
			header('Last-Modified: ' . $lastModifiedHttpFormat);
			header('Cache-Control: private');
		}
		else {
			header('Cache-Control: no-cache');
		}

	}
	else
	{
		if ($use404)
		{
			//
			// send them a 404 http response header
			header("TEST404: TEST404");
			header("Status: 404 Not Found");
			header($httpProtocol . " 404 Not Found");
			exit();
		}
		else
		{
			//
			// show a custom error-image (non-cacheable by the browser)			
			header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");								// Date in the past
			header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");	// Always modified
			header("Cache-Control: no-store, no-cache, must-revalidate");		// HTTP/1.1
			header("Cache-Control: post-check=0, pre-check=0", false);			// HTTP/1.1
			header("Pragma: no-cache");																			// HTTP/1.0
			header('Content-type: image/jpeg');
			$src = imagecreate($_GET['w'], $_GET['h']);											// Create a blank image.
			$bgc = imagecolorallocate($src, 255, 255, 255);
			$tc  = imagecolorallocate($src, 0, 0, 0);
			imagefilledrectangle($src, 0, 0, $_GET['w'], $_GET['h'], $bgc);
			imagestring($src, 1, 5, 5, 'Error', $tc);
			imagejpeg($src, '', 100);
			imagedestroy($src);
			exit();
		}
	}
}

// Get the size of the image:
$image = @getimagesize($img_src);

// Check the input variables and decide what to do:
if (empty($image) || empty($_GET['w']) || empty($_GET['h']) || (empty($allow_larger) && ($_GET['w'] > $image[0] || $_GET['h'] > $image[1])))
{
	if (empty($image) || empty($show_original))
	{
		http_headers();
	}
	else
	{
		// 2Do: Return the original image w/o making a copy (as that is what we currently do):
		$_GET['w'] = $image[0];
		$_GET['h'] = $image[1];
	}
}

// Create appropriate image header:
if ($image[2] == 2 || ($image[2] == 1 && $gif_as_jpeg))
{
	header('Content-type: image/jpeg');
	if ($tn_server_cache) $filename = modify_tn_path($img_src .'.thumb_'.$_GET['w'].'x'.$_GET['h'].'.jpg');
}
elseif ($image[2] == 1 && function_exists('imagegif'))
{
	header('Content-type: image/gif');
	if ($tn_server_cache) $filename = modify_tn_path($img_src .'.thumb_'.$_GET['w'].'x'.$_GET['h'].'.gif');
}
elseif ($image[2] == 3 || $image[2] == 1)
{
	header('Content-type: image/png');
	if ($tn_server_cache) $filename = modify_tn_path(img_src .'.thumb_'.$_GET['w'].'x'.$_GET['h'].'.png');
}

// If you are required to set the full path for file_exists(), set this:
// $filename = '/your/path/to/catalog/'.$filename;

if (file_exists($filename) && $tn_server_cache && filemtime($filename) > filemtime($img_src))
{
	// Output Cache Headers
	http_headers($filename);
	
	if ($image[2] == 2 || ($image[2] == 1 && $gif_as_jpeg))
	{
		$src = imagecreatefromjpeg($filename);
		imagejpeg($src, '', 100);
	}
	elseif ($image[2] == 1 && function_exists('imagegif'))
	{
		$src = imagecreatefromgif($filename);
		imagegif($src);
	}
	elseif ($image[2] == 3 || $image[2] == 1)
	{
		$src = imagecreatefrompng($filename);
		imagepng($src);
	}
	else
	{
		// Not an image or imagecreatefrom...-function does not exits.
		// Let's output an error
		http_headers();
	}
}
else
{
	// Create a new, empty image based on settings:
	if (function_exists('imagecreatetruecolor') && $use_truecolor && ($image[2] == 2 || $image[2] == 3))
	{
		$tmp_img = imagecreatetruecolor($_GET['w'],$_GET['h']);
	}
	else
	{
		$tmp_img = imagecreate($_GET['w'],$_GET['h']);
	}

	$th_bg_color = imagecolorallocate($tmp_img, $r, $g, $b);

	imagefill($tmp_img, 0, 0, $th_bg_color);
	imagecolortransparent($tmp_img, $th_bg_color);

	// Create the image to be scaled:
	if ($image[2] == 2 && function_exists('imagecreatefromjpeg'))
	{
		$src = imagecreatefromjpeg($img_src);
	}
	elseif ($image[2] == 1 && function_exists('imagecreatefromgif'))
	{
		$src = imagecreatefromgif($img_src);
	}
	elseif (($image[2] == 3 || $image[2] == 1) && function_exists('imagecreatefrompng'))
	{
		$src = imagecreatefrompng($img_src);
	}
	else
	{
		// Not an image or imagecreatefrom...-function does not exits.
		// Let's output an error
		http_headers();
	}

	// Scale the image based on settings:
	if (function_exists('imagecopyresampled') && $use_resampling)
	{
		imagecopyresampled($tmp_img, $src, 0, 0, 0, 0, $_GET['w'], $_GET['h'], $image[0], $image[1]);
	}
	else
	{
		imagecopyresized($tmp_img, $src, 0, 0, 0, 0, $_GET['w'], $_GET['h'], $image[0], $image[1]);
	}

	// Output the image:
	if ($image[2] == 2 || ($image[2] == 1 && $gif_as_jpeg))
	{
		if ($tn_server_cache)
		{
			$thumbnail = modify_tn_path($img_src.'.thumb_'.$_GET['w'].'x'.$_GET['h'].'.jpg');
			imagejpeg($tmp_img,$thumbnail, 100);
			http_headers($thumbnail);
		}
		else
		{
			http_headers($img_src);
		}
		imagejpeg($tmp_img, '', 100);
	}
	elseif ($image[2] == 1 && function_exists('imagegif'))
	{
		if ($tn_server_cache)
		{
			$thumbnail = modify_tn_path($img_src.'.thumb_'.$_GET['w'].'x'.$_GET['h'].'.gif');
			imagegif($tmp_img,$thumbnail);
			http_headers($thumbnail);
		}
		else
		{
			http_headers($img_src);
		}
		imagegif($tmp_img);
	}
	elseif ($image[2] == 3 || $image[2] == 1)
	{
		if ($tn_server_cache)
		{
			$thumbnail = modify_tn_path($img_src.'.thumb_'.$_GET['w'].'x'.$_GET['h'].'.png');
			imagepng($tmp_img,$thumbnail);
			http_headers($thumbnail);
		}
		else
		{
			http_headers($img_src);
		}
		imagepng($tmp_img);
	}
	else
	{
		// Not an image or image...-function not supported
		// Let's output an error:
		http_headers();
	}

	// Clear the image from memory:
	imagedestroy($src);
	imagedestroy($tmp_img);
}

?>

Open in new window

showpic.php
0
Comment
Question by:Starquest321
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39928604
Php 5.4 has some big changes compared to 5.3

Turn on error display (error_reporting(E_ALL);) and post here the errors shown.

Thanks,
Dan
0
 

Author Comment

by:Starquest321
ID: 39928656
Hi Dan,

I tried that way, But i don't see any errors.

Add this line in top of that php page for your reference ::

error_reporting(0);

Thanks
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39928659
error_reporting(0); means "do not show any error" :)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Starquest321
ID: 39928663
Hey Dan,

Sorry it's My mistake ::

Updated as :: error_reporting(E_ALL);

Thanks
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39928866
According to my tests, none of the images linked above are working!  How did you determine that some work and some do not?  Maybe post a screen shot?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39928878
This article is kind of old, but maybe it will help with an understanding of the principles.  I'll test it with PHP5.4+ and let you know if the code still works correctly.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_6554-Use-PHP-to-Make-an-Image-Thumbnail.html
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39928913
Tested and working at PHP 5.4+
http://www.iconoun.com/demo/EE_image_thumbnail.php?w=300&h=187&img=images/image_600x374.png

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

A little theory... As a practical matter, JPEG and PNG images are the most likely input formats.  You can add GIF support if you want, but it seems to have fallen into disuse over the last decade (the palette was too small to render photographs correctly).  On the output side, PNG is all you need.  It used to be good to produce JPEG outputs because they were compressed into smaller files, but with the smaller size came a loss of image information.  Today, with faster internet connections, the PNG images work just fine.

If you want to write the thumbnails into a directory, you can modify the imagePNG() function call on line 94.

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


// RESIZE AN IMAGE TO FIT INSIDE A DEFINED TRANSPARENT SPACE
// USE CASE: <img src="http://www.iconoun.com/demo/EE_image_thumbnail.php?w=300&h=187&img=images/image_600x374.jpg" />


// 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(strtoupper($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
)
;
// SHARPEN THE THUMBNAIL
$sharpenMatrix = array
( array( -1.2, -1, -1.2 )
, array( -1,   20, -1 )
, array( -1.2, -1, -1.2 )
)
;
$divisor = array_sum(array_map('array_sum', $sharpenMatrix));
$offset  = 0;
imageconvolution($thumb, $sharpenMatrix, $divisor, $offset);

// 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
 

Author Comment

by:Starquest321
ID: 39928997
Hi Ray,

Thanks for your reply.

Check the below two links ::

https://www.culturalresourcenetwork.com/members/showpic.php?img=uploadedimages/consultant/4067958.png&mh=70&mw=120

https://www.culturalresourcenetwork.com/members/showpic.php?img=uploadedimages/gallery/2192_EADFFKZBNM_1.jpg&mh=75&mw=110




One is displaying Image .. That is PNG

Other one is Jpg not displaying throwing error.

Why this behavior.

Can you please cross check Phpinfo() .. Anything i am missing in that.

http://culturalresourcenetwork.com/phpinfo.php

Thanks
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39929024
We have the same GD library.  I see that your server does not have a default timezone set.  This can cause spurious messages.

Can you please post a fully qualified URL link to each of the images you're using for input?  Thanks.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39929044
Actually, I found the image.  
https://www.culturalresourcenetwork.com/uploadedimages/gallery/2192_EADFFKZBNM.jpg

It's a very large image!  But I can still process it with this script.  You may want to add some error checking (checking the return values from PHP functions) to the old script and see where the hiccup is occurring.

Please see:
http://www.iconoun.com/demo/temp_starquest321.php?w=400&h=400

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


// RESIZE AN IMAGE TO FIT INSIDE A DEFINED TRANSPARENT SPACE


// ACQUIRE THE URL ARGUMENTS - MAY NEED SOME SANITY TESTS?
$thumb_w   = $_GET["w"];
$thumb_h   = $_GET["h"];

// HARDWIRED IMAGE POINTER TO LARGE IMAGE
// $image_url = $_GET["img"];
$image_url = 'https://www.culturalresourcenetwork.com/uploadedimages/gallery/2192_EADFFKZBNM.jpg';

// 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);

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39931290
Please read the grading guidelines and then post back here to explain the marked-down grade, thanks.
http://support.experts-exchange.com/customer/portal/articles/481419
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

610 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