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

时间:2012-01-11 15:35:39

标签: php image gd libraries pixel

<?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++) {

}

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

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

1 个答案:

答案 0 :(得分:6)

边界从0开始,因此在每个方向上延伸到 width - 1和 height - 1。因此,<= $imagew必须为< $imagew。同样适用于<= $imageh

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

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

for ($y = 0; $y < $imageh; $y++) {
  for ($x = 0; $x < $imagew; $x++) {
    // do whatever you want with them in here.
  }
}
相关问题