Page 1 of 1

imagepng to a file

Posted: Sun Mar 23, 2003 7:23 am
by Guy
I'm trying to create a png file, using imagepneg.
this code doesn't work:

Code: Select all

$fp = fopen("c:/tmp.png","w");
$fd = ImagePng($image);
while (!feof($fd)){
    fwrite($fp,$fd,1024);
 }
fclose($fp);
I get the png on the browser but a big empty file.
any suggestions?
thanks
Guy

Posted: Sun Mar 23, 2003 9:20 am
by hedge
Don't think you can do it that way. If you check the doc I think you will find that imagepng will take a second parameter of a filename, if its empty it will push it to the browser else it will write a file. I haven't checked but that's how the other image functions work. Then just call it twice, once to the file, once to the browser.

Posted: Sun Mar 23, 2003 10:51 am
by pootergeist
imagepng() is a GD function and as such works by reading the file into a .gd or .gd2 fileformat - you can only do that with

imagecreatefrompng('filename.png');

and not file()

.............

$png_image = imagecreatefrompng('filename.png');
header("Content-type: image/png");
imagepng($png_image,'new_filename.png');

Posted: Mon Mar 24, 2003 4:27 am
by Ebula
I have been working quite a bit with these png funktions, here is a script that works. It generates a PNG which I display later on.

Code: Select all

<?php function MkPNG($Text,$FileName, $Font=3)
{
 $PixLen = (110-(strlen($Text)*7))/2;
 $im = @ImageCreate (110, 20);
 $background_color = ImageColorAllocate ($im, 0, 52, 102);
 $text_color = ImageColorAllocate ($im, 255, 255, 255);
 ImageString ($im, $Font, $PixLen, 2, $Text, $text_color);
 imagefilledrectangle($im,2,16,108,17,$text_color);
 ImagePNG ($im,$FileName.".png");
};

?>
Do you want to create a new Image or edit one that you have on the web server?

Thanks Ebula

Posted: Tue Mar 25, 2003 7:38 am
by Guy
I want to create a new image.
the function you provided seems to work great.
exactly what I needed.
thanks
Guy