954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to display a .png image stored as a BLOB datatype.

I have added a png image to a table in the database as a BLOB. Adding the image into the database is trivial. However, being able to display the BLOB back to the .png image format is bewildering me.

Examples are hard to come by... How to display the BLOB back to the image?

This simple example only displays the raw binary data:

my (@images) = query("SELECT name FROM test_file WHERE id='$id'");

foreach my $img(@images)
{
    print $img;     # Displays the blob gobble de gook
}


I have heard people using BINMODE but again, I can't get that to do anything useful:

open(IMAGE, ">/tmp/file.png");
binmode IMAGE;
print IMAGE $img;
close(IMAGE);


Any examples are much appreciated.
Regards,ns

nonshatter
Posting Whiz
377 posts since Nov 2009
Reputation Points: 39
Solved Threads: 63
 

How is this script being called? Is it a CGI script, or is it being called in some kind of GUI? As I understand it, you can't just print an image to the command line as it's not a graphical interpreter, so you must be calling this in some other context. If this is in a CGI script, you would display the image as you would display any image in a web page:

my (@images) = query("SELECT name FROM test_file WHERE id='$id'");

foreach my $img(@images)
{
    open(IMG,">$$tempfile.png") or die "Couldn't open $$tempfile.png: $!\n";
    binmode IMG;
    print IMG $img;
    echo "<img src=\"$$tempfile\.png\">";
    close IMG;    
}


Can you give us any more details about how this script is being called/used?

roswell1329
Junior Poster in Training
71 posts since May 2006
Reputation Points: 21
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You