saunders1989
02-20-2010, 11:16 AM
i am really struggling after reading php.net on how to create functions. basically i have a code that creates a smaller image of an image i upload. i need to do 2 images. one thumbnail and an image that can be viewed.
function resizeImage ($directory, $maxwidth, $maxheight, $name)
that is about as much as ive understood.
my code i want to change into a function is this:
<?php
// $image, $maxHeight, $maxWidth
$pinfo = pathinfo($image);
$tmb_name = $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension'];
if(!isset($maxWidth)){
$maxWidth = 100;
}
if(!isset($maxHeight)){
$maxHeight = 150;
}
switch(strtolower(substr($image, -3))) {
case "jpg" :
$fileType = "jpeg";
$imageCreateFunction = "imagecreatefromjpeg";
$imageOutputFunction = "imagejpeg";
break;
case "jpeg" :
$fileType = "jpeg";
$imageCreateFunction = "imagecreatefromjpeg";
$imageOutputFunction = "imagejpeg";
break;
case "png" :
$fileType = "png";
$imageCreateFunction = "imagecreatefrompng";
$imageOutputFunction = "imagepng";
break;
case "gif" :
$fileType = "gif";
$imageCreateFunction = "imagecreatefromgif";
$imageOutputFunction = "imagegif";
break;
}
$size = GetImageSize($image);
$originalWidth = $size[0];
$originalHeight = $size[1];
$x_ratio = $maxWidth / $originalWidth;
$y_ratio = $maxHeight / $originalHeight;
// check that the new width and height aren't bigger than the original values.
// the new values are higher than the original, don't resize or we'll lose quality
if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) {
$newWidth = $originalWidth;
$newHeight = $originalHeight;
} else if (($x_ratio * $originalHeight) < $maxHeight) {
$newHeight = ceil($x_ratio * $originalHeight);
$newWidth = $maxWidth;
} else {
$newWidth = ceil($y_ratio * $originalWidth);
$newHeight = $maxHeight;
}
$src = $imageCreateFunction($image);
$dst = imagecreatetruecolor($newWidth, $newHeight);
// Resample
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
// Save image
$imageOutputFunction($dst, $tmb_name);
ImageDestroy($src);
ImageDestroy($dst);
?>
the reason i need this in a function is as the moment when i upload an image this script is run and is given a maxwidth and maxheight for a thumbnail i need a way of running this script again so i can create an image the user can view when i upload the file.
function resizeImage ($directory, $maxwidth, $maxheight, $name)
that is about as much as ive understood.
my code i want to change into a function is this:
<?php
// $image, $maxHeight, $maxWidth
$pinfo = pathinfo($image);
$tmb_name = $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension'];
if(!isset($maxWidth)){
$maxWidth = 100;
}
if(!isset($maxHeight)){
$maxHeight = 150;
}
switch(strtolower(substr($image, -3))) {
case "jpg" :
$fileType = "jpeg";
$imageCreateFunction = "imagecreatefromjpeg";
$imageOutputFunction = "imagejpeg";
break;
case "jpeg" :
$fileType = "jpeg";
$imageCreateFunction = "imagecreatefromjpeg";
$imageOutputFunction = "imagejpeg";
break;
case "png" :
$fileType = "png";
$imageCreateFunction = "imagecreatefrompng";
$imageOutputFunction = "imagepng";
break;
case "gif" :
$fileType = "gif";
$imageCreateFunction = "imagecreatefromgif";
$imageOutputFunction = "imagegif";
break;
}
$size = GetImageSize($image);
$originalWidth = $size[0];
$originalHeight = $size[1];
$x_ratio = $maxWidth / $originalWidth;
$y_ratio = $maxHeight / $originalHeight;
// check that the new width and height aren't bigger than the original values.
// the new values are higher than the original, don't resize or we'll lose quality
if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) {
$newWidth = $originalWidth;
$newHeight = $originalHeight;
} else if (($x_ratio * $originalHeight) < $maxHeight) {
$newHeight = ceil($x_ratio * $originalHeight);
$newWidth = $maxWidth;
} else {
$newWidth = ceil($y_ratio * $originalWidth);
$newHeight = $maxHeight;
}
$src = $imageCreateFunction($image);
$dst = imagecreatetruecolor($newWidth, $newHeight);
// Resample
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
// Save image
$imageOutputFunction($dst, $tmb_name);
ImageDestroy($src);
ImageDestroy($dst);
?>
the reason i need this in a function is as the moment when i upload an image this script is run and is given a maxwidth and maxheight for a thumbnail i need a way of running this script again so i can create an image the user can view when i upload the file.