2

I have a very large PNG image, and I am writing a method to get the value for a color at a specific (but changing) pixel of that image. When I create the image using:

$image = imagecreatefrompng('map.png');

Is the whole image loaded into memory (not ideal), or does it just read the meta data and prepare for other calls so that when I call:

int imagecolorat ( resource $image , int $x , int $y )

Will it file seek to the right pixel or pull from memory? If I'm trying to optimize this routine to be called repeatedly, would I be better off converting the image data I need into some raw binary format and using file seek? I'd like to avoid repeatedly loading the whole file into memory if possible.

miken32
  • 42,008
  • 16
  • 111
  • 154
BadPirate
  • 25,802
  • 10
  • 92
  • 123
  • 4
    The entire image is loaded into memory, at 4 bytes per pixel; any manipulation will typically require double that amount of memory – Mark Baker May 14 '16 at 21:16
  • @MarkBaker - Sounds like an answer not a comment :) Put it below and I'll give you the check. (Bummer BTW, it makes sense because with compression the exact location of a pixel inside a file wouldn't be known.) Looks like I'll have to convert it to a flat file and use fopen / fseek – BadPirate May 14 '16 at 21:18
  • There used to be a PHP extension called PHPqb (the web site seems dead now http://php-qb.net/) that was designed for highly memory-efficient manipulation of images, though it is still available through [PECL](https://pecl.php.net/package/qb) – Mark Baker May 14 '16 at 21:31

1 Answers1

0

You need a big php memory to play with php image resources. Use graphicsMagick instead. http://www.graphicsmagick.org/

Andrei Todorut
  • 4,260
  • 2
  • 17
  • 28
  • 1
    Not really trying to do image manipulation, what I'm actually trying to do is to get a number of geographic features for a specific coordinate. Elevation has a Google API, but I need things like tree density -- The only sources of these seem to be massive grayscale TIFF files. Basically, I'm going to have to convert them into something that resembles an 8 bit BMP (0 compression) so that I can know mathematically which byte to access to get the value I need. – BadPirate May 14 '16 at 21:34