PHP将jpeg合并到具有alpha透明度的png之上


PHP Merge jpeg on top of png with alpha transparency

我试图在png后面放一个jpeg,其中png具有alpha透明度。

前景图像如下:http://peugeot208.srv.good-morning.no/images/marker-shadow.png

后面的图片是一张脸书个人资料图片,通常是这样的:https://graph.facebook.com/100000515495823/picture

结果图像失去透明度,而是黑色:http://peugeot208.srv.good-morning.no/libraries/cache/test.png

这是我使用的代码:

// combine image with shadow
$newCanvas = imagecreatetruecolor(90,135);
$shadow = imagecreatefrompng("marker-shadow.png");  
//imagealphablending($newCanvas, false);
imagesavealpha($newCanvas, true);   
imagecopy($newCanvas, $canvas, 20, 23, 0, 0, 50, 50);
imagecopy($newCanvas, $shadow, 0, 0, 0, 0, 90, 135);
imagepng($newCanvas, $tempfile, floor($quality * 0.09));

如果我启用imagealphablending($newCanvas,false);,结果是正确的(标记中间的孔是透明的),但后面的图像不见了。

你能解释一下吗?:-)

谢谢!

编辑:找到解决方案

我做了一些修改,最终得到了这个代码——其中的原点不是createimagetruecolor,而是从模板创建的图像——这是一个透明的png。

现在它工作了——结果是适当透明的。我真的不知道为什么。知道为什么吗?

fbimage.php

// Create markerIcon 
$src = $_REQUEST['fbid'];
$base_image = imagecreatefrompng("../images/marker-template.png");
$photo = imagecreatefromjpeg("https://graph.facebook.com/".$src."/picture");
$top_image = imagecreatefrompng("../images/marker-shadow.png");
imagesavealpha($base_image, true);
imagealphablending($base_image, true);
imagecopy($base_image, $photo, 20, 23, 0, 0, 50, 50);
imagecopy($base_image, $top_image, 0, 0, 0, 0, 90, 135);
imagepng($base_image, "./cache/".$src.".png");
?>
<img src="./cache/<?php echo $src ?>.png" />

更新:检查以下代码您可以在此处找到结果:http://peugeot208.srv.good-morning.no/images/marker.php正如你所看到的,背景仍然是黑色的。

// create base image
$base_image = imagecreatetruecolor(90,135);
$photo = imagecreatefromjpeg("marker-original.jpg");
$top_image = imagecreatefrompng("marker-shadow.png");
imagesavealpha($top_image, true);
imagealphablending($top_image, true);
imagesavealpha($base_image, true);
imagealphablending($base_image, true);
// merge images
imagecopy($base_image, $photo, 20, 23, 0, 0, 50, 50);
imagecopy($base_image, $top_image, 0, 0, 0, 0, 90, 135);
// return file
header('Content-Type: image/png');
imagepng($base_image);

解决方案是将颜色分配为100%alpha透明,然后在基础图像的整个画布上绘制一个正方形:

// create base image
$base_image = imagecreatetruecolor(90,135);
// make $base_image transparent
imagealphablending($base_image, false);
$col=imagecolorallocatealpha($base_image,255,255,255,127);
imagefilledrectangle($base_image,0,0,90,135,$col);
imagealphablending($base_image,true);    
imagesavealpha($base_image, true);
// --- 
$photo = imagecreatefromjpeg("marker-original.jpg");
$top_image = imagecreatefrompng("marker-shadow.png");
// merge images
imagecopy($base_image, $photo, 20, 23, 0, 0, 50, 50);
imagecopy($base_image, $top_image, 0, 0, 0, 0, 90, 135);
// return file
header('Content-Type: image/png');
imagepng($base_image);

运行以下php脚本,查看该数组集上可用的天气https。

echo"<pre>";print_r(stream_get_wrappers());echo"</pre>";

结果会是这样。

大堆([0]=>php[1] =>文件[2] =>球[3] =>数据[4] =>http[5] =>ftp[6] =>拉链[7] =>压缩.zlib[8] =>https[9] =>ftps[10] =>压缩.bzip2[11] =>phar)

这里的数组元素8th显示https已启用。如果这在你的代码中不可用,那么。找到php.ini文件并将下面的行放在那里。

extension=php_openssl.dll

在重启服务器之后,你的功能将与facebook资源url一起工作。

我尝试了以下代码,它对我来说很好

$width=400;$height=400;$base_image=imagecreatefromjpeg("base.jpg");$top_image=imagecreatefrompng("top.png");imagesavealpha($top_image,false);imagealphabiling($top_image,false);imagecopy($base_image、$top_image,0、0、0,$width、$height);imagepng($base_image,"merged.png");

我检查了第一个脚本。对于所有透明的png,您必须应用以下代码。

imagesavealpha($shadow,true);imagealphabiling($shadow,true);

另一方面,黑色填充物将在那里。在这里,您没有将其应用于"marker-shadow.png"文件对象

为此挣扎了一段时间,这里没有一个答案能完全帮助我。以下是在尝试将JPG置于透明PNG之上时完美工作的代码(请注意"//!!!*"注释,它们很重要):

// create a true colour, transparent image
// turn blending OFF and draw a background rectangle in our transparent colour 
$image=imagecreatetruecolor($iwidth,$iheight);
imagealphablending($image,false);
$col=imagecolorallocatealpha($image,255,255,255,127);
imagefilledrectangle($image,0,0,$iwidth,$iheight,$col);
imagealphablending($image,true);
// ^^ Alpha blanding is back on.
// !!! *** IMAGE MANIPULATION STUFF BELOW ***
$backImage = imagecreatefrompng("yourimage.png");
imagecopyresampled($image, $backImage, 0, 0, 0, 0, 400, 300, 400, 300);
$foreImage = imagecreatefromjpeg("yourimage.png");
imagecopyresampled($image, $foreImage, 10, 10, 0, 0, 200, 150, 200, 150);
// !!! *** IMAGE MANIPULATION STUFF ABOVE ***
// output the results... 
header("Content-Type: image/png;");
imagealphablending($image,false);
imagesavealpha($image,true);
imagepng($image);

学分:http://www.bl0g.co.uk/creating-transparent-png-images-in-gd.html

// create base image
$photo = imagecreatefromjpeg("Penguins.jpg");
$frame = imagecreatefrompng("frame.png");
// get frame dimentions
$frame_width = imagesx($frame);
$frame_height = imagesy($frame);
// get photo dimentions
$photo_width = imagesx($photo);
$photo_height = imagesy($photo);
// creating canvas of the same dimentions as of frame
$canvas = imagecreatetruecolor($frame_width,$frame_height);
// make $canvas transparent
imagealphablending($canvas, false);
$col=imagecolorallocatealpha($canvas,255,255,255,127);
imagefilledrectangle($canvas,0,0,$frame_width,$frame_height,$col);
imagealphablending($canvas,true);    
imagesavealpha($canvas, true);
// merge photo with frame and paste on canvas
imagecopyresized($canvas, $photo, 0, 0, 0, 0, $frame_width, $frame_height,$photo_width, $photo_height); // resize photo to fit in frame
imagecopy($canvas, $frame, 0, 0, 0, 0, $frame_width, $frame_height);
// return file
header('Content-Type: image/png');
imagepng($canvas);
// destroy images to free alocated memory
imagedestroy($photo);
imagedestroy($frame);
imagedestroy($canvas);