【问题标题】:Join Multiple transparent png image in Single image into php将单个图像中的多个透明png图像加入php
【发布时间】:2013-04-11 12:02:06
【问题描述】:

朋友们,我想从多个透明 PNG 图像生成一个 png 图像,但问题是我只能生成最后一个图像

两张图片不能合并。

我的代码如下

$x = 363;
$y = 267;

$im_dest = imagecreatetruecolor ($x, $y);
imagealphablending($im_dest, false);

$im = imagecreatefrompng('2.png');
$im1 = imagecreatefrompng('1.png');

imagecopy($im_dest, $im1, 0, 0, 0, 0, $x, $y);
imagecopy($im_dest, $im, 0, 0, 0, 0, $x, $y);

imagesavealpha($im_dest, true);
imagepng($im_dest, 'small_redfade.png');

这些是我用来加入单个图像的图像

http://s11.postimg.org/h6lui7yjn/image.png

http://s21.postimg.org/o7zdnwcnb/image.png

【问题讨论】:

    标签: php image-processing gd gdlib imagecreatefrompng


    【解决方案1】:

    这是有效的代码:

    $width = 210;
    $height = 190;
    
    $layers = array();
    $layers[] = imagecreatefrompng("img/01_boy_faceB.png");
    $layers[] = imagecreatefrompng("img/01_boy_hairB.png");
    
    $image = imagecreatetruecolor($width, $height);
    
    // to make background transparent
    imagealphablending($image, false);
    $transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
    imagefill($image, 0, 0, $transparency);
    imagesavealpha($image, true);
    
    /* if you want to set background color
    $white = imagecolorallocate($image, 255, 255, 255);
    imagefill($image, 0, 0, $white);
    */
    
    imagealphablending($image, true);
    for ($i = 0; $i < count($layers); $i++) {
        imagecopy($image, $layers[$i], 0, 0, 0, 0, $width, $height);
    }
    imagealphablending($image, false);
    imagesavealpha($image, true);
    
    imagepng($image, 'final_img.png');
    
    ?>
    

    【讨论】:

      【解决方案2】:

      ImageMagick::Composite 可以解决这个问题,遗憾的是在 GD 中没有这样做,所以让其他人解释如何在那里进行。

      类似:

      <?php
      
      $firstImage = new Imagick("firstImage.png");
      $secondImage = new Imagick("secondImage.png");
      
      $firstImage->compositeImage($secondImage, Imagick::COMPOSITE_COPYOPACITY, 0, 0 );
      
      header('Content-type: image/png');
      echo $firstImage;
      
      ?>
      

      这应该保留 alpha。

      【讨论】:

      • 嗨,我下载了 Imagick dll 并按照我遵循的所有步骤将其安装在 wamp 上,但是虽然我得到了 Imagick 类未找到的致命错误,但首先我得到了 php_imagik.dll 不是有效的 win32 应用程序, 请给我建议
      • 我尝试了这一步,但有一些与放置 imagemagic 二进制文件相关的混淆,将 ImageMagick 二进制文件夹添加到您的 PATH 分钟,我应该添加哪个路径
      • PATH 是一个环境变量,它告诉 Windows 可以从哪里加载。在 Google 上搜索“ PATH”以找到它,它只是要从中读取二进制文件的文件夹列表。
      猜你喜欢
      • 2011-06-02
      • 1970-01-01
      • 2010-11-26
      • 2012-03-06
      • 2013-12-15
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 2018-10-09
      相关资源
      最近更新 更多