I'm fairly new to perl and have hit a stumbling block. I've exhaused every solution and google search I can think of, but to no avail. I've got a script on an internal win2003 server that reads and imports gif image files into a mssql2005 db image field using DBD::ODBC module for the connection. For network simplicity and security, I've been required to store the images inside the db instead of referencing a file outside the db:
open(frontFILE, $imageFront.'.gif'); read (frontFILE, $blobFront, -s "$imageFront"); close frontFILE; open(backFILE, $imageBack.'.gif'); read (backFILE, $blobBack, -s "$imageBack"); close backFILE; my $sth=$db->prepare("Insert INTO docs (FrontImg,BackImg,docID) va +lues (?,?,?); $sth->execute($blobFront, $blobBack, $docID) or die $sth->errstr;
And a public web interface (debian server running apache2) that gets those records from the db (this time using DBD::Sybase for the db connection) and displays to the website inside an <img> tag:
my $whichImage = 'e5c59e0d-fc0e-4d8e-9fa5-7b3bac6f8f25'; #getting the image from the db my $db=DBI->connect('DBI:Sybase:server='.$server,$sql_user,$sq +l_password); $db->do("use ".$database) ; my $statement = "select FrontImg, BackImg, docid from docs whe +re docid = '$whichImage'"; my @rows=@{$db->selectall_arrayref($statement, {Slice => {}})} + ; unless( @rows){die "$statement \ndidn't find the image ".@rows +;} #there should only be one record returned -- docid is the prim +ary key my $data = $rows[0]; #to get the same image from file instead of the db: open FILE, "<$filebase".'/1001-1-B.gif' ; my $blob; read(FILE, $blob, -s $Lockbox::Web::CONFIG{filebase}.'/1001-1- +B.gif'); close FILE; #the following line displays only the dreaded empty box w/a red 'x +' #instead of the image from the database print $q->header(type=>'image/gif', Content_Length=>length($data-> +{BackImg})).$data->{BackImg} ; #the following line displays the image from the file correctly #but i need to get it from the db instead #print $q->header(type=>'image/gif', Content_Length=>length($blob) +).$blob ; if($blob ne $data->{BackImg}){die $data->{BackImg}."\nlength=" +.length($data->{BackImg})."\nfile image = ".$blob."\nlength=".length( +$blob);}
When I compare the output of $blob vs. $data->{BackImg}, $data->{BackImg} looks like a hex string with a length exactly double the length of $blob. $blob starts with 'GIF 89a' followed by what looks like gibberish. I'm not sure if the problem exists in inserting the image to the db, retrieving the image from the db or outputting to the webpage.

I tried playing with hex conversion, but all I ended up with was an empty string.

Any help would be appreciated...thanks

ksublondie

ps -- BTW...the original images (all 170,000+!) exist as tif's that I'm converting to gif's (so I can display inside the browser) using ImageMagick. If there's a more efficient way to do this, I'd appreciate the input.

In reply to insert & retrieve images from DB to web by ksublondie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.