Keleth
02-22-2012, 08: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.
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.