【问题标题】:PHP Merge jpeg on top of png with alpha transparencyPHP在具有alpha透明度的png之上合并jpeg
【发布时间】:2012-04-08 03:43:29
【问题描述】:

我正在尝试将 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);

【问题讨论】:

  • 确保您在 $canvas 图像上也启用了 alpha。
  • 减半。我并不是故意要显得讽刺 :-) 不过,我确实需要一些关于我之前的问题的指导。我一共问了6个问题。一是没有答案。已删除。拿这个问题:806133。最合理的答案是我自己的——但这是一种解决方法。我应该接受自己的答案吗?还是别人的答案不够?

标签: php png gd2


【解决方案1】:

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

【讨论】:

  • 为我解决这个问题的是第 8 行“imagealphablending($base_image,true);”在透明层之后。谢谢。
【解决方案2】:

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

回声“
”;
    print_r(stream_get_wrappers());
    回声“
”;

输出会是这样的。

大批 ( [0] => php [1] => 文件 [2] => 全局 [3] => 数据 [4] => http [5] => ftp [6] => 拉链 [7] => 压缩.zlib [8] => https [9] => FTP [10] => 压缩.bzip2 [11] => 法尔 )

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

extension=php_openssl.dll

重启服务器后,你的函数甚至可以使用 facebook 资源 url。

【讨论】:

  • 它产生了以下结果:Array ( [0] => https [1] => ftps [2] => compress.zlib [3] => compress.bzip2 [4] => php [ 5] => 文件 [6] => glob [7] => 数据 [8] => http [9] => ftp [10] => phar [11] => zip )
【解决方案3】:

我尝试了以下代码,它对我很有效。

$宽度 = 400; $高度 = 400; $base_image = imagecreatefromjpeg("base.jpg"); $top_image = imagecreatefrompng("top.png"); 图像保存阿尔法($top_image,假); 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”文件对象

【讨论】:

  • 由于某种原因,当我尝试通过运行 imagecreatetruecolor(..) 首先创建一个新的图像画布时(参见我的代码),这不起作用。bg 仍然是黑色的。
  • 我检查了您的代码 (fbimage.php) 是否工作正常。唯一的问题是您从 https url 加载 facebook thum。要使其工作,您必须在 php.ini 文件上启用 https 包装器。但是对于本地文件,它工作正常。我会将启用 https 包装器的说明作为另一个答案。
  • 是的 - fbimage.php 也适用于我。与 fbimage.php 和我发布的第一个代码的区别(在我看来)是第一个代码使用 imagecreatetruecolor 作为基本画布,而 fbimage.php 使用 png 作为具有 imagecreatefrompng 的模板。是否有一些代码我应该应用于 imagecreatetruecolor 对象以使其行为与 png 模板相同?
  • 我检查了第一个脚本。对于所有透明 png,您必须应用以下代码。图像保存阿尔法($阴影,真); imagealphablending($shadow, true);否则将有黑色填充物。在这里,您没有将其应用于“marker-shadow.png”文件对象
【解决方案4】:

为此苦苦挣扎了一段时间,这里的答案都没有完全帮助我。以下是尝试在透明 PNG 上添加 JPG 时完美运行的代码(注意 "// !!! *" cmets,它们很重要):

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

【讨论】:

    【解决方案5】:
    // 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 2014-06-30
      • 2017-10-16
      • 1970-01-01
      • 2010-12-30
      相关资源
      最近更新 更多