PHP将png顶部的jpeg与alpha透明度合并

时间:2012-03-23 15:08:22

标签: php png gd2

我正在尝试将一个jpeg放在png后面 - 其中png具有alpha透明度。

前景图片在这里: http://peugeot208.srv.good-morning.no/images/marker-shadow.png

背后的图片是facebook个人资料图片 - 通常是这样的: 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);

5 个答案:

答案 0 :(得分:5)

解决方案是将颜色分配为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);

答案 1 :(得分:1)

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


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

out put就是这样。


    Array
    (
        [0] => php
        [1] => file
        [2] => glob
        [3] => data
        [4] => http
        [5] => ftp
        [6] => zip
        [7] => compress.zlib
        [8] => https
        [9] => ftps
        [10] => compress.bzip2
        [11] => phar
    )

这里数组元素8th显示https已启用。如果您的代码不可用。找到php.ini文件并在那里放置以下行。

<强>延长= php_openssl.dll

在重新启动服务器之后,您的功能将与facebook资源网址一起使用。

答案 2 :(得分:0)

我尝试以下代码,对我来说效果很好。


    $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");


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

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

另外,黑色填充物将在那里。这里你没有将它应用于“marker-shadow.png”文件对象

答案 3 :(得分:0)

挣扎了一段时间,这里的答案都没有完全帮助我。下面是试图在透明PNG上打击JPG时完美运行的代码(注意“// !!! * ”评论,它们很重要):

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

答案 4 :(得分:0)

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