Question:
How can i read the pixel colors from an image in PHP?
?
2010-09-09 07:18:34 UTC
I need to use PHP to read every pixel in a BMP image. I need to read the file in bytes and convert the bytes into the relevant color code using the fread command. at current the code i have is:
$file = $_POST['imglocation'];
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
$pixels = explode("BM",$contents);
echo ord($pixels[1]);

at the moment this opens the file and removes the header. i am not sure if i am on the right lines or not.
Any help is much apreciated.
Three answers:
?
2010-09-09 07:38:27 UTC
Yes... and no... :-)

The first characters are always "BM", you are correct, but there are many more details.

You must first extract ALL headers from the file, then the Palette header (if there is one), and finally get to the storage of the image. This means you do not know where the "image" starts: you must read and understand the headers.

See details at http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html

As if this was not enough, the colour value of each pixel can be 1bit (black/white); 4 bit (16 colors); 8 bit (256 colors); 24 bit (16.7 million colors)

Use Php to extract the header information BEFORE attempting reading the pixel colours.

Good luck!

(This is fine for bitmaps, but don't even try on Jpegs or Png: these are a totally different algorythm!)



EDIT:

There is, however, a MUCH BETTER way: use the GD library functions!

use imagecreatefromwbmp() to read your image,

then imagecolorat(x,y) to get the R,G,B values.

(Much more of that at www.php.net - enter search function "imagecreatefromwbmp" to have the list of all image functions)
2010-09-09 07:37:17 UTC
Hi there,



I've never done such a thing but a think a good way to start will be to read the bmp file structure (spec) wikipedia has an entry on it might be a good starting point:

http://en.wikipedia.org/wiki/BMP_file_format



Then the idea will be to save the file on a temporary directory with fopen read byte by byte and decode color from the binary information and translate it on html RGB format.



The gdlib ( http://www.boutell.com/gd/ ) has a function to transfert bmp to jpg so it might be a good idea to start by reading the code of those function they probably reads byte by byte and translate it in jpeg encoding.



Good luck seems like a nice project.
linnie
2016-06-01 02:33:04 UTC
I'm going to have to direct you from Photoshop 7. I downloaded the free trial of CS2 some time back, and wasn't impressed! My bad! Lol! 1.With your photo, or image,(drawing, whatever), out in your workspace, go up to Window, and click on it, if the Info 'box' isn't open. After you click on Window in the top header, you'll be presented with a drop-down menu, where Info is one of the sub-menu's listed. Click on it. In the Info box, you'll see RGB,(Red/Green/Blue), information, on the left, and CMYK,(Cyan/Magenta/Yellow/Black, [guess they don't want to get Blue confused with Black!), on the right. Use the Eyedropper Tool, out of the Tool Menu, to the left. When you hover over each part of your image, you'll see the numbers change accordingly with the hue/tint of the colors the Eyedropper tool is over. Write these numbers down, to reference them later, when you do a comparison. I use the CMYK, because it's easier to configure in Image/Adjustments/Channel Mixer,(you have to click on Image, then come down to Mode, and over to CMYK in the fly-out sub-menu, and click on it. Then when you go to Image/Adjustments/Channel Mixer you will have the four color's to adjust from. It's easier for me, than trying to figure out the RGB proportions. Don't forget to go back to Image/Mode, and change it back to RGB, or you won't be able to use a lot of the filters, adjustments, etc., should you wish to do so! Probably left something out here, I'm just so comfortable using PS7 now, that it's old hand to me!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...