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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP / GD,如何将圆圈从一个图像复制到另一个图像?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
是否有一种相当直接的方法形区域从一个图像资复制到另一个图像资源?像 imagecopymerge这样的东西,除了圆圈或椭圆形等?
如果可能的话,我想避免使用预先创建的图像文件(任何椭圆形状应该是可能的),如果涉及透明度颜色,他们应该自然地留下图像的其余部分.

原因我问,我有一些类允许在图像的“选定区域”内应用图像操作,其工作方式是首先从图像副本中删除该区域,然后将副本重叠在原始图像上.但是如果你想选择一个矩形,然后在里面取消选择一个圆圈,让操作只影响剩下的区域呢?

你可以试试这个:

>从原始图像开始 – $img
>将该图像复制到png – $copy
>在圆圈/椭圆中创建所需区域的蒙版png图像(“magicpink”图像上带有黑色形状,黑色设置为Alpha透明度的颜色) – $mask
>将$mask复制到$copy的顶部,保持Alpha透明度
>在$copy上更改您需要的内容
>将$copy复制回$img,保持Alpha透明度

// 1. Start wITh the original image  
    $img = imagecreatefromjPEg("./original.jpg");  
    $img_magicpink = imagecolorallocatealpha($img,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);  
    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);  
    $mask_magicpink = imagecolorallocate($mask,255);  
    imagecolortransparent($mask,$mask_black);  
    imagefill($mask,$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,$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);  
    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);  
        imagesetpixel($gray,$i,$j,$gray_color);  
      }  
    }  

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

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

脚本宝典总结

以上是脚本宝典为你收集整理的PHP / GD,如何将圆圈从一个图像复制到另一个图像?全部内容,希望文章能够帮你解决PHP / GD,如何将圆圈从一个图像复制到另一个图像?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。