[Solved] Imagecolorallocate

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

[Solved] Imagecolorallocate

Post by Grim... »

Code: Select all

$color = imagecolorallocate($im, 0, 0, 0);

imageline($im, 5, 19, 66, 867, $color);
imageline($im, 54, 65, 543, 543, $color);   
imageline($im, 55, 3, 1, 166, $color);   
imageline($im, 98, 12, 543, 1, $color);
That should draw 4 black lines, right?

Well, it seems to make the lines any color it likes (although they are alll the same color) - white, red, in fact pretty much anything except for black :(

Have I got this function totally wrong, or what?
Last edited by Grim... on Tue Aug 16, 2005 8:28 am, edited 1 time in total.
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

Isnt the value for black 0,0,0,0? or 255,255,255,255 or am I thinking of something completely different?

I'm thinking the error you're having is due to the numbers before $color.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

No, Black is 255,255,255 (or #FFFFFF).

And the numbers before the color are the line co-ordinates.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Two questions:

1. Are you drawing onto a true color image?

2. Does the imagecolorallocate() function actually do it's job? Does it return true?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Grim... wrote:No, Black is 255,255,255 (or #FFFFFF).
Wrong. Thats white. #000000 is black.
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

I thought 0,0,0,0 was black :P
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Ak, I got confused ;)
What I had in the original was black.

In answer to your questions:
No, I'm not using ImageCreateTrueColor because my silly webhost doesn't have GD 2.0 installed.
Yes, imagecolorallocate seems to be working fine.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

theda wrote:I thought 0,0,0,0 was black :P
0, 0, 0 is black.

3 0's, not 4.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

0,0,0,0 is black if you've got an alpha channel.. but anyway..

I tried this code, and it works fine on my dev machine here. Only I have GD2 rather than GD, so maybe that's an issue..

My code:

Code: Select all

<?php

	$im = imagecreate(1000,600);
	$color = imagecolorallocate($im, 255, 255, 255);
	imagefill($im,0,0,$color);

	$color = imagecolorallocate($im, 0, 0, 0);

	imageline($im, 5, 19, 66, 867, $color);
	imageline($im, 54, 65, 543, 543, $color);   
	imageline($im, 55, 3, 1, 166, $color);   
	imageline($im, 98, 12, 543, 1, $color);

	header("Content-type: image/jpeg");
	imagejpeg($im);

?>
The only thing I can suggest is posting the entire code.. maybe there's a variable change going on somewhere that you've missed.

EDIT: Actually.. are you opening an image and drawing onto it?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Here it is:

Code: Select all

function drawrectangle($image, $tlx, $tly, $brx, $bry)
{   
    //set variable names
    $original_image = $image;
    $filename = "images/user/note/".$image;
    $image = "images/user/cutdown/".$image;
    
    //load original image
    $im = imagecreatefromjpeg($image);
    $color = imagecolorallocate($im, 0, 0, 0);
    
    //draw rectangle
    imageline($im, $tlx, $tly, $brx, $tly, $color);
    imageline($im, $tlx, $bry, $brx, $bry, $color);   
    imageline($im, $tlx, $tly, $tlx, $bry, $color);   
    imageline($im, $brx, $tly, $brx, $bry, $color);
    
    //get image size
    $w = imagesx($im);
    $h = imagesy($im);
    
    //create new image
    $dest = imageCreate($w, $h);
    imagecopy($dest, $im, 0, 0, 0, 0, $w, $h);
    imagejpeg($dest, $filename, 100);
    
    //delete temp images
    imagedestroy($dest);
    imagedestroy($im);
}
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Right.. I know what the problem is then.

When you open a JPEG in GD1.whatever it opens as an indexed color image. This means it has a palette of 256 colors to choose from. If your JPEG has more than 256 colors then the palette will be full.. meaning you can't add another one with imagecolorallocate().

Options open to you are:

1. Save the image you're loading as a GIF or PNG, and make sure it's not got more than 255 colors, leaving a space for your new color. You can do this in Photoshop/Paint Shop Pro.

2. Use imagecolorexact() instead. This will get the color reference from the palette if it's in there. If it's not, then it'll fail.

3. Use imagecolorclosest() instead. This will get the color in the palette that is closest to the one you want. Might be miles off though.

4. Use imagecolorset() to change the color in the palette to what you want. This will get the right color for your lines, but at the same time it will change some of the pixels in the image to the new color.

5. Get a host with GD2.

Personally, I'd either go with 1 or 5 if thats an option, or as a last resort I'd use 3.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

I'm doing 5 shortly, but 3 will be fine for now.

Cheers :)
Post Reply