GD creating images - colours wrong!

hey all!

i’ve been using flash to create an online graffiti wall and using PHP to create JPG images of images created on it but am having a bit of a problem. the JPGs are created fine but the colours are coming out wrong, see image below:

it seems the red and yellow are coming out as blue and light blue. does anyone have any idea why this may be? this is the PHP code i am using:

[code]<?php
//If GD library is not installed
if(!function_exists(“imagecreate”)) die(“Sorry, you need GD library to run this example”);

//Capture Post data
$data = explode(",", $_POST[‘img’]);
$width = $_POST[‘width’];
$height = $_POST[‘height’];

//Allocate image
$image = (function_exists(“imagecreatetruecolor”))?imagecreatetruecolor( $width ,$height ):imagecreate( $width ,$height );
imagefill($image, 0, 0, 0xFFFFFF);

//Copy pixels
$i = 0;
for($x = 0; $x <= $width; $x++){
for($y = 0; $y <= $height; $y++){
while(strlen($data[$i]) < 6) $data[$i] = “0” . $data[$i];
$tmp = “0x” . $data[$i] ;
$data[$i] = sprintf("%x", $tmp - 0x1);

		$r = 255-hexdec("0X".substr( $data[$i] , 0 , 2 ));
		$g = 255-hexdec("0x".substr( $data[$i] , 2 , 2 ));
		$b = 255-hexdec("0x".substr( $data[$i++] , 4 , 2 ));
		$color =  ($r << 16) | ($g << 8) | $b;
		$color = imagecolorallocate($image, $r, $g, $b);

		imagesetpixel ( $image , $x , $y , $color );
		
		/*if ( ($r < 0 || $r >255) || ($g < 0 || $g >255) || ($b < 0 || $b >255) )
		die("something");$  */
    }
}

//Output image and clean
header( “Content-type: image/jpeg” );
ImageJPEG( $image );
imagedestroy( $image );
?>[/code]

thanks!

Why do you substract the hex code from 255?

Also, are you certain the data you receive is RGB and not CMYK or some other color scheme?

This line is useless, the value is overwritten right after.

$color =  ($r << 16) | ($g << 8) | $b; 

i’m not 100% sure as i used code from a website i found. i’m not good at php so i don’t understand all of it myself… :(

On lines like this one:

$r = 255-hexdec("0X".substr( $data[$i] , 0 , 2 )); 

Try removing the 255- part.

If you don’t understand it yourself, I can only provide limited help.

i think i understand it, but don’t understand why it isn’t working

i tried removing the 255 part but made the image inverted.

checked the incoming HEX values and for some reason the yellow comes in as 0,255,255 which is light blue (as shown on the picture) and red comes in as 0,0,255 which is blue (as shown on the picture), but can’t work out why it is doing this and whether its a problem with the PHP or flash…

From what I can gather, $data[$i] holds the data for the ith pixel (it’s color data among other things). Inspect that variable for one of the messed up pixels and see what you can gather. It should contain the hex color code. If that color is incorrect then it looks like the problem is on the flash side of things.

Looks like the problem is actually very simple. R and B are inverted.

[php]
$b = hexdec(“0X”.substr( $data[$i] , 0 , 2 ));
$g = hexdec(“0x”.substr( $data[$i] , 2 , 2 ));
$r = hexdec(“0x”.substr( $data[$i++] , 4 , 2 ));
[/php]

There is a script I found on the web that converts HEX to RGB maybe this script will help the matter:

[code]R = HexToR("#FFFFFF");
G = HexToG("#FFFFFF");
B = HexToB("#FFFFFF");

function HexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
function HexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
function HexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}[/code]
I don’t believe this is PHP but the scripts look similar and could prove useful. On a side note I believe it is java.

Sponsor our Newsletter | Privacy Policy | Terms of Service