PHP透明图像分层问题

时间:2010-01-04 23:18:38

标签: php image image-processing

我不知道为什么会这样:

<?php

     $image = imagecreatefromjpeg($_GET['u']);
     imagealphablending($image, true);
     imagesavealpha($image,true);
     $overlay = imagecreatefrompng("overlay.png");
     imagealphablending($overlay, true);
     imagesavealpha($overlay,true);
     $finalImage = imagecreate(85,85);
     imagealphablending($finalImage, true);
     imagesavealpha($finalImage,true);

     $trans = imagecolorallocate($finalImage,255,0,0);
     imagecolortransparent($finalImage,$trans);
     imagefill($finalImage, 0, 0, $trans);

     imagecopy($finalImage, $image, 5, 5, 0, 0, 75, 75);
     imagecopy($finalImage, $overlay, 0, 0, 0, 0, 85, 85);
     imageDestroy($image);
     imageDestroy($overlay);


     // Content type
     header('Content-type: image/png');
     imagepng($finalImage);
     imagedestroy($finalImage);

&GT;

产生这个:

alt text http://alanjack.co.uk/travel/0rotatedImage.php%20(1).png

进行影像检查时,一个或另一个产生健康的结果:

         imagecopy($finalImage, $image, 5, 5, 0, 0, 75, 75);
     //imagecopy($finalImage, $overlay, 0, 0, 0, 0, 85, 85);
     //ImageDestroy($image);
     //ImageDestroy($overlay);

,并提供:

alt text http://alanjack.co.uk/travel/1rotatedImage.php%20(1).png

         //imagecopy($finalImage, $image, 5, 5, 0, 0, 75, 75);
     imagecopy($finalImage, $overlay, 0, 0, 0, 0, 85, 85);
     //ImageDestroy($image);
     //ImageDestroy($overlay);

,并提供:

alt text http://alanjack.co.uk/travel/2rotatedImage.php%20(1).png

它可能是某种调色板不一致或某种东西 - 与一个是PNG而另一个是JPEG有什么关系?

Grrrrrrrr ...艾伦生气... ALAN想要嘘!!

2 个答案:

答案 0 :(得分:2)

请尝试使用此代码:

<?php

$image = imagecreatefromjpeg($_GET['u']);
imagesavealpha($image, true);
imagealphablending($image, true);

$overlay = imagecreatefrompng("overlay.png");
imagesavealpha($overlay, true);
imagealphablending($overlay, true);

$finalImage = imagecreatetruecolor(85,85);
imagefill($finalImage, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($finalImage, true);
imagealphablending($finalImage, true);

/*
$trans = imagecolorallocatealpha($finalImage, 255, 0, 0, 127);
imagecolortransparent($finalImage, $trans);
imagefill($finalImage, 0, 0, $trans);
*/

imagecopy($finalImage, $image, 5, 5, 0, 0, 75, 75);
imagecopy($finalImage, $overlay, 0, 0, 0, 0, 85, 85);
//imageDestroy($image);
//imageDestroy($overlay);

// Content type
header('Content-type: image/png');
imagepng($finalImage);
//imagedestroy($finalImage);

?>

它能解决你的问题吗?

答案 1 :(得分:0)

当我将imagecreate()更改为imagecreatetruecolor()时,灰色矩形消失了,所以我认为这毕竟是调色板问题!

无论如何,谢谢你们。