微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 PHP 中检测图像上的屏蔽区域?

如何解决如何在 PHP 中检测图像上的屏蔽区域?

我有一些图像,每个图像都有一个相同大小的遮罩(透明)矩形区域。如何检测这些区域的坐标?我搜索了图书馆,但没有一个这样做。我想把我的二维码而不是被屏蔽的部分。

解决方法

我会尝试(未测试)读取图像并使用透明颜色:

$src        = imagecreatefrompng("your-image.png");
$trans      = imageColorAllocateAlpha($src,127);
$transArray = imagecolorsforindex($src,$trans);

我会读取图像尺寸并像这样检查每个像素:

$width  = imagesx($src);
$height = imagesy($src);
for ($x = 0; $x < $width; $x++) {
   for ($y = 0; $y < $height; $y++) {
       $color  = imagecolorat($src,$x,$y);
       $colors = imagecolorsforindex($src,$color);
       if ($colors["alpha"] == $transArray["alpha"]) {
           // you have detected the first pixel of transparency
           // here you have to remember smallest $x and smallest $y
           // as well as biggest $x and biggest $y 
           // that should be your rectangle
       }
   }
 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。