2012-10-28 50 views
2

請幫我算一下圖像中的像素數量,或者放出RGB數組。如何計算圖像中的像素數量(php)

所以這是腳本泰德給我從陣列的一個元件:

<?php 
    $img = "1.png"; 
    $imgHand = imagecreatefrompng("$img"); 
    $imgSize = GetImageSize($img); 
    $imgWidth = $imgSize[0]; 
    $imgHeight = $imgSize[1]; 
    echo '<img src="'.$img.'"><br><br>'; 
    for ($l = 0; $l < $imgHeight; $l++) { 
     for ($c = 0; $c < $imgWidth; $c++) { 
      $pxlCor = ImageColorAt($imgHand,$c,$l); 
      $pxlCorArr = ImageColorsForIndex($imgHand, $pxlCor); 
     } 
    } 


     print_r($pxlCorArr); 
?> 

抱歉,我的英語我從烏克蘭

+4

'$ numOfPix = $ imgWidth * $ imgHeight;' – Petah

+0

你究竟在做什麼?圖像中的像素總數,如imgwidth x imageheight或什麼? – Graviton

+1

我相信他想要一個整個圖像的'$ pxlCorArr',其中索引是像素,顏色值..否則他的代碼沒有任何意義。 – dbf

回答

5

像素的圖像中的數字僅僅是高度乘以寬度。

不過,我認爲這是你想要的東西:

<?php 
    $img = "1.png"; 
    $imgHand = imagecreatefrompng("$img"); 
    $imgSize = GetImageSize($img); 
    $imgWidth = $imgSize[0]; 
    $imgHeight = $imgSize[1]; 
    echo '<img src="'.$img.'"><br><br>'; 

    // Define a new array to store the info 
    $pxlCorArr= array(); 

    for ($l = 0; $l < $imgHeight; $l++) { 
     // Start a new "row" in the array for each row of the image. 
     $pxlCorArr[$l] = array(); 

     for ($c = 0; $c < $imgWidth; $c++) { 
      $pxlCor = ImageColorAt($imgHand,$c,$l); 

      // Put each pixel's info in the array 
      $pxlCorArr[$l][$c] = ImageColorsForIndex($imgHand, $pxlCor); 
     } 
    } 

    print_r($pxlCorArr); 
?> 

這將存儲所有的像素數據在pxlCorpxlCorArr數組,然後你就可以操縱你想要的輸出圖像。

該陣列是一個二維陣列,這意味着你可以用$pxlCorArr[y][x]來引用一個單獨的像素,起始於[0][0]

+0

Thx你!其工作非常出色! –

+0

@VolodyaDaniliv我更新它將數據存儲在二維數組中,以便您可以使用數組中的一種x,y座標引用各個像素。不知道這是你想要的,但現在你知道它是如何完成的,我相信你可以弄明白。 – sachleen

+0

@sachleen排序x,y? :) – dbf