【问题标题】:imagecopymerge get white background instead of blackimagecopymerge 获得白色背景而不是黑色
【发布时间】:2015-07-13 13:25:17
【问题描述】:

我需要将两个图像合并在一起并向其中添加文本。但是当代码输出我得到黑色背景。当我删除“imagecopymerge”功能时,我看到了白色背景。

$firstUrl = '1.jpg';   
$secondUrl = "2.jpg";
$a  = getimagesize ($firstUrl);    

$x = 320;
$y = $a[1]+88;

$image = imagecreatetruecolor($x, $y);
$white = imagecolorallocate($image, 255, 255, 255);

imagefilledrectangle($image, 0, 0, $x, $y, $white);     

$first = imagecreatefromjpeg($firstUrl);
$second = imagecreatefromjpeg($secondUrl);

imagecopymerge($image,$first,0,0,0,0, $x, $y,100);
imagecopymerge($image,$second,$a[0]-70,$a[1]+10,0,0, 60, 60,100);    

$text_color = imagecolorallocate($image, 51, 51, 51);
$font = 'a.ttf';
imagettftext($image, 12, 0, 10, $a[1]+30, $text_color, $font, "text");

header("Content-Type: image/png");
imagepng($image); 

【问题讨论】:

    标签: php


    【解决方案1】:

    换行,imagecopymerge($image,$first,0,0,0,0, $x, $y,100);像 imagecopymerge($image,$first,100,100,0,0, $x, $y,100); 那么它应该可以工作了。

    【讨论】:

      【解决方案2】:

      使用要插入的图片的宽度和高度:

      imagecopymerge($image,$first,0,0,0,0,imagesx($first),imagesy($first),100);
      

      $x 和$y 是最终图像的尺寸。如果$first 小于此值,则imagecopymerge() 用黑色填充缺失区域。

      通过使用imagesx() 和imagesy(),您可以复制图像的确切区域。

      【讨论】:

        猜你喜欢
        • 2013-09-18
        • 2021-04-28
        • 1970-01-01
        • 2019-12-13
        • 1970-01-01
        • 1970-01-01
        • 2014-01-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多