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

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.