Image Resizing Function Problem
Posted: Fri Oct 07, 2005 12:55 pm
feyd | Please use
feyd | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
I am having trouble with the following bit of code. The intention is to take an image file that will be uploaded and resize it until it is of an acceptable filesize. I have the function resize($imageFile) that does the resizing which is working. I then have a function adjustFileSize($imageFile) which I want to call the resize function until the filesize of the image is of an acceptable size. The adjustFileSize bit is not working properly. The problem seems to be that after it calls the resize function, which is resizing the image, the $filesize variable does not change to reflect the smaller filesize of the image. Any help would be much appreciated. Here is the code:Code: Select all
function resize($imageFile)
{
// File and new size
$filename = $imageFile;
$percent = 0.66;
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
preg_match("'^(.*)\.(gif|jpe?g|png)$'i", $imageFile, $ext);
switch (strtolower($ext[2]))
{
case 'jpg' :
case 'jpeg': $source = imagecreatefromjpeg($filename);
break;
case 'gif' : $source = imagecreatefromgif($filename);
break;
case 'png' : $source = imagecreatefrompng($filename);
break;
}
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
preg_match("'^(.*)\.(gif|jpe?g|png)$'i", $imageFile, $ext);
switch (strtolower($ext[2]))
{
case 'jpg' :
case 'jpeg':imagejpeg($thumb,$filename,50);
break;
case 'gif' :imagegif($thumb,$filename);
break;
case 'png' : imagepng($thumb,$filename);
break;
}
}
//resize image until filesize is apropriate
function adjustFileSize($imageFile, $maxFileSize)
{
$filesize = filesize($imageFile);
do
{
resize($imageFile);
$filesize = filesize($imageFile);
}
while($filesize > $maxFileSize);
}feyd | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]