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

I am looking for ideas here. The problem is thus:

I have an MS-SQL database that contains information about people--name, phone, address, ID, etc. I connect to it via a Win32::ODBC connection to return demographic information. Everything works, but the image display is sloppy and I don't like sloppy.

Images (jpgs) are stored in outside the SQL server in a separate folder on the webserver. (There is no OLE field available in the database to place link info.) They are named according the the person's ID. For example John Doe would have the ID "DOEJ" and his image file would be "DOEJ.jpg".The image link is built dynamically in my Perl script and inserted into the HTML display by concatenating the src link with the ID pulled from the data base. Unfortunately, not all the people have photos. The script builds the link and passes it to the browser which displays a broken image icon when there is no picture behind the seemingly legitimate link.

Ideally, I'd like to be able to catch the problem before the data is sent to the browser and substitute a "no picture available for this person" message or image for the broken image link. I'm uncertain of the best approach for this, so I'm tossing it out here for the creative minds of the monastery. How and where can I test to see if there's an image in the image folder before I build the link from the ID data?

WB

Replies are listed 'Best First'.
Re: CGI-SQL-HTML-Display "no image"
by Zaxo (Archbishop) on May 24, 2003 at 04:53 UTC

    Once you've constructed the filename, $fn, you can use a file test operator to check if it exists.

    my $fn = make_filename($user); # or whatever $fn = '/path/to/default.jpg' unless -f $fn; print link_pic($fn);
    to link a default image, or
    if ( -f $fn ) { print link_pic($fn); }
    to simply leave out the link.

    After Compline,
    Zaxo

      I'm going to try this. Regarding program flow, I'm assuming that the creation and test for the file name would come in somewhere after I've fetched the data from the database and the
      print link_pic($fn);
      would be sent to the HTML display. Is that correct? Thanks.
      I've probably wandered a bit from the original suggestion, but I didn't have much luck with 'unless'. I'm better with 'if' and the following statments work on my local PC, giving me the file name when the file exists and DefaultPhoto when the file is not present.(-e worked, while -f did not.)

      $path = "/dir_name/sub_dir_name/"; #Determine if the image file exists my $fn = $path.$Data{dr_id}.'.jpg'; if (-e $fn) { my $image = $fn; } else { $image = $path."DefaultPhoto.gif";

      When I post it to my server, it doesn't seem to find the file. It returns the requested person's information, but returns the DefaultPhoto.jpg even when the image file exists. I thought perhaps the problem was in my path name somewhere, so I printed $fn and $image to the browser. I'm pulling a person that should have a photo and I get the following back:

      File is: /dir_name/sub_dir_name/BOWG.jpg Image is: /dir_name/sub_dir_name/DefaultPhoto.gif
      The browser displays $image (DefaultPhoto.jpg) and returns the link in the HTML below

      <TR><TD><img src=/dir_name/sub_dir_name/DefaultPhoto.gif></TD></TR>

      So, I'm stumped. I am missing something but I can't see it. Is there some reason I can't actually check the file's existence on the server when I coming through a CGI script? Is there another way to do this?
      TIA,
      WB

        You need to present the img tag with src as an url. That will take transforming the filesystem path to something like 'http://host.nil/' . substr $fn, length( $ENV{DOCUMENT_ROOT})

        After Compline,
        Zaxo

Re: CGI-SQL-HTML-Display "no image"
by CountZero (Bishop) on May 24, 2003 at 08:44 UTC

    You can have an extra (boolean) field in your database which indicates whether or not there is a photo available.

    It will save you the time-consuming task of checking through the file-system whether a file is available.The "dark side" of this solution is of course that you must keep you data-base updated. There is no gain without pain.

    Or you can have a "default image" on the file system for each entry, which shows something like "no picture available yet" and when the "real" photo is put in the file-system it will automatically overwrite the default image.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Thanks for the reply. Sadly, I can't touch the structure of the database. Fixing the problem at the source would be the way I would do it if I could.

      Default image is a good idea. Laziness keeps me from wanting to create a separate image for each missing photo, though. (We're talking a hundred or more photos.) If I can think of a way to automate the process, all the better. There must be a module or something out there that would create repetitive images?

        Read the original reply by Zaxo more closely. You don't need a default image for every missing image...you just need one for all the missing images. That's what Zaxo's code above demonstrates.

        Mark