imagepng to a 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
User avatar
Guy
Forum Commoner
Posts: 53
Joined: Sun Jan 12, 2003 3:34 am

imagepng to a file

Post 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
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post 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.
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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');
User avatar
Ebula
Forum Newbie
Posts: 15
Joined: Wed Mar 19, 2003 7:21 am
Location: Hamburg, Germany

Post 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?
User avatar
Guy
Forum Commoner
Posts: 53
Joined: Sun Jan 12, 2003 3:34 am

Thanks Ebula

Post by Guy »

I want to create a new image.
the function you provided seems to work great.
exactly what I needed.
thanks
Guy
Post Reply