Imagepng won't return an image;

neilmichael9797

New Member
Messages
7
Reaction score
0
Points
1
Hi, I'm trying to use the imagepng function to create a simple dynamic signature.

Here is my code:
PHP:
<?php
$my_img = imagecreate( 200, 80 );
$background = imagecolorallocate( $my_img, 0, 0, 255 );
$text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
imagestring( $my_img, 4, 30, 25, "testing",
  $text_colour );
imagesetthickness ( $my_img, 5 );
imageline( $my_img, 30, 45, 165, 45, $line_colour );

header( "Content-type: image/png" );
imagepng( $my_img );
imagecolordeallocate( $line_color );
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $my_img );
?>
This all works fine, except, it does not output a png image, rather it is a php image (See pic)
pUwAqOj.png

As you can see, it is 12.php when it should be 12.png

Any help is appreciated!
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
You're not saving the image anywhere -- you're using the library to create an image. You're also telling the server that the .php file is going to be a "image/png" instead of "application/php" -- meaning your .php file acts as a .png.
 

neilmichael9797

New Member
Messages
7
Reaction score
0
Points
1
You're not saving the image anywhere -- you're using the library to create an image. You're also telling the server that the .php file is going to be a "image/png" instead of "application/php" -- meaning your .php file acts as a .png.
No, what I want is my .php to act as a .png, but it won't as seen in the picture. I want an output of a png image
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
Yes -- why wouldn't it be? I think you're misunderstanding the entire concept of what you're doing.

Code:
<?php
$my_img = imagecreate( 200, 80 ); //create image resource identifier $my_img. Basically a placeholder in memory for the image.
$background = imagecolorallocate( $my_img, 0, 0, 255 ); //Allocate memory for an image color identifer
$text_colour = imagecolorallocate( $my_img, 255, 255, 0 ); //see above.
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 ); //see above
imagestring( $my_img, 4, 30, 25, "testing",
  $text_colour ); // draw a string horizontally on an image resource identifer ($my_img)
imagesetthickness ( $my_img, 5 ); //set the thickness for a line on the image resource.
imageline( $my_img, 30, 45, 165, 45, $line_colour ); //draw a line on the image resource

header( "Content-type: image/png" ); // set the content type of the page so that a .php file is interpreted by the browser as type image/png.
imagepng( $my_img ); //create an image from the image resource, alternately save the image to file
imagecolordeallocate( $line_color ); // deallocate color identifier
imagecolordeallocate( $text_color ); //see above
imagecolordeallocate( $background ); //see above
imagedestroy( $my_img ); //destroy image resource $my_img from memory.
?>
 
Last edited:

phoebex2

Member
Messages
55
Reaction score
16
Points
8
It's a dynamically generated image, not a static (or saved) image. It's generated on the fly and doesn't "exist" in realtime like a jpeg or something saved from a website.

It saves storage space, as it's not a "physical" file in the sense that a saved and uploaded image is, see?

Think of it like a ghost; it's seen, so it exists, but it has no "physical" existence. Its existence is in memory, not actual space. This is a good thing, really. You wouldn't want your site jammed full of images that may or may not have even been seen and will never be used again, right?
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
If you're doing it here (on a Free Hosting account), you can't use .NET (ASP.NET, C#). You'd need to use PHP, which is the only server-side language you have guaranteed access to. There are plenty of tools and libraries out there to make it easier for you (and tutorials to help you make your own), but you have to remember to include "PHP" in your search. Most of what you want to do will happen to "images", which are abstract representations of the picture you're working with and have nothing to do with any file format. PNG only comes up when you import an existing image file or create output. The stuff in between is not PNG, it's just "image".

There's nothing special about PNG that you need to know. It's just a file format for images. Knowing the internal structure of a PNG file isn't necessary unless you are building an import/export library (PHP has that built in) or trying to manually recover a damaged file. The only thing you need to know about the format is that it comes in two "flavours": paletted (only a small number of colours available, but with very small file sizes — very much like GIF); and full-colour (16.7 million colours, partial/alpha transparency if you need it, but much larger files — like TIFF, but with better compression).
 
Top