PHP/GD,如何将圆从一个图像复制到另一个图像?
有没有一种简单的方法将圆形区域从一个图像资源复制到另一个图像资源?类似的东西,除了圆形或椭圆形等?PHP/GD,如何将圆从一个图像复制到另一个图像?,php,image,gd,selection,geometry,Php,Image,Gd,Selection,Geometry,有没有一种简单的方法将圆形区域从一个图像资源复制到另一个图像资源?类似的东西,除了圆形或椭圆形等? 如果可能的话,我希望避免使用预先创建的图像文件(任何椭圆形都应该是可能的),如果涉及到透明颜色,它们应该自然地保留图像的其余部分 我问的原因是,我有几个类可以在图像的“选定区域”内应用图像操作,其工作原理是首先从图像副本中删除该区域,然后将副本覆盖回原始图像。但是,如果要选择一个矩形,然后在该矩形内取消选择一个圆,并使操作仅影响剩下的区域,该怎么办?您可以尝试以下方法: 从原始图像开始-$img
如果可能的话,我希望避免使用预先创建的图像文件(任何椭圆形都应该是可能的),如果涉及到透明颜色,它们应该自然地保留图像的其余部分 我问的原因是,我有几个类可以在图像的“选定区域”内应用图像操作,其工作原理是首先从图像副本中删除该区域,然后将副本覆盖回原始图像。但是,如果要选择一个矩形,然后在该矩形内取消选择一个圆,并使操作仅影响剩下的区域,该怎么办?您可以尝试以下方法:
“3.在圆/椭圆中创建所需区域的蒙版png图像(黑色图像,上面有白色形状,黑色设置为alpha透明度的颜色)-$mask.4.在$firstcopy顶部复制$mask,保持alpha透明度。”我现在不是有一个黑色或白色圆的原始图像吗?或者是一个透明的黑色或白色圆圈的图像?另外,如果选择本身有黑色或白色,那么在最后阶段再次合并所有内容时,这不会出错吗?@MSpreij我添加了一个代码示例,详细说明了您需要执行的操作。。。我已经测试过了,知道它能用在牙床上!今晚晚些时候我会尝试。出现错误:
for($I=0;$I>16)&0xFF代码>,缺少部分脚本,请参阅。phalacee的回答解决了您的问题吗?这个页面被一个用户指向,所以最好知道它是否有效。
// 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);