Php check if image is greyscale function memory leak -


i'm using function check if image greyscale or not, file paths , names loaded in correctly , runs fine.

however it's started give usual memory exhausted errors i'm wondering causing this?

fatal error: allowed memory size of 134217728 bytes exhausted (tried allocate 32 bytes) in ... on line 51

the line 51 $b[$i][$j] = $rgb & 0xff;

how can optimize function use less memory, possibly half image work out average or if average high?

function checkgreyscale(){      $imginfo = getimagesize($this->filelocation);      $width = $imginfo[0];     $height = $imginfo[1];      $r = array();     $g = array();     $b = array();      $c = 0;      ($i=0; $i<$width; $i++) {          ($j=0; $j<$height; $j++) {              $rgb = imagecolorat($this->file, $i, $j);              $r[$i][$j] = ($rgb >> 16) & 0xff;             $g[$i][$j] = ($rgb >> 8) & 0xff;             $b[$i][$j] = $rgb & 0xff;              if ($r[$i][$j] == $g[$i][$j] && $r[$i][$j] == $b[$i][$j]) {                 $c++;             }         }     }      if ($c == ($width * $height))         return true;     else         return false; } 

you sure need whole table in memory?

not tested quickie:

function checkgreyscale(){      $imginfo = getimagesize($this->filelocation);      $width = $imginfo[0];     $height = $imginfo[1];      $r = $g = $b = 0; // defaulting 0 before loop      $c = 0;      ($i=0; $i<$width; $i++) {          ($j=0; $j<$height; $j++) {              $rgb = imagecolorat($this->file, $i, $j);              // less memory usage, quite need             $r = ($rgb >> 16) & 0xff;             $g = ($rgb >> 8) & 0xff;             $b = $rgb & 0xff;              if( !($r == $g && $r == $b)) { // if not greyscale?                 return false; // stop proceeding ;)             }         }     }      return true; } 

this way instead of storing image bytes in memory, prolly more doubling memory usage, using actual set of bytes you're running calculations on. should work far donn't load image exceeding php memory limit @ all.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -