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);
?>
Bookmark to:
Go Back
