perl: Image grab REGEX

Hi

From the Image-Grab page on cpan

  use Image::Grab;
  my $url ='http://www.example.com/FileName.html'
                      my $pic = Image::Grab->new(SEARCH_URL=>$url,
                                    REGEXP    =>'.*\.jpg');
                  $pic->grab;
                  my $File = basename($url);
                      $File =~ s/html/jpg/i;
                    # Now to save the image to disk
                    open(IMAGE, ">$File") || die"$File: $!";
                    binmode IMAGE;  # for MSDOS derivations.
                    print IMAGE $pic->image;
                    close IMAGE;

Open in new window


I'm getting the error "Couldn't determine an absolute URL!" because the html page I'm searching uses upper case 'JPG' so the regex fails

How do I make the REGEX non case sensitive?

Also can you determine the File name of the image from the REGEX?
" my $File = $pic->name;" causes another error
Can't access `name' field in class Image::Grab
LVL 1
trevor1940Asked:
Who is Participating?
 
ozoCommented:
REGEXP    =>qr'.*\.jpg'i
or
REGEXP    =>'(?i).*\.jpg'

You can get the name from $pic->expand_url
0
 
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Well... It appears Image::Grab fails to support passing a pre-compiled regex, so you can't easily say ignore case... so, I'd try this...

.+\.[jJ][pP][eE]?[gG]$

Open in new window

0
 
trevor1940Author Commented:
In case someone else has similar issue this worked

my $Fold = "Path/To/SaveJPG";
                     unless (-d "$Fold")
                      {
                       make_path("$Fold");
                      }                      

            my $uri   = URI->new($url);
            my $pic = Image::Grab->new(SEARCH_URL=>$url,
            REGEXP    =>qr'.*\.jpg'i); 
            $pic->grab;
            my $File = $Fold . "\\" . basename($pic->expand_url);
           ## check if has extension sometimes basename returns just file name
            unless($File =~m/jpg$/i){
             $File .= ".jpg";
            }
            print "File $File\n";
            
            # Now to save the image to disk
            open(IMAGE, ">$File") || die"$File: $!";
            binmode IMAGE;  # for MSDOS derivations.
            print IMAGE $pic->image;
            close IMAGE;

Open in new window

0
 
trevor1940Author Commented:
Thanx ozo
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.