954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

PHP Thumbnailer

0
By Travis R on Oct 18th, 2011 5:40 am

What this script does is take in an image and export a thumbnailed version.


Function Arguments:
$file : Desired image to create a thumbnail of
$destination : End location for the thumbnail
$zoom : Setting type, explained later (default: false)
$t_width : Thumbnail width (default: 150)
$t_height : Thumbnail height (default: 150)


This script is capable of making 2 types of thumbnails:Cropped to fit (Small Preview)
Resized (Fit Width)

A sample of this script is here: http://turt2live.com/PHP_Classes/thumbnails/

If you would like the "Cropped to fit (Small Preview)" then set $zoom = true, otherwise, set it to false.

<?php
function create_thumbnail($file, $destination, $zoom = false, $t_width = 150, $t_height = 150){
	$image_source =			getimagesize($file);	
	$source_mime = 			$image_source['mime'];
	$source_x = 			$image_source[0];
	$source_y = 			$image_source[1];
	
	if($source_mime == "image/png"){
		$image = imagecreatefrompng($file);
	}else if($source_mime == "image/jpeg"){
		$image = imagecreatefromjpeg($file);
	}else if($source_mime == "image/gif"){
		$image = imagecreatefromgif($file);
	}
	
	//Zoom means "crop", else means "resize"
	if($zoom == true){
		$crop_x = 		floor($source_x / 2) - floor($t_width / 2);
		$crop_y = 		floor($source_y / 2) - floor($t_height / 2);
		
		if($crop_x < 0){
			$crop_x = 0;
		}
		if($crop_y < 0){
			$crop_y = 0;
		}
		if (($source_x - $crop_x) < $t_width){
			$t_width = $source_x - $crop_x;
		}
		if (($source_y - $crop_y) < $t_height){
			$t_height = $source_y - $crop_y;
		}
		
		$export_image = imagecreatetruecolor($t_width, $t_height);
		imagecopy($export_image, $image, 0, 0, $crop_x, $crop_y, $source_x, $source_y);
		imagepng($export_image, $destination);
	}else{
		$ratio = 			$t_width / $source_x;
		$t_height = 		$source_y * $ratio;
		$export_image = 	imagecreatetruecolor($t_width, $t_height);
		
		imagecopyresized($export_image, $image, 0, 0, 0, 0, $t_width, $t_height, $source_x, $source_y);
		imagepng($export_image, $destination);
	}
	
	return $destination;
}
?>

The code in the snippet is an example of how terrible the gd library is at the best of times when dealing with photos users have uploaded. That is why it is best to use the Imagick module whenever possible. It is faster, in oop and after a while becomes easier to use than gd. For example I have prepared the following script which loads a psd image which would be impossible in gd then resizes it, saves it to a file and displays the picture to the browser.

<?php
$file='images/banner.psd'; //yes this format is accepted
$width=96;
$height=72;

//load image
$thumb = new Imagick($file);

//resize image
$thumb->resizeImage($width,$height,Imagick::FILTER_LANCZOS,1);

//save image to file
$thumb->writeImage('mythumb.gif');

//convert image to jpeg
$thumb->setImageFormat('jpeg');
$thumb->setImageCompressionQuality(100);

//display image to browser
header("Content-type: image/jpeg");
echo $thumb->getImageBlob();

//remove from memory
$thumb->clear();
$thumb->destroy(); 

?>

Note that you can insert any image type that you like into the Imagick() parameter and it will accept it. No if statements, no mime types, just insert and go. So much more simpler.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

cwarn23, in your code I cannot see what variable image is equal t.
Could you please explain

Matthew N.
Junior Poster
101 posts since Jul 2010
Reputation Points: 10
Solved Threads: 10
 

Did you mean $image because check again, there is no variable image ;)

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

cwarn32, I appreciate your code, but it does not allow for a smaller "preview" of the image, do you know how you could use your suggestion to compensate for this?

turt2live
Junior Poster in Training
85 posts since Jan 2011
Reputation Points: 15
Solved Threads: 3
 
cwarn32, I appreciate your code, but it does not allow for a smaller "preview" of the image, do you know how you could use your suggestion to compensate for this?


My code resizes the image and displays it allowing for a smaller preview. So please describe what exactly it is your after as my code already fits that description.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

If you go to the sample link I provided on my code it gives you 2 thumbnails:

One that resizes and one that crops the image to a certain area.

Your code does the resize, but not the crop.

turt2live
Junior Poster in Training
85 posts since Jan 2011
Reputation Points: 15
Solved Threads: 3
 

well to crop you simply add the following code:

$thumb->cropImage($width,$height,$x,$y);
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

Alright, thank you :)

turt2live
Junior Poster in Training
85 posts since Jan 2011
Reputation Points: 15
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: