Image upload and resize producing a black image.
Posted: Fri Oct 07, 2005 8:49 am
We've moved a website over to a different server and now the following bit of code which uploads and resizes an image is failing somewhere. It works apart from producing a black image as a result, a problem which I know is common but conveniently can't find any posts on when I need to!
I'm not even sure if this script worked fully on its previous server as we only recently inherited this from the previous company so I didn't code it to begin with.
I think the problem is to do with the ImagePaletteCopy ($dimg, $simg) bit but I'm not sure. Can someone help? The server is running PHP 4.3.10.
Thanks
This is part of the the form handling script:
And this is the custom function that gets called.
I'm not even sure if this script worked fully on its previous server as we only recently inherited this from the previous company so I didn't code it to begin with.
I think the problem is to do with the ImagePaletteCopy ($dimg, $simg) bit but I'm not sure. Can someone help? The server is running PHP 4.3.10.
Thanks
This is part of the the form handling script:
Code: Select all
if ($_FILES['newSectionImage']['tmp_name'])
{
list($width, $height, $type, $attr) = getimagesize($_FILES['newSectionImage']['tmp_name']);
switch($type)
{
case 1:
$extension = '.gif';
$tempImage = imagecreatefromgif($_FILES['newSectionImage']['tmp_name']);
break;
case 2:
$extension = '.jpg';
$tempImage = imagecreatefromjpeg($_FILES['newSectionImage']['tmp_name']);
break;
case 3:
$extension = '.png';
$tempImage = imagecreatefrompng($_FILES['newSectionImage']['tmp_name']);
break;
default:
die("<B>Error:</B> This image type is not supported, you must use a gif, jpeg or PNG file.");
}
// Set new image's height and width
$new_width = 60;
$new_height = round($height/($width/60));
// Resample
$newImage = imagecreate($new_width, $new_height)
or die("<B>Error: New image could not be created.</B> ");
// Do a bicubic resample
imageCopyResampleBicubic($newImage, $tempImage, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
$img = "../images/sections/section_$section$extension";
$sectionImage = "section_$section$extension";
imagejpeg($newImage, $img, 95)
or die("<B>Error:</B> Image could not be saved.");
// Destroy Images
imagedestroy($newImage);
imagedestroy($tempImage);
}
// Do MySQL to retrieve section data.
$dbQuery = "UPDATE sections
SET sectionImage = '$sectionImage',
sectionTitle = '$sectionTitle',
sectionText = '$sectionText'
WHERE sectionID = $section";
// Execute query
$dbResult = mysql_query($dbQuery, $dbLink)
or die("$dbQuery: The section data could not be updated.");Code: Select all
function imageCopyResampleBicubic (&$dimg, &$simg, $dx, $dy, $sx, $sy, $dw, $dh, $sw, $sh)
{
ImagePaletteCopy ($dimg, $simg);
$rX = $sw / $dw;
$rY = $sh / $dh;
$nY = 0;
for ($y=$dy; $y<$dh; $y++)
{
$oY = $nY;
$nY = round(($y + 1) * $rY);
$nX = 0;
for ($x=$dx; $x<$dw; $x++)
{
$oX = $nX;
$nX = round(($x + 1) * $rX);
$r = $g = $b = $a = 0;
for ($i=$nY; --$i>=$oY;)
{
for ($j=$nX; --$j>=$oX;)
{
$c = ImageColorsForIndex ($simg, ImageColorAt ($simg, $j, $i));
$r += $c['red'];
$g += $c['green'];
$b += $c['blue'];
$a++;
}
}
ImageSetPixel ($dimg, $x, $y, ImageColorClosest ($dimg, $r/$a, $g/$a, $b/$a));
}
}
}