SEARCH
  Web Programming PHP Image Class: Function Resize

Image Class: Function Resize

nathanael PHP May 30, 2007

function resize($Image, $Img_Dir, $width, $height, $mod_name = '', $target_dir = ''){
$ext = $this->getExtension($Image);
if($ext != "jpg" && $ext != "jpeg" && $ext != "gif"){
$_SESSION['Message'] = "Only .jpg, .jpeg, and .gif are supported";
return FALSE;
}
$sourcefile = $Img_Dir . $Image;
$imagesize = getimagesize("$sourcefile");
if($imagesize[0] > $width || $imagesize[1] > $width) {
if ($imagesize[0] > $imagesize[1]) {
$percentage = ($width / $imagesize[0]);
} else {
$percentage = ($height / $imagesize[1]);
}
$new_width = round($imagesize[0] * $percentage);
$new_height = round($imagesize[1] * $percentage);
$dest_x = $new_width;
$dest_y = $new_height;
if($target_dir != ''){
$targetfile = $target_dir . $mod_name . $Image;
} else {
$targetfile = $Img_Dir . $mod_name . $Image;
}
$jpegqual = 100;
switch($ext){
case 'jpg' :
case 'jpeg' :
$source_id = imageCreatefromjpeg("$sourcefile");
break;
case 'gif' :
$source_id = imageCreatefromgif("$sourcefile");
break;
}
$target_id = imagecreatetruecolor($dest_x, $dest_y);
$target_pic = imagecopyresampled ($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$imagesize[0],$imagesize[1]);
imagejpeg ($target_id,"$targetfile",$jpegqual);
}
}

Ok so this is all the code that makes up the resize function. we pass 4 varialbes, and have two option variable.

First we have $Image. this is the name of the image we will resize. remember in the upload function, we return the name of the uploaded image. this is why. we first call the upload, then the upload function returns the name of the uploaded image. we then pass that to the image resize. you'll see this more in the sample file.

Next we have $Img_Dir. This is the directory in which the source file is (the imge we will resize). this is NOT the destination directory. The desination directory is the 2nd option variable ($target_dir). Ill go over this more when I explain $target_dir. Remember, this directory needs to have a mode of 777.

Next we have $width and $height. This is for the MAX width and height, not for a definded width and height. Say we have a 1024x768 image, and set the $width and $height each to 50 (50x50) if this created the image 50x50, the image would be skewed. What happens is, the function figures out the correct proportions to resize the image to. In the case of the 1024x768 image, the width would be set to 50px (because the width has the largest number) then the height would be set to a size to keep the correct proportions (which is 38px) .

Next we have our option variable, $mod_name = ''. If we dont set this (which we dont have to do), the resized image will keep the same name as the source file, and over write it (unless we set the 2nd option variable). So if we want the resized image, and the original image in the same directory, we have to modify the name of the resized image so it doesnt over write the original. If thats the case we want to set the mod name. whatever you place in the mod name, well be added to the beginning of the origianl file name. So say we resized img.jpg, and give a mod name of thumb_ the resized image would be thumb_img.jpg

Now we have our 2nd option variable $target_dir = ''. If you want the resized image stored in a different directory than the original, you will set that directory here. As with $Img_Dir, this directory will need a mode of 777. Now say you want to set this option varialbe, but not $mod_name. you will still need to pass $mod_name, you will just set it to blank (ill show you in the sample file).

Next we have

$sourcefile = $Img_Dir . $Image;

All this does is sets $source file = to the $Img_Dir + $Image (which means $Img_Dir should end with a /)

Say we set $Img_Dir to /images/ and $Image to img.jpg, $sourcefile would be images/img.jpg

Next is:

$imagesize = getimagesize("$sourcefile");
if($imagesize[0] > $width || $imagesize[1] > $width) {
if ($imagesize[0] > $imagesize[1]) {
$percentage = ($width / $imagesize[0]);
} else {
$percentage = ($height / $imagesize[1]);
}
$new_width = round($imagesize[0] * $percentage);
$new_height = round($imagesize[1] * $percentage);
$dest_x = $new_width;
$dest_y = $new_height;

Short and simple: This does the math to figure out the correct proportions of the image size.

Now we figure out the name and directory of the resized image.

if($target_dir != ''){
$targetfile = $target_dir . $mod_name . $Image;
} else {
$targetfile = $Img_Dir . $mod_name . $Image;
}

All this says is that if the option variable is set for $target_dir use that directory, if not, use the original directory. Also notice $mod_name is always in there. thats because if its not set, its blank, so theres really no need to break it down more to say if its set use it, if not, dont use it. If its not set, its blank and wont be seen; keeping the original file name.

Now we have

$jpegqual = 100;

This sets the quality of the imgage. 100 being best 0 being lest.

Now we figure out what kind of file it was, and start making the resized image

switch($ext){
case 'jpg' :
case 'jpeg' :
$source_id = imageCreatefromjpeg("$sourcefile");
break;
case 'gif' :
$source_id = imageCreatefromgif("$sourcefile");
break;
}

This does a case switch based on the extention. we use imageCreatefromjpeg if its a jpg or jpeg, and use imageCreatefromgif if its a gif. remember when I was talking about the png? well there is a function imageCreatefrompng, but like I said, it never worked for me. you can add that here if you'd like, add this after the 2nd break

case 'png' :
$source_id = imageCreatefrompng("$sourcefile");
break;

Finally we create the final resized image

}
$target_id = imagecreatetruecolor($dest_x, $dest_y);
$target_pic = imagecopyresampled ($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$imagesize[0],$imagesize[1]);
imagejpeg ($target_id,"$targetfile",$jpegqual);
}
}

Now for the sample file

<?
session_start();
require_once 'func_images.php';

$obj_images = new images();
$GLOBALS['img_root'] = "images/";
$GLOBALS['sub'] = "images/thumbs/";

switch($_REQUEST['case']){
default :
if(isset($_SESSION['Message'])){
echo $_SESSION[Message];
unset($_SESSION['Message']);
}
echo<<<EOF
<form method="post" action="$PHP_SELF" name="theForm" enctype="multipart/form-data">
<input type="file" name="img" />
<input type="hidden" name="case" value="submit" />
<input type="submit" />
</form>
EOF;
$ext = $obj_images->getExtension("./test/munky_val.jpg");
echo $ext;
break;
case 'submit' :
//upload the image
$upload = $obj_images->upload("img", $GLOBALS['img_root']);

//check if upload passed a session message
if(isset($_SESSION['Message']){
header("location: $PHP_SELF");
exit;
}
//resize the image (using the same name and directory as the original)
$obj_images->resize($upload, $GLOBALS['img_root'], "50", "50");

//resize the image (using a mod name)
// $obj_images->resize($upload, $GLOBALS['img_root'], "50", "50", "thumb_");

//resize the image (using a mod name and different destination)
// $obj_images->resize($upload, $GLOBALS['img_root'], "50", "50", "thumb_", $GLOBALS['sub']);

//resize the image (using no mod name with a different dest)
// $obj_images->resize($upload, $GLOBALS['img_root'], "50", "50", "", $GLOBALS['sub']);
header("location: $PHP_SELF");
break;
}
?>

Now for the last function

subscribe to newsletter