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

Hey monks, I've been using "perlmagic" and I get the following error:

Error reading file /websites/phorm/www/pics/animals/shippo.gif No such file or directory at /websites/snowbunny/cgi-bin//gallery.cgi line 53

(not sure what's with the //gallery.cgi, that's a seperate issue though) The code is using "perlmagick" in an attempt to create an image montage, as below. I've checked as the www-data (apache) user and can DEFINATELY read the file in question.
#!/usr/bin/perl use CGI; use Image::Magick; use CGI::Carp qw(fatalsToBrowser); my $cgi = new CGI; $magick=Image::Magick->new(); $images=Image::Magick->new(); ######## my $basepath = "/websites/snowbunny/www"; my $path = $cgi->url_param("path"); my %filetypes; $filetypes{jpg}="jpeg"; $filetypes{gif}="gif"; #### $path =~ tr/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345 +6789\ \_\-\///c; $path =~ s/\/\///gis; $path =~ s/\.\.//gis; $path = "gallery1/dec_2004"; my $fullpath = "$basepath/$path"; #Just in case the cgi path was mangled, but it isn't!!! $fullpath="/websites/phorm/www/pics/animals"; if ( -d $fullpath ) { opendir(DIR, "$fullpath"); @files = readdir(DIR); my @pictures; my $picture_count; foreach $file(@files) { # warn("Checking $file"); if ($file =~ /\.([\w\d]+)$/) { $extension = lc($1); if ( defined($filetypes{$extension} ) ) { my $filepath = "$fullpath/$file"; #$filepath =~ s/\ /\\\ /gi; $pictures[$picture_count] = Image::Magick->new(); warn("# of pictures: " . @$images); open (IMAGE_IN, "<$filepath") or die("Cannot read file $filepa +th\n$!"); #pictures[$picture_indx] = $pictures[$picture_count]->ReadImag +e("$filepath") or die("Cannot read $filepath"); # $pictures[$picture_indx] = $pictures[$picture_count]->Read(fil +e=>\*IMAGE_IN) or die("Error reading file $filepath $!"); close(IMAGE_IN); $pictures[$picture_count]->Label("$filename"); push(@$images, $pictures[$picture_indx] ); $picture_count++; } } } if ( $picture_count > 0) { my $montage=$images->Montage(geometry=>'130x194+10+5>',gravity +=>'Center', bordercolor=>'green',borderwidth=>1,tile=>'5x1000',compose=> +'over', background=>'#ffffff',font=>'Generic.ttf',pointsize=>18,fi +ll=>'#600', stroke=>'none'); $montage->Set(matte=>'false'); print "Content-type: image/jpeg\n\n"; $montage->Write('jpg:-'); } else { print "Content-type: text/html\n\n"; print "no pictures in $fullpath"; } } else { die("Invalid path $fullpath"); }

Replies are listed 'Best First'.
Re: PerlMagick "file not found" issues
by g0n (Priest) on Mar 17, 2005 at 11:30 UTC
    I can't see anything immediately wrong with the line in question. Perhaps you could try the following

    use CGI; use strict; my $cgi = new CGI; print $cgi->header; print $cgi->start_html; open (FH,"<full path of your file>") or print "Can't :$!"; close FH; print $cgi->end_html;

    or something similar. That should narrow down whether you have a code problem (which looks unlikely) or a problem actually reading the file, for whatever (possibly apache related) reason.

    g0n, backpropagated monk
      Ahhh, but that's the problem. I can in fact, read the file.

      This is the line it indicates it is croaking at.
      $pictures[$picture_indx] = $pictures[$picture_count]->Read(file=>\*IMA +GE_IN) or die("Error reading file $filepath $!");
      So that means the OPEN command has already succeeded... imagemagick has no reason to screw up...
      I'm thinking perhaps PM is just messed on my box, though it's a debian/stable stock version. The images aren't corrupt either, as they show fine from my browser when viewed from outside a script. The error line it mentions is actually this:
Re: PerlMagick "file not found" issues
by PodMaster (Abbot) on Mar 18, 2005 at 07:43 UTC
    What does Read return? Last I checked Image::Magick didn't store errors in $!

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.