2012-03-23 64 views
3

我想把一个jpeg放在一个PNG - 其中PNG有alpha透明度。PHP合并与alpha透明png顶部的jpeg

的前景图像是在这里: 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); 
+0

确保您已在$ canvas图片上启用了alpha。 – 2012-03-23 15:16:44

+0

Halfer。我不是故意讽刺的:-)但是我确实需要一些方向来解决我以前的问题。我总共提出了6个问题。一个没有答案。删除。拿这个问题:806133.最合理的答案是我自己的 - 但它是一种解决方法。我应该接受我自己的答案吗?或者别人没有足够的答案? – nitech 2012-03-26 06:54:41

回答

5

解决的办法是分配一个颜色为100%的α透明然后画一个正方形基本图像的整个画布上:

// 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因为它解决了我的问题 – Blizz 2012-07-11 11:35:50

+0

对我来说,解决这个问题的第8行是“imagealphablending($ base_image,true);”在透明度层之后。谢谢。 – Robbie 2014-06-17 04:44:59

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”文件对象

+0

由于某些原因,当我尝试首先创建新图像画布时,这不起作用 - 请参阅我的代码 - 通过运行imagecreatetruecolor(..)bg仍然是黑色的。 – nitech 2012-03-23 22:19:12

+0

我检查你的代码(fbimage.php)它的工作正常。唯一的问题是你从https url加载facebook thum。为了使它工作,你必须在php.ini文件中启用https包装。但对于本地文件,它工作正常。我会把指令启用https封装作为另一个答案。 – sugunan 2012-03-24 03:27:57

+0

是的 - fbimage.php也适用于我。 fbimage.php和我发布的第一个代码的不同之处在于第一个代码使用imagecreatetruecolor作为基本画布,而fbimage.php使用png作为模板和imagecreatefrompng。 是否有一些代码我应该适用于imagecreatetruecolor对象以使它的行为方式与png模板相同? – nitech 2012-03-24 19:04:12

1

运行下面的php脚本并查看天气https可用的数组集。

 

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

输出将是这样的。

 

    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资源URL工作,即使。

+0

它产生如下: 阵列 ( [0] => HTTPS [1] => FTPS [2] =>压缩。 ZLIB [3] => compress.bzip2 [4] => PHP [5] =>文件 [6] =>水珠 [7] =>数据 [8] => HTTP [9] = > ftp [10] => phar [11] => zip ) – nitech 2012-03-24 19:00:01

0

与此挣扎了一段时间,没有一个答案这里帮助我完全。下面是我在尝试拍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

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