【问题标题】:How to create an image with transparent background如何创建具有透明背景的图像
【发布时间】:2017-10-04 20:13:55
【问题描述】:

如何使用 GDlib 创建具有透明背景的图像?

header('content-type: image/png');

$image = imagecreatetruecolor(900, 350);

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

$text_color = imagecolorallocate($image, 0, 51, 102);
imagestring($image,2,4,4,'Test',$text_color);

imagepng($image);
imagedestroy($image);

这里的背景是黑色的

【问题讨论】:

  • 你必须使用 imagefill 并用分配的颜色(imagecolorallocatealpha)填充它,alpha 设置为 0。
  • @noice,创建一个答案.. 它有效
  • @NoICE:分配颜色不是必须的,是真彩色图像。

标签: php gdlib


【解决方案1】:

添加一行

imagefill($image,0,0,0x7fff0000);

imagestring 之前的某个位置,它将是透明的。

0x7fff0000 分解为:

alpha = 0x7f
red = 0xff
green = 0x00
blue = 0x00

它是完全透明的。

【讨论】:

  • 你让我很开心 :) 谢谢!
【解决方案2】:

这样的……

$im = @imagecreatetruecolor(100, 25);
# important part one
imagesavealpha($im, true);
imagealphablending($im, false);
# important part two
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
# do whatever you want with transparent image
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, $font - 3, $lime, "captcha.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);

【讨论】:

  • 这使我的透明背景变成白色而不是黑色:(
  • 我想生成一个尺寸等于文本长度的图像,我该如何实现?没有给函数@imagecreatetruecolor(100, 25) 提供静态参数(100,25);
【解决方案3】:

您必须使用 imagefill() 并用分配的颜色 (imagecolorallocatealpha()) 填充它,并将 alpha 设置为 0。

正如@mvds 所说,“不需要分配”,如果是真彩色图像(24 位或 32 位),它只是一个整数,因此您可以将该整数直接传递给 imagefill()

当您调用imagecolorallocate() 时,PHP 在后台对真彩色图像所做的事情是相同的——它只是返回计算得到的整数。

【讨论】:

    【解决方案4】:

    这应该可行:

    $img = imagecreatetruecolor(900, 350);
    
    $color = imagecolorallocatealpha($img, 0, 0, 0, 127); //fill transparent back
    imagefill($img, 0, 0, $color);
    imagesavealpha($img, true);
    

    【讨论】:

      【解决方案5】:

      这应该可以。它对我有用。

      $thumb = imagecreatetruecolor($newwidth,$newheight);
      $transparent = imagecolorallocatealpha($thumb, 0, 0, 0, 127);
      imagefill($thumb, 0, 0, $transparent);
      imagesavealpha($thumb, true);
      imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
      imagepng($thumb, $output_dir);
      

      【讨论】:

      • 不鼓励仅使用代码回答,因为它们没有为未来的读者提供太多信息,请对您所写的内容提供一些解释
      • 函数imagesavealpha() 拯救了我的一天。文档说:sets the flag to attempt to save full alpha channel information (as opposed to single-color transparency) when saving PNG images
      【解决方案6】:

      有时由于 PNG 图像的问题,您将无法获得透明图像。图片应采用以下推荐格式之一:

      PNG-8 (recommended)
      Colors: 256 or less
      Transparency: On/Off
      GIF
      Colors: 256 or less
      Transparency: On/Off
      JPEG
      Colors: True color
      Transparency: n/a
      

      imagecopymerge 函数无法正确处理 PNG-24 图像;因此不推荐。

      如果您使用 Adob​​e Photoshop 创建水印图像,建议您使用“Save for Web”命令并进行以下设置:

      File Format: PNG-8, non-interlaced
      Color Reduction: Selective, 256 colors
      Dithering: Diffusion, 88%
      Transparency: On, Matte: None
      Transparency Dither: Diffusion Transparency Dither, 100%
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-17
        • 1970-01-01
        • 1970-01-01
        • 2011-11-26
        • 2010-11-03
        • 1970-01-01
        • 1970-01-01
        • 2012-05-26
        相关资源
        最近更新 更多