Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How to save an image (using GD) to a file

by belize (Deacon)
on Nov 27, 2000 at 20:52 UTC ( [id://43509]=perlquestion: print w/replies, xml ) Need Help??

belize has asked for the wisdom of the Perl Monks concerning the following question:

I am using the GD module to create an image and can get it to print directly to a web page using:

print "Content-type: image/gif\n\n"; print $im->gif;
used as follows referencing an external CGI:

<IMG SRC="image.cgi">
But if I use the image source as a sub-routine:

<IMG SRC="&image_routine">
I get an ASCII printout on the page instead of the image.

So I would like to get the script to save the GD produced image as a file instead of outputting directly to the Webpage, but am having trouble getting it to work. I am using the following code:

$gif_data = $im->gif; open (TEST,"$image_test") || die; binmode TEST; print TEST $gif_data; close TEST
The script runs fine, except the $image_test does not show.

Any ideas?

Replies are listed 'Best First'.
Re: How to save an image (using GD) to a file
by clemburg (Curate) on Nov 27, 2000 at 21:23 UTC

    Why don't you use the CGI solution? With suitable parameters, you can have a very generic image generator with that.

    As for your problem, try this:

    open (TEST,">$image_test") || die;

    See? There is a ">" in front of the filename.

    And lastly, you will want to get a copy of Programming Web Graphics with Perl and Gnu Software.

    Update: tilly reminded me that the die in the code I gave is "uninformative". Well, he is right, of course. You should use:

    open(TEST,">$image_test") || die "Error: no open (write mode) for '$image_test': $!";

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

      Thanks Christian. The ">" was the exact problem. The image now displays. I will modify the:
      || die
      to reflect an error message. Thanks again.
Re: How to save an image (using GD) to a file
by KM (Priest) on Nov 27, 2000 at 21:24 UTC
    open (TEST,"$image_test") || die;

    You are opening for reading, not writing here. This may be your problem. I am assuming you have an older version of GD, since the newer ones don't support GIFs.

    Cheers,
    KM

Re: How to save an image (using GD) to a file
by arturo (Vicar) on Nov 27, 2000 at 21:03 UTC

    I'd do what you're doin: open a file and print the image to that filehandle. Then, when I generated the HTML, have the src attribute point to the file I just created.

    UPDATE

    When you say "doesn't show" do you get the "image not found" image?

      .
    • Have you checked to see that the image is where you think it is?
    • Is the image file in a directory under the server's document tree?
    • Does the image file have permissions such that the webserver process can read it?
      Thanks for your reply. That is exactly what I am trying to do, save it as an image. I found the answer from another response. I was leaving out the ">" when opening the file to write to. It now works. Now I am working on a next problem and will probably be asking for assistence once I research it a bit on my own. Thanks again for the rapid response.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://43509]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-18 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found