View Full Version : Resize Function

11-11-2007, 10:49 PM
Hi There guys

finished writing a function to resize uploaded images, it workes pretty well, but earlier i uploaded an image and because i'm resizing in % it was still pretty big, so i was going to set specific widths and heights (100w & 150h) instead of in %

is it better to resize in percentage or set height/widths would you say?

function resize_images($filename,$uploaddirectory,$var_loggedinuser) {

## Find out the files extension
$ext = explode(".", $filename);
$ext = $ext[count($ext)-1];

## Make all filenames lowercase
$ext = strtolower($ext);

if($ext == "jpg" || $ext == "jpeg")

$image = imagecreatefromjpeg($uploaddirectory);

elseif($ext == "png")

$image = imagecreatefrompng($uploaddirectory);

elseif($ext == "gif")

$image = imagecreatefromgif($uploaddirectory);

## save the file in %
$size = 0.50;

## rename the thumbnail
$newimagename_thumb = $var_loggedinuser. "-" .time();
$save = "thumbs/$newimagename_thumb.$ext";

## get the files dimensions
list($width,$height) = getimagesize($uploaddirectory);

## New measurements
$modwidth = $width * $size;
$modheight = $height * $size;

## Start making the image
$thumbnail = imagecreatetruecolor($modwidth, $modheight) ;
imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);

## Only return the image identified correctly
if($ext == 'jpg' || $ext == 'jpeg')

imagejpeg($thumbnail, $save, 100);

if($ext == 'gif')

imagegif($thumbnail, $save, 100);

if($ext == 'png')

imagepng($thumbnail, $save, 100);

return($newimagename_thumb. "." .$ext);


should i re-write this from scratch or is it easily modified to implement set width and height can anyone tell me?

thanks guys


11-11-2007, 10:58 PM
Why don't you just do both?

Set the maximum height and width and then just check if the percentage of the height/width mentioned is bigger than the maximum height/width and then reduce it relatively.

11-12-2007, 12:45 AM
It depends on what you're using the images for, and if you want them an exact size or if you just need them less than a certain size. For the former you would just set the size, and for the latter you would resize it proportionately.