Correct way to get image pixel data in PHP?


I am trying to get image pixel data in php the way we get in js canvas like


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

        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'sgetImageData() 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. ,




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 you 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

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

People are also looking for solutions to the problem: html - Fatal error: Can't use function return value in write context in views/stock/form.php on line 29


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.