如何在 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
       }
   }
 }