我在图像像素循环中做错了什么?

use*_*241 2 php gd image pixel libraries

<?php

$img = imagecreatefrompng("cuack.png");

$imagew = imagesx($img);
$imageh = imagesy($img);

$width = array();
$heigth = array();          
$x = 0;
$y = 0;

for ($x = 0; $x <= $imagew; $x++) {

    $rgba = imagecolorat($img,$x,1);
    $alpha = ($rgba & 0x7F000000) >> 24;

    var_dump($alpha);
}

for ($x = 0; $x <= $imageh; $x++) {

}
Run Code Online (Sandbox Code Playgroud)

我正在尝试检查图像中的每个像素是否有透明像素,但我收到以下错误:

注意:imagecolorat()[function.imagecolorat]:1920,1在第18行的C:\ www\index.php中超出范围

Joe*_*oey 6

边界从0开始,因此在每个方向上延伸到宽度 -1和高度 -1.因此,<= $imagew需要< $imagew.同样地<= $imageh.

宽度和高度只是告诉您有多少行和列的像素,而不是最大的行或列索引(低一个).

要遍历整个图像,只需使用两个嵌套循环:

for ($y = 0; $y < $imageh; $y++) {
  for ($x = 0; $x < $imagew; $x++) {
    // do whatever you want with them in here.
  }
}
Run Code Online (Sandbox Code Playgroud)