1

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. ,

user889030
  • 2,795
  • 2
  • 29
  • 41
  • Try to add this top of your script: `set_time_limit(0);` and `ini_set('memory_limit', '-1');` – Chandra Kumar Aug 18 '17 at 04:54
  • Possible duplicate of [Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php](https://stackoverflow.com/questions/415801/allowed-memory-size-of-33554432-bytes-exhausted-tried-to-allocate-43148176-byte) – Gino Mempin Aug 18 '17 at 04:57
  • no it still not working , getting : Out of memory (allocated 1474560000) (tried to allocate 134217728 bytes) – user889030 Aug 18 '17 at 05:00
  • my php.ini memory_limit=2G , and time is 300 seconds but same issue – user889030 Aug 18 '17 at 05:05

1 Answers1

1

PHP on the server side only allows you to allocate a certain amount of memory to a process at any given time. You are using 1,073,741,824 bytes That's one gigabyte of data. Remember that you're page is probably in UTF8, so every character uses two bytes in memory. Your client could handle this because there's more memory available and no limitations.

Do some basic calculations with me:

4487 x 2864 = 12,850,000 pixels.

You are storing 4 values in an array that take up a lot of space. So 255,255,255,1 takes up 10 spaces (times 2) is 20 times the amount of pixels, which roughly equates to 256 megabytes of data, add the amount needed for the keys (red, green, blue and alpha) and you quickly run out of memory.

That's why picture viewers use optimized algorithms to show big (raw) files like that (not even talking about compression) like JPEG. Try opening a raw file like that in notepad (it will tell you that it ran out of memory).

So basically only get the image pixel data your currently need (a clipped) version of the image and calculate the rest when panned to. And there are a lot of people out there that probably have better solutions/algorithms to this.

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

Reading at http://php.net/manual/en/function.imagecolorat.php

You basically only want to store the pixels in an array you want to edit, you only need to know their location, so you can access their colour data and transparency later.

Another way to save memory is to cut out the middle man; imagecolorsforindex seems redundant. You always now you have four values in your array. In the end your array length should always be divisible by four. Cutting out that function also stores the rgb values into the array, and that's what you are doing.

Mouser
  • 12,807
  • 3
  • 25
  • 51