Re: Reading only RGB portion of an image, file_get_conents minus file headers etc

From: Date: Mon, 23 Jan 2012 15:24:36 +0000
Subject: Re: Reading only RGB portion of an image, file_get_conents minus file headers etc
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-316359@lists.php.net to get a copy of this message
On 23 January 2012 14:34, Matijn Woudt <tijnema@gmail.com> wrote: > On Mon, Jan 23, 2012 at 3:26 PM, Nicholas Cooper <nicholas@twpagency.com> wrote: >> On 23 January 2012 13:26, Matijn Woudt <tijnema@gmail.com> wrote: >>> On Mon, Jan 23, 2012 at 1:51 PM, Nicholas Cooper <nicholas@twpagency.com> wrote: >>>> Hi everyone, >>>> >>>> I've created an image in RGB from ImageMagick, it's 4 by 4 so I'm >>>> expecting 48 numbers (4*4*3). [width*height*(R,G,B)] >>>> >>>> When I read the file with PHP and unpack it I get between 330 and 333, >>>> I guess this difference is down to headers and end of file data.  Is >>>> there anyway to access only the useful image bits, those 48 numbers? >>>> >>>> Thank you, >>>> >>>> Nicholas Cooper >>>> >>> >>> Hi, >>> >>> The easiest way is probably using imagecolorat[1]. Just do a nested >>> for loop over x and y values, and call imagecolorat for each pixel. >>> >>> Matijn >>> >>> [1] www.php.net/imagecolorat >> >> Thank you that does the trick and gives the expected output.  I plan >> to be processing a large number of images and have always been wary of >> using the built in image functions for performance reasons.  So if >> there is any other solutions I'm welcome to them, or even if someone >> just wants to say that performance is not such an issue any more. > > > If the image you want to process is a simple bitmap (.bmp) file, then > you can easily parse it yourself. Wikipedia has a page on the file > format [1]. > > In short, fopen your file, fseek to 0x000A, read 4 bytes, and parse > them as little-endian (with unpack or so). fseek to that value, and > then you can read 4*4*3 bytes with fseek. That is your RGB data, if it > is in that format ofcourse. If you're not sure which format they are > you might need to parse more of the BMP header, but that's up to you. > > Matijn > > [1] http://en.wikipedia.org/wiki/BMP_file_format Thank you, I haven't quite managed to find the start of my 16 bytes, but I get the idea, so I should be able to get this working after reading more about the format.

« previous php.general (#316359) next »