DevSnips.com Code Snippet Repository
search:    


Navigation
  Home
About
Library
Contact
 
Snippet Library
  ColdFusion   338  
  ASP   201  
  PHP   101  
  HTML   11  
  JavaScript   77  
  XML   2  
  CSS   5  
  SQL   13  
  JSP   2  
  C#   1  
  ASP.NET   0  
  Submit a Code Snippet
 
Blog Archive
  September 2007
August 2007
July 2007
June 2007
May 2007
November 2006
October 2006
Search Archives
 
Random Affiliates
  ReviewMe!
PHP Arch
Uno-Code
BioMetric Base

Want to become an affiliate?
Read more...


Privacy Policy
© 2010

Blog Archive

 
PHP and GD for generating and cropping avatars

Since I'm still on a major GD kick, I wanted to talk about a new trick that I'm using to generate avatars on the fly. This technique uses a single file, crops that image to be centered (left/right) and outputs a square avatar at set dimensions. Since this is using GD, we're actually creating the image and not simply resizing it via the img tag.

I'm using two custom user defined functions (resizeToMin() and getImageType()) to help with the calculations on the dimension and the file types. getImageType uses the exif extension of PHP, so you'll need to be sure that you have exif support built in. This may require a re-compile of PHP. If you do not wish to use exif (shame on you), you can handle type based off of the file extension, but there might be security issues related to that... or at the very least, possible error issues.

The reason I want to determine file type is to know which functions to call, since functions are specific to jpg or gif, etc (for example: imagejpeg() vs imagegif()) .

This exercise takes an image and sizes it to the minimum height or width allowed. From there,it'll then center the image and cut the excess off. After it has the image it then renders it and displays it to the browser.

The magic really happens with imagecopyresampled() with the 3rd and 4th arguments which are 'x-coordinate of destination point' and 'y-coordinate of destination point'. These handle the actual overlaying of the image ontop of the canvas, in essence, cropping it.

Below is the full example of my avatar script. Let me know if you have any problems or most importantly... improvements!


<?
function resizeToMin($min, $filename, $filepath, $point){
	$currentimagesize = getimagesize($filepath.$filename);
	$image_width 	= $currentimagesize[0];
	$image_height	= $currentimagesize[1];
	$sizefactor		= 0;
	switch($point){
		case "height":
			if($image_height > $min){
			      $sizefactor 	= (double)($min / $image_height) ;
			}else{
				$sizefactor 	= 1;
			}
			break;
		case "width":
			if($image_width > $min){
			      $sizefactor 	= (double)($min / $image_width) ;
			}else{
				$sizefactor 	= 1;
			}
			break;
		case "global":
			if (($image_height > $min) || ($image_width > $min)){
			    if($image_height < $image_width) { 
			      $sizefactor = (double) ($min / $image_height);
			    }else{
			      $sizefactor = (double) ($min / $image_width) ;
			    }
			}
			break;
	}
	$newwidth 		= (int) ($image_width * $sizefactor);
	$newheight 		= (int) ($image_height * $sizefactor); 
	$oldsize 		= $image_width . "x" . $image_height;
	$newsize 		= $newwidth . "x" . $newheight;
	if($newsize	== "0x0"){
		$newsize 	= $image_width."x".$image_height;
	}
	return $newsize;
}


function getImageType($inType,$inArg){
	$type 					= "n/a";
	$imgTypeConstants		= array(
								IMAGETYPE_GIF,
								IMAGETYPE_JPEG
								);
	if($inType == 'form'){
		$arg				= $_FILES[$fieldName]['tmp_name'];
	}else{
		if(file_exists($inArg)){
			$arg			= $inArg;
		}else{
			echo "not valid path";
			exit();
		}
	}
	foreach($imgTypeConstants as $constantVal){
		if(exif_imagetype($arg) == $constantVal){
			$type			= exif_imagetype($arg);
			break;
		}
	}
	unset($imgTypeConstants);
	return $type;
}

$width 			= 40;
$height 		= 40;
$path			= '/path/to/file';
$filename		= 'apache.jpg';
$dimensions 	= getimagesize($path.$filename);
$type			= getImageType('path',$path.$filename);
if($type != 1 || $type != 2){exit();}
$canvas 		= imagecreatetruecolor($width,$height);
$piece 			= ($type == 1 ? imagecreatefromgif($path.$filename) :imagecreatefromjpeg($path.$filename));
$sizedDimension = resizeToMin($width, $filename, $path, "global");
$dimArr			= explode('x',$sizedDimension);
$newWidth		= $dimArr[0];
$newHeight		= $dimArr[1];
$cropLeft		= 0;
$cropHeight		= 0;
if($newWidth > $width){
	$center		= ($dimensions[0] / 2);
	$offset		= $center - $dimensions[0];
	$cropLeft	= abs($offset) / 2;
}
if($newHeight > $height){
	$center		= ($dimensions[1] / 2);
	$offset		= $center - $dimensions[1];
	$cropHeight	= abs($offset) / 2;
}
imagecopyresampled($canvas, $piece, 0, 0, $cropLeft, $cropHeight, $newWidth, $newHeight, $dimensions[0], $dimensions[1]);
if($type == 1){
	header('Content-type: image/gif');
	imagegif($canvas);
}else{
	header('Content-type: image/jpeg');
	imagejpeg($canvas);
}
imagedestroy($canvas);
imagedestroy($piece);
unset($dimensions);
unset($dimArr);
?> 


Submitted on 05/18/07 at 3:54PM
Post Comment | Comments: 0
Bookmark to:
Add 'PHP and GD for generating and cropping avatars' to Del.icio.us Add 'PHP and GD for generating and cropping avatars' to digg Add 'PHP and GD for generating and cropping avatars' to FURL Add 'PHP and GD for generating and cropping avatars' to blinklist Add 'PHP and GD for generating and cropping avatars' to reddit Add 'PHP and GD for generating and cropping avatars' to Feed Me Links Add 'PHP and GD for generating and cropping avatars' to Technorati Add 'PHP and GD for generating and cropping avatars' to Yahoo My Web Add 'PHP and GD for generating and cropping avatars' to Newsvine Add 'PHP and GD for generating and cropping avatars' to Socializer Add 'PHP and GD for generating and cropping avatars' to Ma.gnolia Add 'PHP and GD for generating and cropping avatars' to Stumble Upon Add 'PHP and GD for generating and cropping avatars' to Google Bookmarks Add 'PHP and GD for generating and cropping avatars' to RawSugar Add 'PHP and GD for generating and cropping avatars' to Squidoo Add 'PHP and GD for generating and cropping avatars' to Spurl Add 'PHP and GD for generating and cropping avatars' to BlinkBits Add 'PHP and GD for generating and cropping avatars' to Netvouz Add 'PHP and GD for generating and cropping avatars' to Rojo Add 'PHP and GD for generating and cropping avatars' to Blogmarks Add 'PHP and GD for generating and cropping avatars' to Shadows Add 'PHP and GD for generating and cropping avatars' to Simpy Add 'PHP and GD for generating and cropping avatars' to Co.mments Add 'PHP and GD for generating and cropping avatars' to Scuttle

Go Back








Advertisements

GoToMeeting - Online Meetings Made Easy