PHP GD:裁剪多边形:适用于某些图像,但不适用于某些图像

发布于 2024-12-22 12:01:44 字数 722 浏览 4 评论 0原文

我想裁剪出从 Google Maps Static API 保存的图像的多边形(我有一个透明区域)。 然后我创建了第二个图像,保存它并在其上尝试了相同的脚本,但不同之处在于第二个尝试的图像没有效果/更改,但它是相同的图像。也许是 PHP 错误? 我正在使用 PHP 5.3.3。

<?php
$image = imagecreatefrompng('map.png');
$image2 = imagecreatefrompng('map2.png');


$black = imagecolorallocatealpha($image, 0, 0, 0, 127);
$black2 = imagecolorallocatealpha($image2, 0, 0, 0, 127);


imagefilledpolygon($image, array(0,0, 20,20, 0,20), 3, $black);
imagefilledpolygon($image2, array(0,0, 20,20, 0,20), 3, $black2);

header('Content-Type: image/png');
imagepng($image);
#imagepng($image2);
?>

图片1: 1

图片 2: 2

I want to crop out a polygon (that I have there a transparent area) of an image I saved from Google Maps Static API.
Then I created a second image, saved it and tried also the same script on it but with the difference that the was no effect/change on the second tried image but it's the same image. Maybe a PHP bug?
I'm using PHP 5.3.3.

<?php
$image = imagecreatefrompng('map.png');
$image2 = imagecreatefrompng('map2.png');


$black = imagecolorallocatealpha($image, 0, 0, 0, 127);
$black2 = imagecolorallocatealpha($image2, 0, 0, 0, 127);


imagefilledpolygon($image, array(0,0, 20,20, 0,20), 3, $black);
imagefilledpolygon($image2, array(0,0, 20,20, 0,20), 3, $black2);

header('Content-Type: image/png');
imagepng($image);
#imagepng($image2);
?>

Image 1:
1

Image 2:
2

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

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

发布评论

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

评论(1

┊风居住的梦幻卍 2024-12-29 12:01:44

我找到了解决方案:您必须为图像设置 imagealphablending imagesavealpha 设置才能使此透明度发挥作用。问题还在于这些图像具有不同的位。工作图像为 8 位,而不工作图像为 24 位。

imagealphablending($image2, false);
imagesavealpha($image2, true);

这个评论对我帮助很大: http://www.php.net/manual /en/function.imagecreatefrompng.php#47083

I found the solution: You have to set imagealphablending and imagesavealpha settings for the images to get this transparency working. The problem also that these images have different bits. The working images had 8bit while the not working had 24bit.

imagealphablending($image2, false);
imagesavealpha($image2, true);

This comment helped me much: http://www.php.net/manual/en/function.imagecreatefrompng.php#47083

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