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

PHP将jpeg合并到png之上,并具有alpha透明度,php,png,gd2,Php,Png,Gd2,我试图把一个jpeg放在png后面-png有alpha透明度 前景图像如下所示: 后面的图像是facebook个人资料图像-通常如下所示: 结果图像失去透明度,变为黑色: 这是我使用的代码: // combine image with shadow $newCanvas = imagecreatetruecolor(90,135); $shadow = imagecreatefrompng("marker-shadow.png"); //imagealphablending($new

我试图把一个jpeg放在png后面-png有alpha透明度

前景图像如下所示:

后面的图像是facebook个人资料图像-通常如下所示:

结果图像失去透明度,变为黑色:

这是我使用的代码:

// 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);,结果是正确的(标记中间的空洞是透明的),但是后面的图像消失了。p> 你能解释一下吗?:-)

谢谢

编辑:找到解决方案

我做了一些修改,最终得到了这段代码——源代码不是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" />

我尝试了下面的代码,它对我很有效

$width = 400; $height = 400; $base_image = imagecreatefromjpeg("base.jpg"); $top_image = imagecreatefrompng("top.png"); imagesavealpha($top_image, false); imagealphablending($top_image, false); imagecopy($base_image, $top_image, 0, 0, 0, 0, $width, $height); imagepng($base_image, "merged.png"); $width=400; $height=400; $base_image=imagecreatefromjpeg(“base.jpg”); $top_image=imagecreatefrompng(“top.png”); imagesavealpha($top_image,false); imagealphablending($top_image,false); imagecopy($base\u image,$top\u image,0,0,0,$width,$height); imagepng($base_image,“merged.png”);
我检查第一个脚本。对于所有透明png,您必须应用以下代码

imagesavealpha($shadow,true); 图像混合($shadow,true)


其他明智的做法是,黑色填充物将在那里。在这里,您没有将其应用于“marker shadow.png”文件对象,请按照php脚本运行,并查看该数组集上可用的天气https

echo "<pre>"; print_r(stream_get_wrappers()); echo "</pre>"; 回声“; print_r(stream_get_wrappers()); 回声“; 输出将是这样的

// 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);
排列 ( [0]=>php [1] =>文件 [2] =>全球 [3] =>数据 [4] =>http [5] =>ftp [6] =>拉链 [7] =>compress.zlib [8] =>https [9] =>ftps [10] =>compress.bzip2 [11] =>法尔 ) 此处数组元素8显示启用了https。如果您的代码中没有该选项,那么。找到php.ini文件并将下面的行放在那里

extension=php\u openssl.dll


重新启动服务器后,您的功能将与facebook资源url甚至一起工作。

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

// 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);

为此挣扎了一段时间,这里没有一个答案能完全帮助我。下面是试图在透明PNG上加上JPG的代码(注意“/!!!*”注释,它们很重要):


信用证:

确保您也在$canvas图像上启用了alpha。Halfer。我并不是有意要显得讽刺:-)不过,我确实需要一些关于我之前问题的指导。我总共问了6个问题。一个是没有答案。删除。回答这个问题:806133。最合理的答案是我自己的——但这是一个解决办法。我应该接受我自己的答案吗?或者其他人的回答不充分?由于某种原因,当我试图首先通过运行ImageCreateTureColor(..)创建一个新的图像画布(使用我的代码)时,这不起作用。bg仍然是黑色的。我检查您的代码(fbimage.php)是否工作正常。唯一的问题是,您正在从https url加载facebook thum。要使其工作,您必须在php.ini文件上启用https包装器。但对于本地文件,它工作正常。我将把启用https包装器的指令作为另一个答案。是的-fbimage.php也适用于我。在我看来,fbimage.php与我发布的第一个代码的区别在于,第一个代码使用imagecreatetruecolor作为基础画布,而fbimage.php使用png作为带有imagecreatefrompng的模板。是否有一些代码应该应用于ImageCreateTureColor对象,以使其与png模板的行为相同?我检查了第一个脚本。对于所有透明png,您必须应用以下代码。imagesavealpha($shadow,true);图像混合($shadow,true);其他明智的做法是,黑色填充物将在那里。在这里,您没有将其应用于“marker shadow.png”文件对象,它产生了以下结果:数组([0]=>https[1]=>ftps[2]=>compress.zlib[3]=>compress.bzip2[4]=>php[5]=>file[6]=>glob[7]=>data[8]=>http[9]=>ftp[10]=>phar[11]=>zip)为我解决这个问题的是第8行“imagealphablending($base_image,true);“在透明层之后。谢谢。
// 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);
// 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);
// 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);