Correct way to get image pixel data in PHP? -


i trying image pixel data in php way in js canvas

ctx.getimagedata(); 

my php function image pixel data:

        function canvasgetimagedata($canvas) {             $imagewidth = imagesx($canvas);             $imageheight = imagesy($canvas);              $imgdata = array();             // loop on each single pixel.             ($y = 0; $y < $imageheight; $y++) {                 ($x = 0; $x < $imagewidth; $x++) {    $colorinfo = imagecolorsforindex($canvas, imagecolorat($canvas, $x, $y)); // line #44                      array_push($imgdata,$colorinfo['red']);                     array_push($imgdata,$colorinfo['green']);                     array_push($imgdata,$colorinfo['blue']);                     array_push($imgdata,$colorinfo['alpha']);             }         }         return $imgdata;     } 

i call

$mglink = 'image link'; $canvas = imagecreatefrompng($mglink); $imgdata = canvasgetimagedata($canvas); 

i have 1 image of resolution 4487 x 2864 when supply function memory error line #44

allowed memory size of 1073741824 bytes exhausted (tried allocate 24 bytes)

so wondering there better way image pixel data , had no problem of getting image pixel data javascript's getimagedata() function , front end script big on had memory issues have decided move portion server side. ,

php on server side allows allocate amount of memory process @ given time. using 1,073,741,824 bytes that's 1 gigabyte of data. remember you're page in utf8, every character uses 2 bytes in memory. client handle because there's more memory available , no limitations.

do basic calculations me:

4487 x 2864 = 12,850,000 pixels. 

you storing 4 values in array take lot of space. 255,255,255,1 takes 10 spaces (times 2) 20 times amount of pixels, equates 256 megabytes of data, add amount needed keys (red, green, blue , alpha) , run out of memory.

that's why picture viewers use optimized algorithms show big (raw) files (not talking compression) jpeg. try opening raw file in notepad (it tell ran out of memory).

so image pixel data need (a clipped) version of image , calculate rest when panned to. , there lot of people out there have better solutions/algorithms this.

******************************** *                              * *         ============         * *         =          =         *  = -> calculated bit. *         ============         * ******************************** total image 

reading @ http://php.net/manual/en/function.imagecolorat.php

you want store pixels in array want edit, need know location, can access colour data , transparency later.

another way save memory cut out middle man; imagecolorsforindex seems redundant. have 4 values in array. in end array length should divisible four. cutting out function stores rgb values array, , that's doing.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -