Page 1 of 1
[Solved] Imagecolorallocate
Posted: Mon Aug 15, 2005 9:28 am
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?
Posted: Mon Aug 15, 2005 9:46 am
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.
Posted: Mon Aug 15, 2005 9:51 am
by Grim...
No, Black is 255,255,255 (or #FFFFFF).
And the numbers before the color are the line co-ordinates.
Posted: Mon Aug 15, 2005 11:00 am
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?
Posted: Mon Aug 15, 2005 11:01 am
by onion2k
Grim... wrote:No, Black is 255,255,255 (or #FFFFFF).
Wrong. Thats white. #000000 is black.
Posted: Mon Aug 15, 2005 11:12 am
by theda
I thought 0,0,0,0 was black

Posted: Mon Aug 15, 2005 11:25 am
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.
Posted: Mon Aug 15, 2005 11:26 am
by Grim...
theda wrote:I thought 0,0,0,0 was black

0, 0, 0 is black.
3 0's, not 4.
Posted: Mon Aug 15, 2005 2:02 pm
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?
Posted: Mon Aug 15, 2005 4:31 pm
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);
}
Posted: Tue Aug 16, 2005 4:54 am
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.
Posted: Tue Aug 16, 2005 8:27 am
by Grim...
I'm doing 5 shortly, but 3 will be fine for now.
Cheers
