【问题标题】:PHP imagecopy with transparent background具有透明背景的PHP图像复制
【发布时间】:2011-12-05 10:09:59
【问题描述】:

我使用此代码从另一个 png 图像创建图像,默认情况下背景为黑色。我的问题是如何设置透明背景?

$input = imagecreatefrompng('image.png');
$output = imagecreatetruecolor(50, 50);

imagecopy($output, $input, 4,0, 8,8, 8,8);
imagecopy... etc.

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

有没有一种简单的方法可以做到这一点?谢谢

【问题讨论】:

    标签: php png imagecreatefrompng


    【解决方案1】:

    在给定的图像中设置透明颜色。

    int imagecolortransparent ( resource $image [, int $color ] )
    

    这是link

    【讨论】:

    • 是的!这有效:$black = imagecolorallocate($output, 0, 0, 0); imagecolortransparent($output, $black); 在 imagecopy 之前
    • 如果我想让透明成为透明色怎么办?
    • 所有被复制的东西都会在目标图像上将其透明颜色绘制为黑色。我不想丢失源图像中已经是黑色的像素。
    【解决方案2】:

    由于 PHP 函数 imagecopymerge 不适用于 Alpha 通道,因此您需要使用此页面上第一条评论中的函数 imagecopymerge_alphahttp://php.net/manual/en/function.imagecopymerge.php

    以透明图片为基础,与你需要的图片合并即可。

    我已经尝试过了,它适用于我的一个项目。

    【讨论】:

    • 也为我工作。
    • 这对我不起作用,当我复制具有透明度的源图像时,源图像中的所有透明颜色都在目标图像上绘制为黑色。
    【解决方案3】:
    imagealphablending($input, true);
    imagesavealpha($input, true);
    
    imagealphablending($output, true);
    imagesavealpha($output, true);
    

    【讨论】:

    • 这是不正确的,弯曲应该设置为false,然后再运行imagesagealpha
    【解决方案4】:

    没有一个解决方案对我有用,它总是将源图像上的透明像素转换为目标图像上的黑色。有效的方法是将 imagecopy/imagecopymerge/imagecopymerge_alpha 更改为 imagecopyresampled 并且只传递相同的宽度和高度两次。

        //Create destination image.
        $png = imagecreatetruecolor(1024, 1024);
        imagealphablending($png, false);
        imagesavealpha($png, true);
        //Make destination image be all transparent.
        $color = imagecolorallocatealpha($png, 0, 0, 0, 127); //127 means completely transparent.
        imagefill($png, 0, 0, $color);
    
        //Load source image.
        $png2 = imagecreatefrompng($sourceurl);
        imagealphablending($png2, false);
        imagesavealpha($png2, true);
        $sizex = imagesx($png2);
        $sizey = imagesy($png2);
    
        //Copy to destination and save to file.
        imagecopyresampled( $png, $png2, 
        0, 0,
        0, 0, 
        $sizex, $sizey, 
        $sizex, $sizey);
        imagepng($png, "result.png");
    

    【讨论】:

      【解决方案5】:

      或者说

      int imagesavealpha($img,true);
      

      http://www.php.net/manual/en/function.imagesavealpha.php

      【讨论】:

      • imagesavealpha 只是设置在写出图像数据(写入文件或缓冲区)时是否应保存 alpha 通道信息
      【解决方案6】:

      全部功劳归于: http://consistentcoder.com/combine-a-transparent-png-image-on-top-of-another-image-with-php

      以下代码会将前景图像叠加到背景图像上,同时保留叠加层的透明度

      //set the source image (foreground)
      $sourceImage = 'table.png';
      
      //set the destination image (background)
      $destImage = 'create-a-surreal-head-of-tree-photo-manipulation.jpg';
      
      //get the size of the source image, needed for imagecopy()
      list($srcWidth, $srcHeight) = getimagesize($sourceImage);
      
      //create a new image from the source image
      $src = imagecreatefrompng($sourceImage);
      
      //create a new image from the destination image
      $dest = imagecreatefromjpeg($destImage);
      
      //set the x and y positions of the source image on top of the destination image
      $src_xPosition = 75; //75 pixels from the left
      $src_yPosition = 50; //50 pixels from the top
      
      //set the x and y positions of the source image to be copied to the destination image
      $src_cropXposition = 0; //do not crop at the side
      $src_cropYposition = 0; //do not crop on the top
      
      //merge the source and destination images
      imagecopy($dest,$src,$src_xPosition,$src_yPosition,
                $src_cropXposition,$src_cropYposition,
                $srcWidth,$srcHeight);
      
      //output the merged images to a file
      /*
       * '100' is an optional parameter,
       * it represents the quality of the image to be created,
       * if not set, the default is about '75'
       */
      imagejpeg($dest,
          'combine-a-transparent-png-image-on-top-of-another-image-with-php-01.jpg',
          100);
      
      //destroy the source image
      imagedestroy($src);
      
      //destroy the destination image
      imagedestroy($dest);
      

      【讨论】:

        猜你喜欢
        • 2012-05-26
        • 2021-08-24
        • 1970-01-01
        • 2015-05-15
        • 2014-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多