I am trying to get image pixel data in php the way we get in js canvas like
ctx.getImageData();
My php function to get image pixel data:
function CanvasGetImageData($canvas) {
$imageWidth = imagesx($canvas);
$imageHeight = imagesy($canvas);
$imgdata = array();
// Loop over each single pixel.
for ($y = 0; $y < $imageHeight; $y++) {
for ($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 am call it like
$mglink = 'image link';
$canvas = imagecreatefrompng($mglink);
$imgData = CanvasGetImageData($canvas);
I have one image of resolution 4487 x 2864 when I supply it to function it get memory error it line #44
Allowed memory size of 1073741824 bytes exhausted (tried to allocate 24 bytes)
so I am wondering is there any better way to get image pixel data , I had no problem of getting image pixel data with Javascript's getImageData()
function , however my front end script is big which over all had memory issues so I have decided to move some portion to server side. ,