...

View Full Version : Resize uploaded image



levani
02-21-2010, 12:29 PM
I'm using this script for multiple image upload, but I want to resize each images uploaded through the form so that the maximum height and width should be 500px, without loosing the proportions. Here is the script:


while(list($key,$value) = each($_FILES['images']['name']))

{

if(!empty($value))

{

$filename = $value;

$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line



while(file_exists('upimg/'.$filename))

$filename=rand(1,9999).$filename;

$add = "upimg/$filename";

//echo $_FILES['images']['type'][$key];

// echo "<br>";

copy($_FILES['images']['tmp_name'][$key], $add);

chmod("$add",0777);

}

Can anyone please help?

Thanks in advance

Killermud
02-21-2010, 03:10 PM
Well theres no way to essentially change the properties of the image using php, but there is a way to test to make sure that what your uploading is within the ranges you want. So heres what i would do :


list($width,$height) = getimagesize($_FILES['images']['tmp_name']);

if($width<500 && $height<500){
// contiune upload process
}

But if this is not what you want there is a way to take the image and create a new image from your old one with new image sizes, but this is long and unideal.

MattF
02-21-2010, 03:21 PM
But if this is not what you want there is a way to take the image and create a new image from your old one with new image sizes, but this is long and unideal.

Not ideal why? The method is used on a multitude of sites. It's a far better option than chucking grossly over-sized images down the wire.

Killermud
02-21-2010, 03:43 PM
Well firstly because you need to load the image, test its image type, size, then adjust the file sizes, then you need to save the file, then output thats a lot to do rather than just testing the image size alone, much easier quicker and less strainous for the server.

MattF
02-21-2010, 04:11 PM
Well firstly because you need to load the image, test its image type, size, then adjust the file sizes, then you need to save the file, then output thats a lot to do rather than just testing the image size alone, much easier quicker and less strainous for the server.

You resize each image exactly once, at the time when they are uploaded. Again, constantly serving a grossly over-sized image is far more detrimental on both server and client resources than a resize on upload.

Killermud
02-21-2010, 04:17 PM
I am unaware of this method, all the methods i know of require you upload the image and then create a new image off this with your properties. Even the imagecopyresampled() function requires you to upload an image and create a new image for it.

MattF
02-21-2010, 05:14 PM
I am unaware of this method, all the methods i know of require you upload the image and then create a new image off this with your properties. Even the imagecopyresampled() function requires you to upload an image and create a new image for it.

Which is exactly what the O.P is wanting to do. Upload an image, (or images), and have them resized to around 500px or so whilst retaining the width/height ratio.

MattF
02-21-2010, 05:19 PM
Resize function:



function image_resize($sourcefile, $width, $height, $delete = false)
{
$ext = strtolower(substr(strrchr($sourcefile, '.'), 1));
$extensions = array('gif', 'jpg', 'jpeg', 'png');

if (empty($ext) || !in_array($ext, $extensions))
{
return false;
}

list($currwidth, $currheight, $type) = getimagesize($sourcefile);

if (empty($currwidth) || empty($currheight) || $type > 3)
{
return false;
}

$ratio = ($currwidth/$currheight);

if ($currwidth <= $width && $currheight <= $height)
{
if (is_file($sourcefile))
{
copy($sourcefile, $sourcefile.'_new');
return $sourcefile.'_new';
}
return false;
}
else
{
$outputfile = $sourcefile.time();

if (($width/$height) > $ratio)
{
$width = ($height*$ratio);
}
else
{
$height = ($width/$ratio);
}

$dimg = imagecreatetruecolor($width, $height);
$background = imagecolorallocate($dimg, 255, 255, 255);
imagefill($dimg, 0, 0, $background);

if ($type == 1)
{
$simg = imagecreatefromgif($sourcefile);
imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $width, $height, $currwidth, $currheight);
imagegif($dimg, $outputfile);
}
else if ($type == 2)
{
$simg = imagecreatefromjpeg($sourcefile);
imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $width, $height, $currwidth, $currheight);
imagejpeg($dimg, $outputfile, 85);
}
else if ($type == 3)
{
$simg = imagecreatefrompng($sourcefile);
imagealphablending($simg, true);
imagesavealpha($simg, true);
imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $width, $height, $currwidth, $currheight);
imagepng($dimg, $outputfile, 9);
}
imagedestroy($dimg);
imagedestroy($simg);

if (is_file($outputfile))
{
if ($delete)
{
unlink($sourcefile);
}
return $outputfile;
}
return false;
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum