[SOLVED] Image background of wrong colour

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

[SOLVED] Image background of wrong colour

Post by evilmonkey »

Hello. I am using imagecolorallocate() to set a background colour, but it doesn't seem to be working. In the PHP manual it says that the first call to imagecolorallocate() sets the background colour. I want my background to be white, instead, it's coming out as black. What am I doing wrong?

Code: Select all

<?php
$newimage = imagecreatetruecolor(200, 200);
$background = imagecolorallocate($newimage, 255, 255, 255); //should this set the background to white?
$black = imagecolorallocate($newimage, 255, 255, 255);
imagestring($newimage, 2, 3, 10, "some text in balck", $black);

header("Content-type: image/png");
imagepng($newimage);
imagedestroy($newimage);
?>
Last edited by evilmonkey on Wed Jun 30, 2004 8:56 pm, edited 1 time in total.
HappyTchoum
Forum Newbie
Posts: 8
Joined: Tue Dec 16, 2003 3:18 pm

Post by HappyTchoum »

mmm

Code: Select all

<?php 
$newimage = imagecreatetruecolor(200, 200); 
$background = imagecolorallocate($newimage, 255, 255, 255); //should this set the background to white? 
$black = imagecolorallocate($newimage, 255, 255, 255); 
imagestring($newimage, 2, 3, 10, "some text in balck", $black); 

header("Content-type: image/png"); 
imagepng($newimage); 
imagedestroy($newimage); 
?>

... Your background, you've put it in black with the 255's. It should be 0s instead.

Code: Select all

<?php 
$newimage = imagecreatetruecolor(200, 200); 
$background = imagecolorallocate($newimage, 0, 0, 0); //should this set the background to white? 
$black = imagecolorallocate($newimage, 255, 255, 255); 
imagestring($newimage, 2, 3, 10, "some text in balck", $black); 

header("Content-type: image/png"); 
imagepng($newimage); 
imagedestroy($newimage); 
?>
Let me tell you that this subject is quite [mod]!@#$ up... Prepare yourself. I still didn't found information how to put an image in the background without [mod]!@#$ all the other colors...

If you have an idea... contact me at happytchoum@hotmail.com. It would be very appreciated =O)

HappyTchoum

feyd | watch the language, Happy
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

No, unfortunatly that still keeps the background as balck. I feel like I've tried everything...Here's the modded code:

Code: Select all

$newimage = imagecreatetruecolor(200, 200);
$background = imagecolorallocate($newimage, 0, 0, 0); //this line is getting annoying :X
$black = imagecolorallocate($newimage, 255, 255, 255);
imagestring($newimage, 2, 3, 10, "text in black", $black);

header("Content-type: image/png");
imagepng($newimage);
imagedestroy($newimage);
I still need help :(
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

It's the lesser documented changes, try this.....

Code: Select all

<?php
$newimage = imagecreatetruecolor(200, 200);
$background = imagecolorallocate($newimage, 255, 255, 255);
$fill = imagefill($newimage, 0, 0, $background);
$black = imagecolorallocate($newimage, 0, 0, 0);
imagestring($newimage, 2, 3, 10, "some text in balck", $black);

header("Content-type: image/png");
imagepng($newimage);
imagedestroy($newimage);
?>
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Yup, that seems to have done it. Thanks very much. Also, can you please tell me how to include another .png image in my blank image? (overlay another image).

Thanks for the help!
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

I would recommend you read through this GD tutorial/presentation, it gives examples and sample php code for many common tasks.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

I got that going with imagecopy(). Thanks for the help everyone. :D
Post Reply