Jump to content

GD creating images - colours wrong!


iamali84

Recommended Posts

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:

[url=http://farm1.static.flickr.com/126/352712855_bccd09ca61_b.jpg][img]http://farm1.static.flickr.com/126/352712855_bccd09ca61.jpg[/img][/url]

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!
Link to comment
https://forums.phpfreaks.com/topic/33585-gd-creating-images-colours-wrong/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.