Imagepng won't return an image;

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by neilmichael9797, Feb 7, 2014.

  1. neilmichael9797

    neilmichael9797 New Member

    Messages:
    7
    Likes Received:
    0
    Trophy 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 
    imagecreate20080 );
    $background imagecolorallocate$my_img00255 );
    $text_colour imagecolorallocate$my_img255255);
    $line_colour imagecolorallocate$my_img128255);
    imagestring$my_img43025"testing",
      
    $text_colour );
    imagesetthickness $my_img);
    imageline$my_img304516545$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)
    [​IMG]
    As you can see, it is 12.php when it should be 12.png

    Any help is appreciated!
     
  2. leafypiggy

    leafypiggy Manager of Pens and Office Supplies Staff Member

    Messages:
    3,819
    Likes Received:
    163
    Trophy 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.
     
  3. neilmichael9797

    neilmichael9797 New Member

    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    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
     
  4. leafypiggy

    leafypiggy Manager of Pens and Office Supplies Staff Member

    Messages:
    3,819
    Likes Received:
    163
    Trophy Points:
    63
  5. neilmichael9797

    neilmichael9797 New Member

    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
  6. leafypiggy

    leafypiggy Manager of Pens and Office Supplies Staff Member

    Messages:
    3,819
    Likes Received:
    163
    Trophy 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: Feb 7, 2014
  7. phoebex2

    phoebex2 Member

    Messages:
    55
    Likes Received:
    16
    Trophy 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?
     
  8. essellar

    essellar Community Advocate Community Support

    Messages:
    3,295
    Likes Received:
    227
    Trophy 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).
     
    • Useful Useful x 1

Share This Page