Discussion:
GD : Black background with imagecreatetruecolor
Arnaud
2002-11-21 21:22:07 UTC
Permalink
Hi everyone,

I'd like to use "imagecreatetruecolor" function in order to "cut and paste"
JPG files on a white background. It seems not to work correctly.

Here is my test scripts :

This one works perfectly :
<?
$im=imagecreate(50,50);
$COULEUR_BLANC=imagecolorallocate($im,255,255,255);
header ("Content-type: image/png");
ImagePNG($im);
imagedestroy($im);
?>

I obtain a white image but using GD 2.x, the "imagecopy" function fails.

This other script doesn't work :
<?
$im=imagecreatetruecolor(50,50);
$COULEUR_BLANC=imagecolorallocate($im,255,255,255);
header ("Content-type: image/png");
ImagePNG($im);
imagedestroy($im);
?>

I'm getting a black image.

My configuration is PHP 4.2.3 with gd2 running on Windows XP (I've also
tested it on my web provider machine - Linux with PHP 4.2.2)

Any help would be great.

Thanks in advance

Arnaud
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Morgan Hughes
2002-11-21 21:47:04 UTC
Permalink
Post by Arnaud
Hi everyone,
I'd like to use "imagecreatetruecolor" function in order to "cut and paste"
JPG files on a white background. It seems not to work correctly.
This one bit me when I started working with GD 2.x as well. Simply draw
a filled rectangle over your entire canvas in the color you want:

<?
$im=imagecreatetruecolor(50,50);
$COULEUR_BLANC=imagecolorallocate($im,255,255,255);
imagefilledrectangle($im,0,0,49,49,$COULEUR_BLANC);
header ("Content-type: image/png");
ImagePNG($im);
imagedestroy($im);
?>

The reason seems to be that when you create an indexed-color image
(imagecreate()) gd sets all pixels to index 0, and when you allocate
your first color, that's index 0. Thus all background pixels magically
become that color.

When you use imagecreatetruecolor(), it sets all the pixels to black,
and you have to explicitly draw them in to get them white.
--
Morgan Hughes
C programmer and highly caffeinated mammal.
***@kyhm.com
ICQ: 79293356
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Arnaud
2002-11-21 22:26:39 UTC
Permalink
Yeah !!! It works

Thanks Morgan

Arnaud
Post by Morgan Hughes
Post by Arnaud
Hi everyone,
I'd like to use "imagecreatetruecolor" function in order to "cut and paste"
JPG files on a white background. It seems not to work correctly.
This one bit me when I started working with GD 2.x as well. Simply draw
<?
$im=imagecreatetruecolor(50,50);
$COULEUR_BLANC=imagecolorallocate($im,255,255,255);
imagefilledrectangle($im,0,0,49,49,$COULEUR_BLANC);
header ("Content-type: image/png");
ImagePNG($im);
imagedestroy($im);
?>
The reason seems to be that when you create an indexed-color image
(imagecreate()) gd sets all pixels to index 0, and when you allocate
your first color, that's index 0. Thus all background pixels magically
become that color.
When you use imagecreatetruecolor(), it sets all the pixels to black,
and you have to explicitly draw them in to get them white.
--
Morgan Hughes
C programmer and highly caffeinated mammal.
ICQ: 79293356
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Van Andel, Robert
2002-11-21 23:26:02 UTC
Permalink
Testing

Robbert van Andel

-----Original Message-----
From: Arnaud [mailto:***@free.fr]
Sent: Thursday, November 21, 2002 1:22 PM
To: php-***@lists.php.net
Subject: [PHP] GD : Black background with imagecreatetruecolor


Hi everyone,

I'd like to use "imagecreatetruecolor" function in order to "cut and paste"
JPG files on a white background. It seems not to work correctly.

Here is my test scripts :

This one works perfectly :
<?
$im=imagecreate(50,50);
$COULEUR_BLANC=imagecolorallocate($im,255,255,255);
header ("Content-type: image/png");
ImagePNG($im);
imagedestroy($im);
?>

I obtain a white image but using GD 2.x, the "imagecopy" function fails.

This other script doesn't work :
<?
$im=imagecreatetruecolor(50,50);
$COULEUR_BLANC=imagecolorallocate($im,255,255,255);
header ("Content-type: image/png");
ImagePNG($im);
imagedestroy($im);
?>

I'm getting a black image.

My configuration is PHP 4.2.3 with gd2 running on Windows XP (I've also
tested it on my web provider machine - Linux with PHP 4.2.2)

Any help would be great.

Thanks in advance

Arnaud
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


"The information transmitted is intended only for the person or entity to
which it is addressed and may contain confidential and/or privileged
material. Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by persons or
entities other than the intended recipient is prohibited. If you received
this in error, please contact the sender and delete the material from all
computers."
Continue reading on narkive:
Search results for 'GD : Black background with imagecreatetruecolor' (Questions and Answers)
4
replies
How do you create graphs in PHP?
started 2009-08-28 21:10:27 UTC
programming & design
Loading...