裁剪圆形图像并与具有透明背景的标记合并
Crop cirlcle image and merge with a marker with transparent bg
我正在尝试裁剪方形图像并将其与标记合并,但我无法使复制的圆形图像透明。
当将圆形图像保存为 PNG 时,它显示它的角是透明的,但是当我在 Photoshop 中打开它时,它的角上有一个白色,如下面的最终图像。
这是我使用的代码:
//SAVED THE CIRCLE PNG IMAGE
$width = 320;
$height = 320;
$img1 = '';
switch($fileExt){
case '.png':
$img1 = ImageCreateFrompng($img= $image_config['new_image']);
break;
case '.jpg':
$img1 = ImageCreateFromjpeg($img= $image_config['new_image']);
break;
case '.gif':
$img1 = ImageCreateFromgif($img= $image_config['new_image']);
break;
}
$x=$width ;
$y=$height;
$img2 = imagecreatetruecolor($x, $y);
$bg = imagecolorallocate($img2, 255, 255, 255);
imagefill($img2, 0, 0, $bg);
$e = imagecolorallocate($img2, 0, 0, 0);
$r = $x <= $y ? $x : $y;
imagefilledellipse ($img2, ($x/2), ($y/2), $r, $r, $e);
imagecolortransparent($img2, $e);
imagecopymerge($img1, $img2, 0, 0, 0, 0, $x, $y, 100);
imagecolortransparent($img1, $bg);
header("Content-type: image/png");
imagepng($img1, './img/deviceImg/pin'.$datetime.'.png');
imagedestroy($img2); // kill mask first
imagedestroy($img1); // kill canvas last
//MERGING IT WITH THE PIN
$width = 320;
$height = 320;
$image_1 = imagecreatefrompng('./img/deviceImg/pin.png');
imagesavealpha($image_1, true);
imagealphablending($image_1, true);
$image_2 = imagecreatefrompng('./img/deviceImg/pin'.$datetime.'.png');
imagesavealpha($image_2, true);
imagealphablending($image_2, true);
imagecopy($image_1, $image_2, 40, 22, 0, 0, $width, $height);
imagepng($image_1, './img/deviceImg/pinASD'.$datetime.'.png');
仅使用 imagecopymerge(), not imagecopy() 复制透明度。所以你的倒数第二行:
imagecopy($image_1, $image_2, 40, 22, 0, 0, $width, $height);
应改为:
imagecopymerge($image_1, $image_2, 40, 22, 0, 0, $width, $height, 100);
注意末尾的额外参数 (pct
)。根据手册:
The two images will be merged according to pct which can range from 0 to 100. When pct = 0, no action is taken, when 100 this function behaves identically to imagecopy() for pallete images, except for ignoring alpha components, while it implements alpha transparency for true colour images.
我正在尝试裁剪方形图像并将其与标记合并,但我无法使复制的圆形图像透明。
当将圆形图像保存为 PNG 时,它显示它的角是透明的,但是当我在 Photoshop 中打开它时,它的角上有一个白色,如下面的最终图像。
这是我使用的代码:
//SAVED THE CIRCLE PNG IMAGE
$width = 320;
$height = 320;
$img1 = '';
switch($fileExt){
case '.png':
$img1 = ImageCreateFrompng($img= $image_config['new_image']);
break;
case '.jpg':
$img1 = ImageCreateFromjpeg($img= $image_config['new_image']);
break;
case '.gif':
$img1 = ImageCreateFromgif($img= $image_config['new_image']);
break;
}
$x=$width ;
$y=$height;
$img2 = imagecreatetruecolor($x, $y);
$bg = imagecolorallocate($img2, 255, 255, 255);
imagefill($img2, 0, 0, $bg);
$e = imagecolorallocate($img2, 0, 0, 0);
$r = $x <= $y ? $x : $y;
imagefilledellipse ($img2, ($x/2), ($y/2), $r, $r, $e);
imagecolortransparent($img2, $e);
imagecopymerge($img1, $img2, 0, 0, 0, 0, $x, $y, 100);
imagecolortransparent($img1, $bg);
header("Content-type: image/png");
imagepng($img1, './img/deviceImg/pin'.$datetime.'.png');
imagedestroy($img2); // kill mask first
imagedestroy($img1); // kill canvas last
//MERGING IT WITH THE PIN
$width = 320;
$height = 320;
$image_1 = imagecreatefrompng('./img/deviceImg/pin.png');
imagesavealpha($image_1, true);
imagealphablending($image_1, true);
$image_2 = imagecreatefrompng('./img/deviceImg/pin'.$datetime.'.png');
imagesavealpha($image_2, true);
imagealphablending($image_2, true);
imagecopy($image_1, $image_2, 40, 22, 0, 0, $width, $height);
imagepng($image_1, './img/deviceImg/pinASD'.$datetime.'.png');
仅使用 imagecopymerge(), not imagecopy() 复制透明度。所以你的倒数第二行:
imagecopy($image_1, $image_2, 40, 22, 0, 0, $width, $height);
应改为:
imagecopymerge($image_1, $image_2, 40, 22, 0, 0, $width, $height, 100);
注意末尾的额外参数 (pct
)。根据手册:
The two images will be merged according to pct which can range from 0 to 100. When pct = 0, no action is taken, when 100 this function behaves identically to imagecopy() for pallete images, except for ignoring alpha components, while it implements alpha transparency for true colour images.