Page 1 of 1

Use of imagecreatetruecolor() for PNG image file?

Posted: Wed Dec 03, 2008 2:28 am
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???

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

Posted: Wed Dec 03, 2008 2:38 am
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);
 
?>
 

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

Posted: Wed Dec 03, 2008 3:46 am
by onion2k
Surely you should be using imagecreatefrompng() to load a PNG image..

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

Posted: Wed Dec 03, 2008 3:58 am
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!!!