PHP/GD,如何将一个圆形从一张图片复制到另一张图片?

6
有没有一种相对简单的方法,可以将一个圆形区域从一个图像资源复制到另一个图像资源中?类似于 imagecopymerge,但是可以是圆形或椭圆形等等吗?
如果可能的话,我想避免使用预先创建的图像文件(任何椭圆形状都应该是可能的),如果涉及透明颜色,则它们应该自然地保留其余部分的图像。
我提出这个问题的原因是,我有几个类,允许在图像的“选定区域”内应用图像操作,这是通过首先从图像的副本中删除该区域,然后将副本覆盖回原始图像来实现的。但是,如果您想选择一个矩形,然后在其中取消选择一个圆形,并且只影响剩余的区域,那该怎么办呢?

phalacee的答案解决了你的问题吗?这个页面正在被一个当前的问题所指向,所以知道它是否有效会很好。 - halfer
1个回答

8
您可以尝试以下操作:
  1. 从原始图像开始 - $img
  2. 将该图像复制到png格式中 - $copy
  3. 创建一个遮罩png图像,以圆形/椭圆形为区域(一个黑色形状的“magicpink”图像,其中黑色设置为alpha透明度颜色)- $mask
  4. 在保持Alpha透明度的情况下,将$mask复制到$copy上方
  5. 更改$copy上需要修改的内容
  6. 在保持Alpha透明度的情况下,将$copy再次复制回$img上

    // 1. Start with the original image  
    $img = imagecreatefromjpeg("./original.jpg");  
    $img_magicpink = imagecolorallocatealpha($img, 255, 0, 255, 127);  
    //imagecolortransparent($img, $img_magicpink);  

    // (Get its dimensions for copying)  
    list($w, $h) = getimagesize("./original.jpg");  

    // 2. Create the first copy  
    $copy = imagecreatefromjpeg("./original.jpg");  
    imagealphablending($copy, true);  

    $copy_magicpink = imagecolorallocate($copy, 255, 0, 255);  
    imagecolortransparent($copy, $copy_magicpink);  

    // 3. Create the mask  
    $mask = imagecreatetruecolor($w, $h);  
    imagealphablending($mask, true);  

    // 3-1. Set the masking colours  
    $mask_black = imagecolorallocate($mask, 0, 0, 0);  
    $mask_magicpink = imagecolorallocate($mask, 255, 0, 255);  
    imagecolortransparent($mask, $mask_black);  
    imagefill($mask, 0, 0, $mask_magicpink);  

    // 3-2. Draw the circle for the mask  
    $circle_x = $w/2;  
    $circle_y = $h/2;  
    $circle_w = 150;  
    $circle_h = 150;  
    imagefilledellipse($mask, $circle_x, $circle_y, $circle_w, $circle_h, $mask_black);  

    // 4. Copy the mask over the top of the copied image, and apply the mask as an alpha layer  
    imagecopymerge($copy, $mask, 0, 0, 0, 0, $w, $h, 100);  


    // 5. Do what you need to do to the image area  
    // My example is turning the original image gray and leaving the masked area as colour  
    $x = imagesx($img);  
    $y = imagesy($img);  
    $gray = imagecreatetruecolor($x, $y);  
    imagecolorallocate($gray, 0, 0, 0);  
    for ($i = 0; $i > 16) & 0xFF;  
        $g = ($rgb >> 8) & 0xFF;  
        $b = $rgb & 0xFF;  
         //for gray mode $r = $g = $b  
        $color = max(array($r, $g, $b));  
        $gray_color = imagecolorexact($img, $color, $color,   $color);  
        imagesetpixel($gray, $i, $j, $gray_color);  
      }  
    }  

    // 6. Merge the copy with the origianl - maintaining alpha  
    imagecopymergegray($gray, $copy, 0, 0, 0, 0, $w, $h, 100);  
    imagealphablending($gray, true);  
    imagecolortransparent($gray, $mask_magicpink);  

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

  1. 创建一个遮罩PNG图像,用于圆形/椭圆形中所需的区域(黑色图像上有白色形状,黑色设置为Alpha透明度的颜色)- $mask。4. 将$mask复制到$firstcopy的顶部,保持Alpha透明度。现在我是否拥有原始图像,其中包含黑色或白色圆圈?或者是一个透明图像,上面有黑色或白色圆圈?此外,如果选择本身有黑色或白色,那么在最终合并所有内容时,不会出现问题吗?
- MSpreij
@MSpreij 我已经添加了一个代码示例,详细说明了你需要做什么...我已经测试过它并知道它可以工作。 - jsnfwlr
1
出现了错误:for ($i = 0; $i > 16) & 0xFF;,脚本的一部分丢失,请参见博客文章 - x-yuri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接