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

发布于 2024-07-26 11:12:03 字数 368 浏览 4 评论 0原文

是否有一种相当简单的方法将圆形区域从一个图像资源复制到另一个图像资源? 类似 imagecopymerge 的东西,除了圆形或椭圆形等?
如果可能的话,我想避免使用预先创建的图像文件(任何椭圆形都可以),并且如果涉及透明颜色,它们自然应该保留图像的其余部分。

我问的原因是,我有一些类允许在图像的“选定区域”内应用图像操作,其工作原理是首先从图像副本中删除该区域,然后将副本覆盖回原始图像上。 但是,如果您想选择一个矩形,然后在其中取消选择一个圆形,并且操作仅影响剩下的区域,该怎么办?

Is there a reasonably straightforward way to copy a circular area from one image resource to another? Something like imagecopymerge except with circles or ovals etc?
If possible, I want to avoid having to use pre-created image files (any oval shape should be possible), and if there's transparency colours involved they should naturally leave the rest of the image alone.

Reason I'm asking, I have a few classes that allow to apply image operations inside a "selected area" of an image, which works by first deleting that area from a copy of the image, then overlaying the copy back on the original. But what if you want to select a rectangle, and then inside that deselect a circle, and have the operations only affect the area that's left?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扶醉桌前 2024-08-02 11:12:03

您可以尝试以下操作:

  1. 从原始图像开始 - $img
  2. 将该图像复制为 png - $copy
  3. 在圆形/椭圆形中创建所需区域的遮罩 png 图像(带有黑色形状的“magicpink”图像,带有黑色设置为 alpha 透明度的颜色) - $mask
  4. 将 $mask 复制到 $copy 的顶部,保持 Alpha 透明度
  5. 更改 $copy 上需要的内容
  6. 将 $copy 复制回 $img 上,保持 Alpha 透明度

    // 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);  

You could try this:

  1. Start with original image - $img
  2. Copy that image to a png - $copy
  3. Create a mask png image of the area you want in the circle/ellipse (a 'magicpink' image with a black shape on it, with black set to the colour of alpha transparency) - $mask
  4. Copy $mask over the top of $copy maintaining the Alpha transparency
  5. Change what you need to on $copy
  6. Copy $copy back over $img maintaining the Alpha transparency

    // 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);  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文