...

View Full Version : Setting default transparency color for read png



Keleth
02-22-2012, 09:34 PM
So I've got a pretty straight forward script:


list($imgWidth, $imgHeight, $imgType) = getimagesize($_FILES['img']['tmp_name']);
if (image_type_to_mime_type($imgType) == 'image/jpeg' || image_type_to_mime_type($imgType) == 'image/pjpeg') $tempImg = imagecreatefromjpeg($_FILES['img']['tmp_name']);
elseif (image_type_to_mime_type($imgType) == 'image/gif') $tempImg = imagecreatefromgif($_FILES['img']['tmp_name']);
elseif (image_type_to_mime_type($imgType) == 'image/png') $tempImg = imagecreatefrompng($_FILES['img']['tmp_name']);

$xRatio = $maxWidth / $imgWidth;
$yRatio = $maxHeight / $imgHeight;

if ($imgWidth <= $maxWidth && $imgHeight <= $maxHeight) {
$finalWidth = $imgWidth;
$finalHeight = $imgHeight;
} elseif (($xRatio * $imgHeight) < $maxHeight) {
$finalWidth = $maxWidth;
$finalHeight = ceil($xRatio * $imgHeight);
} else {
$finalWidth = ceil($yRatio * $imgWidth);
$finalHeight = $maxHeight;
}

$tempColor = imagecreatetruecolor($finalWidth, $finalHeight);
imagecopyresampled($tempColor, $tempImg, 0, 0, 0, 0, $finalWidth, $finalHeight, $imgWidth, $imgHeight);

// Define $imageID here

imagejpeg($tempColor, FILEROOT."/images/uploads/$imageID.jpg", 100);
imagedestroy($tempImg);
imagedestroy($tempColor);

However, when I upload a PNG, any transparency in it goes black. I'm assuming the same would happen with a GIF. I'm trying to figure out how to make the transparency go white instead. Anyone have thoughts? I've looked at functions such as imagecolortransparent, but can't seem to use it correctly, if it is the right function to use at all.

Fou-Lu
02-23-2012, 03:19 PM
I'm thinking set the imagealphablending on for $tempColor, then paint the background white. After that, copy the image from $tempImg onto it.
Try that, see what it does. I can't test that where I am.

Keleth
02-23-2012, 03:59 PM
Haha, I'll be honest, I have no idea how to really do what you said :P I found that script online... I really need to learn GD... not asking for the specific code (though I'd be stupid to turn it down), but could you point me in the right direction?

Fou-Lu
02-23-2012, 06:57 PM
That would be a matter of:


$white = imagecolorallocate($tempColor, 255, 255, 255);
imagealphablending($tempColor, true);
imagefill($tempColor, 0, 0, $white);

Then perform the copy.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum