Solved

Images from MYSQL Database Using ImageMagick

Posted on 2008-10-16
36
460 Views
Last Modified: 2012-05-05
I'm back again with yet another question about ImageMagick...yay. So, I am unable to get ImageMagick to work with the method I'm using to display a picture. All I want to do is pull the image from the database and view it. I tried this numerous times before and couldn't get it to work. I now have ImageMagick installed and hope it could help me with this. So, below is the code I'm trying now. It's actually writing the file to a directory, and I have /pictures mapped to /usr/local/pictures in case it looks like I'm pointing it to the wrong place. Also, I don't need to write the file anywhere, I just didn't know any other way to pull the data from the DB and then display it. Any help would be greatly appreciated.
sub Check21Images{

  my($DB, $FORM, $SESS) = @_;
 

  my $DB21 = modules::Db->ConnectCheck21();
 

  modules::Io->TextHtmlHeader();
 

  my $Action = modules::Io->Server();
 

  my $LongReadLen = $DB21->{dbh}->selectrow_array(qq{SELECT MAX(DATALENGTH(Img)) FROM Image_View});

  $DB21->{dbh}->do("set textsize $LongReadLen");
 

  my $sql2 = "SELECT Img, CheckNum

              FROM Image_View

              WHERE UserID = '$FORM->{StoreNum}'

              AND Acct = '$FORM->{BankAcctNum}'

              AND Routing = '$FORM->{RoutingNum}'

              AND CheckNum = '$FORM->{CheckNum}'

              AND Amt = '$FORM->{Amt}'";
 

  my $rs = $DB21->SelectHash($sql2);
 

  print<<EOD;
 

  <html>

  <head>

  </head>

  <body>

  <table border=0 width=35%>

  <tr>

    <td><b>Check 21 Image Link</b></td>

  </tr>

EOD

  my $bgcolor;

  my $background;

  my $num = 0;

  foreach(sort{$rs->{$a}->{CheckNum} cmp $rs->{$b}->{CheckNum}}keys(%$rs)){

    $num++;

    if($bgcolor == 1){

      $bgcolor = 0;

    }elsif($bgcolor == 0){

      $bgcolor = 1;

    }

    if ($bgcolor == 1) {

        $background = "lightgrey";

    }else {

        $background = "#FFFFFF";

    }
 

    ## Here's my ImageMagick problem ##

    my $fh = $rs->{$_}->{Img};

    my $new_file = "/usr/local/pictures/TESTEST.jpg";

    open(my $out, ">$new_file") or die "new_file: $!\n";

    binmode($out);

    print $out $_ while(<$fh>);

    close($out);
 

    my $image = Image::Magick->new;

    my $x = $image->Read("$new_file");

    if($x ne ""){

      print STDERR "x is $x\n";

    }

    $x = $image->Write("$new_file");

    if($x ne ""){

      print STDERR "x is $x\n";

    }

    print "<tr><td bgcolor='$background'><img src='/pictures/TESTEST.jpg'></td></tr>";

    ## Ends here ##

  }

  print "</table>";

  print "<form name=myForm action=$Action><br><br>SELECT * FROM Image_View\n";

  print "<br>WHERE UserID = <input type=text size=10 name=StoreNum value=$FORM->{StoreNum}><br>\n";

  print "AND Acct = <input type=text size=20 name=BankAcctNum value=$FORM->{BankAcctNum}><br>\n";

  print "AND Routing = <input type=text size=20 name=RoutingNum value=$FORM->{RoutingNum}><br>\n";

  print "AND CheckNum = <input type=text size=10 name=CheckNum value=$FORM->{CheckNum}><br>\n";

  print "AND Amt = <input type=text size=10 name=Amt value=$FORM->{Amt}>&nbsp;&nbsp;&nbsp;<input type=button name=RUN value=RUN onClick='javascript: myForm.submit();'>\n";

  print "<input type=hidden name=app value=Check21Images></form>\n";

  my $sql = "SELECT UserID, Acct, Routing, CheckNum, Amt

             FROM Image_View

             WHERE UserID = '0300'

            ";

  my $rs = $DB21->SelectHash($sql);
 

  print "<table border=0 width='100%'>\n";

    print "<tr><td>UserID</td><td>Acct</td><td>Routing</td><td>CheckNum</td><td>Amt</td></tr>";

  foreach(sort{$rs->{$a}->{CheckNum} cmp $rs->{$b}->{CheckNum}}keys(%$rs)){

    print "<tr><td>$rs->{$_}->{UserID}</td><td>$rs->{$_}->{Acct}</td><td>$rs->{$_}->{Routing}</td><td>$rs->{$_}->{CheckNum}</td><td>$rs->{$_}->{Amt}</td></tr>";

  }

  print "</table>\n";

}

Open in new window

0
Comment
Question by:linbayzak
  • 19
  • 15
  • 2
36 Comments
 
LVL 6

Expert Comment

by:RSLE
Comment Utility
you don't need to use imageMagick to display the image... none the less you can spit it out form ImageMagick to STDOUT and display it straight to the browser using:

print "Content-type: image/jpeg\n\n";
binmode STDOUT;
$example->Write('jpg:-');

but this should be called seperately
0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
You can display it directly from the database.  If you have the picture data in $pictre
#!/usr/bin/perl

use strict;

use DBI;

use CGI ':standard';
 

#Get picture from database, save to $picture
 

print header('text/jpeg');  #or whatever format the image is

binmode(STDOUT);

print $picture;

Open in new window

0
 

Author Comment

by:linbayzak
Comment Utility
RSLE: I couldn't get your example to work, perhaps I am doing something wrong.

Adam314: I am wanting to output this to a dialog box, so I have tried your method, as I have shown below. I get the javascript error that the object doesn't support this method when trying it, I'm guessing because the image data might break a length limitation on the javascript command. Any other ideas on how to do this?
  foreach(sort{$rs->{$a}->{CheckNum} cmp $rs->{$b}->{CheckNum}}keys(%$rs)){

    $num++;

    if($bgcolor == 1){

      $bgcolor = 0;

    }elsif($bgcolor == 0){

      $bgcolor = 1;

    }

    if ($bgcolor == 1) {

        $background = "lightgrey";

    }else {

        $background = "#FFFFFF";

    }

    my $Window  = "toolbar=no,location=no,directories=no,status=no,";

       $Window .= "menubar=no,scrollbars=yes,resizable=yes";
 

    print "

    <script language=javascript>

    function ShowPicture$num(){

      S$num = window.ShowModalDialog('$Action?app=ShowThisPic&Pic=$rs->{$_}->{Img}','Check21Images','$Window');

    }

    </script>";

    print "<script>ShowPicture$num()</script>";

  }
 

sub ShowThisPic{

  my($DB,$FORM,$SESS,$Pic) = @_;
 

  print header('text/tif');

  binmode(STDOUT);

  print $Pic;

}

Open in new window

0
 
LVL 6

Expert Comment

by:RSLE
Comment Utility
It looks like you are trying to inject the image directly into the html code... which will not work.  You have to do the image and html separately.

this isn't incorporated into your script but just an example of reading the file in from a file and then spitting it out directly from a cgi:

----
#!/usr/bin/perl

use Image::Magick;

## Read the image in ##
$model=Image::Magick->new;
$x=$model->ReadImage('example.jpg');
warn "$x" if "$x";

## And display it to the web ##
print "Content-type: image/jpeg\n\n";
binmode STDOUT;
$model->Write('jpg:-');
----

However, since it's agreed you don't need to be using ImageMagick to do this, a very basic example of this is below...


...
 

if ($id) { &image;}

else { &html; }
 

sub image {

        $sth = $dbh->prepare(qq{SELECT image FROM $table WHERE id = '$id' limit 1});

        $rv = $sth->execute();
 

        $image = $sth->fetchrow_array;

        print "Content-type: $EXT{$ext}\n\n";

	binmode $file;

	print $file;
 

        $sth->finish;

}
 

sub html {

	print "Content-type: text/html\n\n";

	print "<img src=$ENV{SCRIPT_NAME}?id=example001.jpg>";

}

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
The MIME type for a .tif image is 'image/tiff'.  There is no 'text/tif' MIME type, so that might be part of the problem.  You can get a list here:
    http://www.iana.org/assignments/media-types/

Next, I'm not sure what $Action is, or what that page does with the options you gave it.  The $Pic in the ShowThisPic subroutine needs to be the actual picture data, and there can not have already been another header printed.  And I'm assuming the header function here is from the CGI module.

If the page specified by $Action doesn't output any headers, it loads the picture data, and it calls the ShowThisPic subroutine with the picture data as the 4th argument, then the problem is not there, but somewhere else.

You might also benefit from the HTML::Template module:
    http://search.cpan.org/~samtregar/HTML-Template-2.9/Template.pm
0
 

Author Comment

by:linbayzak
Comment Utility
RSLE: I'm ruling out the ImageMagick entirely since it seems that's not necessary. I don't understand the other code that well either. It doesn't seem like it would work in the situation I am using it in.

Adam314: I changed the mime type to image/tiff, but that is not the problem. It is not even getting to that sub, because the javascript is failing to launch the dialog box, which is where I am getting the javascript error I was talking about before. $Action is essentially $ENV{SCRIPT_NAME}, just to get me to the path where I'm at. The url basically just opens to the ShowThisPic subroutine, but for some reason javascript isn't liking the syntax, which looks correct to me (I pasted it exactly from another sub where it worked fine). The Pic= arguement is the actual image data, which in the source code is massive looking. So, the header in the ShowThisPic subroutine should be the only header for the new window, correct? Yes, the header is from CGI module.  So, I'm wondering if there's another way than what I've done to open a new window to display this picture file?
0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
If I understood you correctly, in this code:
    print "
    <script language=javascript>
    function ShowPicture$num(){
      S$num = window.ShowModalDialog('$Action?app=ShowThisPic&Pic=$rs->{$_}->{Img}','Check21Images','$Window');
    }
    </script>";
The $rs->{$_}->{Img} contains the actual picture data.  If so, this is the problem.
What you need is to have the first parameter to the ShowModalDialog be a URL that when called, will return the picture data.

So maybe something like this:
    #Have $UniqueIdForPicture be some unique ID that identifies this picture
    #(like the primary key of the table that contains the image data)
    S$num = window.ShowModalDialog('/GetImage.pl?ImageID=$UniqueIdForPicture'','Check21Images','$Window');

Then in GetImage.pl, something like this:
    #!/usr/bin/perl
    use strict;
    use DBI;
    use CGI ':standard';
   
    #Get picture data from database, save to a variable...
    #i'm using $pic in this example, but it can be whatever you want
    #It should be whatever sets $rs->{$_}->{Img} in $Action?app=ShowThisPic&Pic=$rs->{$_}->{Img}    
   
    print header('image/tiff');
    binmode(STDOUT);
    print $pic;

0
 

Author Comment

by:linbayzak
Comment Utility
This has definitely helped a lot. I am now at least getting the dialog box to pop up, but it is only displaying a really long string of characters and -Type: image/tiff at the end of the first string (there are two, because there will always be two images per check - front and back). I'm surprised it's not at the end of both strings. I thought it was just the header but I have commented it out and it is still showing just a string of a characters followed by "-Type: image/tiff". I have tried opening this in both window.openModalDialog() and window.open, neither method will display images. window.open simply displays an incredibly long line of text with no second line like I would expect for the second image, and no "-Type: image/tiff". Any ideas? (new code below)
sub Check21Images{

  my($DB, $FORM, $SESS) = @_;
 

  my $DB21 = modules::Db->ConnectCheck21();
 

  modules::Io->TextHtmlHeader();
 

  my $Action = modules::Io->Server();
 

  my $LongReadLen = $DB21->{dbh}->selectrow_array(qq{SELECT MAX(DATALENGTH(Img)) FROM Image_View});

  $DB21->{dbh}->do("set textsize $LongReadLen");
 

  my $sql2 = "SELECT COUNT(*)

              FROM Image_View

              WHERE UserID = '$FORM->{StoreNum}'

              AND Acct = '$FORM->{BankAcctNum}'

              AND Routing = '$FORM->{RoutingNum}'

              AND CheckNum = '$FORM->{CheckNum}'

              AND Amt = '$FORM->{Amt}'";
 

  my $rs = $DB21->Count($sql2);
 

  my $Window  = "toolbar=no,location=no,directories=no,status=no,";

     $Window .= "menubar=no,scrollbars=yes,resizable=yes";
 

  print<<EOD;
 

  <html>

  <head>

  <script language='javascript'>

    function ShowPicture(){

      window.open('$Action?app=ShowThisPic&UserID=$FORM->{StoreNum}&Acct=$FORM->{BankAcctNum}&Routing=$FORM->{RoutingNum}&CheckNum=$FORM->{CheckNum}&Amt=$FORM->{Amt}','Check21Images','$Window');

    }

  </script>

  </head>

EOD

  if($rs > 0){

    print "<body onLoad='ShowPicture()'>";

  }else{

    print "<body>";

  }

  print<<EOD;

  <table border=0 width=35%>

  <tr>

    <td><b>Check 21 Image Link</b></td>

  </tr>

  </table>

EOD

  my $bgcolor;

  my $background;

  my $num = 0;
 

  print "<form name=myForm action=$Action><br><br>SELECT * FROM Image_View\n";

  print "<br>WHERE UserID = <input type=text size=10 name=StoreNum value=$FORM->{StoreNum}><br>\n";

  print "AND Acct = <input type=text size=20 name=BankAcctNum value=$FORM->{BankAcctNum}><br>\n";

  print "AND Routing = <input type=text size=20 name=RoutingNum value=$FORM->{RoutingNum}><br>\n";

  print "AND CheckNum = <input type=text size=10 name=CheckNum value=$FORM->{CheckNum}><br>\n";

  print "AND Amt = <input type=text size=10 name=Amt value=$FORM->{Amt}>&nbsp;&nbsp;&nbsp;<input type=button name=RUN value=RUN onClick='javascript: myForm.submit();'>\n";

  print "<input type=hidden name=app value=Check21Images></form>\n";

  my $sql = "SELECT UserID, Acct, Routing, CheckNum, Amt

             FROM Image_View

             WHERE UserID = '0300'

            ";

  my $rs = $DB21->SelectHash($sql);
 

  print "<table border=0 width='100%'>\n";

    print "<tr><td>UserID</td><td>Acct</td><td>Routing</td><td>CheckNum</td><td>Amt</td></tr>";

  foreach(sort{$rs->{$a}->{CheckNum} cmp $rs->{$b}->{CheckNum}}keys(%$rs)){

    print "<tr><td>$rs->{$_}->{UserID}</td><td>$rs->{$_}->{Acct}</td><td>$rs->{$_}->{Routing}</td><td>$rs->{$_}->{CheckNum}</td><td>$rs->{$_}->{Amt}</td></tr>";

  }

  print "</table></body></html>\n";

}
 

sub ShowThisPic{

  my($DB,$FORM,$SESS) = @_;
 

  my $DB21 = modules::Db->ConnectCheck21();
 

  my $LongReadLen = $DB21->{dbh}->selectrow_array(qq{SELECT MAX(DATALENGTH(Img)) FROM Image_View});

  $DB21->{dbh}->do("set textsize $LongReadLen");
 

  my $sql = "SELECT Img, CheckNum

             FROM Image_View

             WHERE UserID = '$FORM->{UserID}'

             AND Acct = '$FORM->{Acct}'

             AND Routing = '$FORM->{Routing}'

             AND CheckNum = '$FORM->{CheckNum}'

             AND Amt = '$FORM->{Amt}'";
 

  my $rs = $DB21->SelectHash($sql);

  print header('image/tiff'); ### I have tried this line both commented and not.
 

  foreach(keys(%$rs)){

    my $Pic = $rs->{$_}->{Img};

    binmode(STDOUT);

    print $Pic;

    print "<br>";

  }

}

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
If you go to your browser, and enter a URL to get a picture, does the picture show up?
eg: enter whatever this works out to in your URL bar:
    $Action?app=ShowThisPic&UserID=$FORM->{StoreNum}&Acct=$FORM->{BankAcctNum}&Routing=$FORM->{RoutingNum}&CheckNum=$FORM->{CheckNum}&Amt=$FORM->{Amt}


I'm guessing that your main page is outputting a 'text/html' header.  Since you can only have one header, once that is sent, the header in your ShowThisPic is interpreted as being part of the body, not the header.


Also note that having private information in the URL can be un-secure - I'm not certain, but it looks like that might be what is happening here...
0
 

Author Comment

by:linbayzak
Comment Utility
I can't paste it into the URL as the main script checks if I am logged in using session variables. I noticed, however, when I just opened it using window.open() in the script without a header I get an Internal Server Error of malformed header, so I don't think it's adding any additional headers to the page. Perhaps modalDialogBox automatically does in javascript, but a regular open window does not, if that helps any.
0
 

Author Comment

by:linbayzak
Comment Utility
Actually, it is happening with the modalDialogBox as well, my mistake.
0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
Is there anything in your error log?

The session variables would have the same value even if you entered the URL directly in your browser, as long as it was from within the same browser (eg: don't start another brower instance and enter the URL, do it in a new tab of the same browser).
0
 

Author Comment

by:linbayzak
Comment Utility
No, nothing in the error log. I had a script that wouldn't allow manual entry into the URL (and for our clients they can't even see it, so it's secure to the extent we need it to be), and I disabled that so I could enter it into the same window, and I get the same results.
0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
What webserver are you using?   Normally, whenver there is an internal server error, there is an entry in the log file.

I'm not understanding how entering the URL directly doesn't work.  The webserver shouldn't know (or care) if the request is a modal window, a regular popup window, or a top page by itself.

Whatever this:
$Action?app=ShowThisPic&UserID=$FORM->{StoreNum}&Acct=$FORM->{BankAcctNum}&Routing=$FORM->{RoutingNum}&CheckNum=$FORM->{CheckNum}&Amt=$FORM->{Amt}
works out to, if you enter that in your URL bar, you should get that page.  You will first have to go through whatever login pages your site requires.  But once the session is created, it will either be passed through a cookie, or through the query string - and I don't see anything that looks like a session in the query string above, so I'm guessing it's in a cookie - which will be sent whether the request is for a window within a page, or the URL bar.

0
 

Author Comment

by:linbayzak
Comment Utility
No, I was saying that it was the same as if I ran the script. The internal server error was when I wasn't including a header, because I was seeing "-Type: image/tiff" in the character string, so I thought the header might somehow be put in twice, but it wasn't. So, yes, typing in what all the variables resolve at and everything into the URL bar still gives me the same result as running the script itself.
0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
Test these by just entering the URL directly.

First try test1.  If that doesn't work, then try test2.  Post the output of both here...

#################### Test 1

sub ShowThisPic{

  my($DB,$FORM,$SESS) = @_;

 

  my $DB21 = modules::Db->ConnectCheck21();

 

  my $LongReadLen = $DB21->{dbh}->selectrow_array(qq{SELECT MAX(DATALENGTH(Img)) FROM Image_View});

  $DB21->{dbh}->do("set textsize $LongReadLen");

 

  my $sql = "SELECT Img, CheckNum

             FROM Image_View

             WHERE UserID = '$FORM->{UserID}'

             AND Acct = '$FORM->{Acct}'

             AND Routing = '$FORM->{Routing}'

             AND CheckNum = '$FORM->{CheckNum}'

             AND Amt = '$FORM->{Amt}'";

 

  my $rs = $DB21->SelectHash($sql);

  print header('image/tiff'); ### I have tried this line both commented and not.

  

  my @keys = keys %$rs;

  binmode(STDOUT);

  print $rs->{$keys[0]}->{Img};

}
 

#################### Test 2

sub ShowThisPic{

  my($DB,$FORM,$SESS) = @_;

  print header('text/plain');

  print "Running ShowThisPic\n";

  print "  DB=$DB\n";

  print "  FORM=$FORM\n";

  print "  SESS=$SESS\n";

  print "  $_ = $_[$_]\n" for(3..$#_);

}

Open in new window

0
 

Author Comment

by:linbayzak
Comment Utility
Okay, the first test prints out a long string of characters: 49492a00886700000557fffffcb4c4229d11992a46114e88e8da379e46d
1844788e291f2f11d91c1488e1920dcbc5f25889745d1c447c8f1b191d
11f23e611865d1c44bacb81e0ae473239970a4788f98c8e88e88e0e47...and so on

The second one prints:

Running ShowThisPic
  DB=modules::Db=HASH(0xa85e834)
  FORM=HASH(0xa861b7c)
  SESS=HASH(0xa86f2ec)

0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
What webserver are you using?

With either, do you see something like:
    Content-Type: text/plain
    Content-Type: image/tiff
at the beginning of the output?

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:linbayzak
Comment Utility
Well, we're using a Unix box. I'm unsure of the exact version. No, I do not get those printed out unless I were to put two headers on the same page, then the second one would be visible.
0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
Can you try it with FireFox with the LiveHTTPHeaders plug-in installed.  Try the first test again, and post the output from the headers window.

From the output, it looks like there is a header being sent before the image/tiff header.
0
 

Author Comment

by:linbayzak
Comment Utility
https://[ip was here]/cgi-bin/dev/Dispatch.pl?app=ShowThisPic&UserID=0300&Acct=7160012907&Routing=7241329&CheckNum=0001500931&Amt=150

GET /cgi-bin/dev/Dispatch.pl?app=ShowThisPic&UserID=0300&Acct=7160012907&Routing=7241329&CheckNum=0001500931&Amt=150 HTTP/1.1
Host: [ip was here]
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: CashExpressLogin=[ip was here]|session=|user='David Savage'|store=0200|user_id=4|time_zone=C; Set-Cookie: CurrentPage='' expires='-1d'; CurrentPage=Main Menu

HTTP/1.x 200 OK
Date: Mon, 20 Oct 2008 15:06:17 GMT
Server: Apache/2.0.63 (FreeBSD) mod_ssl/2.0.63 OpenSSL/0.9.7e-p1
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: image/tiff
0
 

Author Comment

by:linbayzak
Comment Utility
Funny thing is in FireFox it actually tries to download the Dispatch.pl as a TIF file when i type the address into the URL. IE just gave me a page of random characters like I showed you. But, the file when downloaded is not viewable.
0
 

Author Comment

by:linbayzak
Comment Utility
Same thing happens if I run it in the program. I click on the button, a pop-up comes up, and then I get a download prompt for Dispatch.pl as a TIF file. In IE, I would get the pop-up but filled with random characters.
0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
Are you sure the data is a TIFF type of file?
0
 

Author Comment

by:linbayzak
Comment Utility
Well, that's what we've been told by the company that is providing our image using their software. Do you think perhaps there's something wrong with the image itself?
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
Comment Utility
A TIFF file should have the second 16-bit value contain 42, which means the 3rd and 4th bytes should be either hex 2A00 for little-endian, or 002A for big-endian.

Your string looks like a hex representation of this:  49492a00....
Where each byte is represented by 2 hex characters.

It looks like maybe the TIFF data was encoded.  Here is another test that assumes the data was encoded as a hex string, and decodes it.

Give this version a try, and let me know what you get.
#################### Test 3

sub ShowThisPic{

  my($DB,$FORM,$SESS) = @_;

 

  my $DB21 = modules::Db->ConnectCheck21();

 

  my $LongReadLen = $DB21->{dbh}->selectrow_array(qq{SELECT MAX(DATALENGTH(Img)) FROM Image_View});

  $DB21->{dbh}->do("set textsize $LongReadLen");

 

  my $sql = "SELECT Img, CheckNum

             FROM Image_View

             WHERE UserID = '$FORM->{UserID}'

             AND Acct = '$FORM->{Acct}'

             AND Routing = '$FORM->{Routing}'

             AND CheckNum = '$FORM->{CheckNum}'

             AND Amt = '$FORM->{Amt}'";

 

  my $rs = $DB21->SelectHash($sql);

  my @keys = keys %$rs;

  my $picdata = $rs->{$keys[0]}->{Img};

  $picdata = pack('H*', $picdata);

  print header('image/tiff');

  

  binmode(STDOUT);

  print $picdata;

}

Open in new window

0
 

Author Comment

by:linbayzak
Comment Utility
Awesome! When I type that into the address bar it asks for me to download an image, which is the graphic of the check! The problem now being that it only works when typing it into the address bar. The modalDialogBox pops up but stays blank when running the same app from my form. Any ideas on correcting this aspect and I should be fixed up!
0
 

Author Comment

by:linbayzak
Comment Utility
Another note: I will have two images per check so I need to have each display in the dialog box. I'm thinking this would be the way to do it.


sub ShowThisPic{

  my($DB,$FORM,$SESS) = @_;
 

  my $DB21 = modules::Db->ConnectCheck21();
 

  my $LongReadLen = $DB21->{dbh}->selectrow_array(qq{SELECT MAX(DATALENGTH(Img)) FROM Image_View});

  $DB21->{dbh}->do("set textsize $LongReadLen");
 

  my $sql = "SELECT Img, CheckNum

             FROM Image_View

             WHERE UserID = '$FORM->{UserID}'

             AND Acct = '$FORM->{Acct}'

             AND Routing = '$FORM->{Routing}'

             AND CheckNum = '$FORM->{CheckNum}'

             AND Amt = '$FORM->{Amt}'";
 

  my $rs = $DB21->SelectHash($sql);
 

  print header('image/tiff');

  foreach(keys(%$rs)){

    my $picdata = $rs->{$_}->{Img};

    $picdata = pack('H*', $picdata);
 

    binmode(STDOUT);

    print $picdata;

    print "<br>";

  }

}

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
The browser does not show TIFF type images.  This is why you get a dialog asking you to save it when you enter the URL, and why the modalDialogBox doesn't show anything.  You will need the images in a format the browser will display, such as JPEG or GIF.

You could batch process the entire database, and store the modified format in the database (more space, but faster), or process the TIFF image data on an as-needed basis (less space, but slower).
0
 

Author Comment

by:linbayzak
Comment Utility
Ok, I know I'm being a pain here...but how would I go about changing the format on the fly after downloading from the database? I have ImageMagick installed if that helps anything. I was looking at their documentation but it is unclear for changing format without writing it to a directory. I am unsure how to actually go about this.
0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
A single request can only return a single image.  So to show two images, you do:
1) Have the modalDialogBox load an HTML page that contains <img src="..."> twice, once for each image
2) Have the script read both images from the database, and join them into one image

You can use "-" as the filename in Write to write to standard out:
   $image->Write("gif:-");  #Write image to standard out in GIF format
0
 

Author Comment

by:linbayzak
Comment Utility
Ok, I am sorry if I am coming across as inexperienced here...I've just never had to do anything like this before. I'm trying a script like you are suggesting but only get two broken images when I run it from URL bar or from the actual program.
sub ShowThisPic{

  my($DB,$FORM,$SESS) = @_;
 

  my $DB21 = modules::Db->ConnectCheck21();
 

  my $LongReadLen = $DB21->{dbh}->selectrow_array(qq{SELECT MAX(DATALENGTH(Img)) FROM Image_View});

  $DB21->{dbh}->do("set textsize $LongReadLen");
 

  my $sql = "SELECT Img, CheckNum

             FROM Image_View

             WHERE UserID = '$FORM->{UserID}'

             AND Acct = '$FORM->{Acct}'

             AND Routing = '$FORM->{Routing}'

             AND CheckNum = '$FORM->{CheckNum}'

             AND Amt = '$FORM->{Amt}'";
 

  my $rs = $DB21->SelectHash($sql);
 

  modules::Io->TextHtmlHeader();

  my ($image, $x);

  $image = Image::Magick->new;

  my $picdata;

  foreach(keys(%$rs)){

    $picdata = $rs->{$_}->{Img};

    $picdata = pack('H*', $picdata);
 

    binmode(STDOUT);

    $x = $image->Read('$picdata');

  }

  $x = $image->Write("gif:-");

  print "<img src='...'><img src='...'>";

}

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
If you go with option 1 above, the HTML page will have to look like this, where $UserID, $Acct, $Routing, $CheckNum, and $Amt all have appropriate values.

Then the ShowThisPic subroutine will have to look at the Which field to determine whether it should show the first or second picture.

<html>

  <body>

    <img src="/cgi-bin/dev/Dispatch.pl?app=ShowThisPic&UserID=$UserID&Acct=$Acct&Routing=$Routing&CheckNum=$CheckNum&Amt=$Amt&Which=1">

    <br />

    <img src="/cgi-bin/dev/Dispatch.pl?app=ShowThisPic&UserID=$UserID&Acct=$Acct&Routing=$Routing&CheckNum=$CheckNum&Amt=$Amt&Which=2">

  </body>

</html>

Open in new window

0
 

Author Comment

by:linbayzak
Comment Utility
Ok, I think I almost have it. I am still getting broken images, but I think I'm close to what I need. Here's what I'm using right now. What am I missing? Should I change the header?
sub ShowThisPic2{

  my($DB,$FORM,$SESS) = @_;
 

  my $DB21 = modules::Db->ConnectCheck21();
 

  my $LongReadLen = $DB21->{dbh}->selectrow_array(qq{SELECT MAX(DATALENGTH(Img)) FROM Image_View});

  $DB21->{dbh}->do("set textsize $LongReadLen");
 

  my $sql = "SELECT Img, CheckNum

             FROM Image_View

             WHERE UserID = '$FORM->{UserID}'

             AND Acct = '$FORM->{Acct}'

             AND Routing = '$FORM->{Routing}'

             AND CheckNum = '$FORM->{CheckNum}'

             AND Amt = '$FORM->{Amt}'";
 

  my $rs = $DB21->SelectHash($sql);

  my @keys = keys %$rs;

  my $picdata = $rs->{$keys[$FORM->{Which}]}->{Img};

  $picdata = pack('H*', $picdata);

  print header('image/tiff');
 

  binmode(STDOUT);

  my $image = Image::Magick->new;

  open(IMAGE, "$picdata");

  $image->Write("gif:-");

}
 

sub ShowThisPic{ ### This is the sub called by the modalDialogBox

  my ($DB,$FORM,$SESS) = @_;
 

  my $Action = modules::Io->Server();

  modules::Io->TextHtmlHeader();
 

  print<<EOD;

  <img src="$Action?app=ShowThisPic2&UserID=$FORM->{UserID}&Acct=$FORM->{Acct}&Routing=$FORM->{Routing}&CheckNum=$FORM->{CheckNum}&Amt=$FORM->{Amt}&Which=0"><br>

  <img src="$Action?app=ShowThisPic2&UserID=$FORM->{UserID}&Acct=$FORM->{Acct}&Routing=$FORM->{Routing}&CheckNum=$FORM->{CheckNum}&Amt=$FORM->{Amt}&Which=1"><br>

EOD

}

Open in new window

0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 500 total points
Comment Utility

sub ShowThisPic2{

  ...

  binmode(STDOUT);

  my $image = Image::Magick->new;

  $image->read(blob=>$picdata);

  $image->Write("gif:-");

}

Open in new window

0
 

Author Closing Comment

by:linbayzak
Comment Utility
Thank you so much for your help, I know it's been a long drawn out question. It is highly appreciated!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now