首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在PHP中检测图像的蒙版区域?

如何在PHP中检测图像的蒙版区域?
EN

Stack Overflow用户
提问于 2021-07-21 22:59:23
回答 1查看 25关注 0票数 0

我有一些图像,它们中的每一个都有一个相同大小的遮罩(透明)矩形区域。如何检测这些区域的坐标?我搜索了库,但没有一个库能做到这一点。我想把我的二维码而不是蒙面部分放进去。

EN

回答 1

Stack Overflow用户

发布于 2021-07-21 23:21:12

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

代码语言:javascript
运行
复制
$src        = imagecreatefrompng("your-image.png");
$trans      = imageColorAllocateAlpha($src, 0, 0, 0, 127);
$transArray = imagecolorsforindex($src, $trans);

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

代码语言:javascript
运行
复制
$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
       }
   }
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68471880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档