Use of imagecreatetruecolor() for PNG image file?

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
rasmita
Forum Newbie
Posts: 11
Joined: Wed Nov 19, 2008 5:38 am

Use of imagecreatetruecolor() for PNG image file?

Post by rasmita »

Do any one know about imagecreatetruecolor() function which is used for PNG images. Is it necessary to use this function for png files, as it is returning black backgrounds of images, due to which images are not visible and they are black in color. Can anyone help me???
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Use of imagecreatetruecolor() for PNG image file?

Post by papa »

Code: Select all

<?php
 
header ("Content-type: image/png");
 
//Set image resource
$im = @imagecreatetruecolor(60, 20)
or die("Cannot Initialize new GD image stream");
 
//Set default colors
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
$orange = imagecolorallocate($im, 239, 149, 29);
 
//set bg
imagefill($im, 0, 0, $black);
 
imagestring($im, 4, 6, 2, "I love Papa", $orange);
imagepng($im);
imagedestroy($im);
 
?>
 
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Use of imagecreatetruecolor() for PNG image file?

Post by onion2k »

Surely you should be using imagecreatefrompng() to load a PNG image..
rasmita
Forum Newbie
Posts: 11
Joined: Wed Nov 19, 2008 5:38 am

Re: Use of imagecreatetruecolor() for PNG image file?

Post by rasmita »

Before while using only imagecreatetruecolor(), errors like "gd-png error: setjmp returns error condition " and "gd-png: fatal libpng error: zlib error" were coming making the image of black color. Now after using this -->

$image_p = imagecreatetruecolor($new_width, $new_height);
imagesavealpha($image_p, true);
$trans_colour = imagecolorallocatealpha($image_p, 0, 0, 0, 127);
imagefill($image_pthank, 0, 0, $trans_colour);

$red = imagecolorallocate($image_p, 255, 0, 0);
imagefilledellipse($image_p, 400, 300, 400, 300, $red);


if ($extn=='png')
{
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagepng($image_p,$filename1);
}

my problem is solved to some extent.

Thank for your help to sort out this problem!!!
Post Reply