The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Fetch::Image - fetch a remote image into a File::Temp

SYNOPSIS

use Fetch::Image;
use Try::Tiny; #or just use eval {}, it's all good

my $fetcher = Fetch::Image->new( {
    'max_filesize' => 524_288,
    'user_agent' => 'mozilla firefox or something...',
    'allowed_types' => {
        'image/png' => 1,
        'image/jpg' => 1,
        'image/jpeg' => 1,
        'image/pjpeg' => 1,
        'image/bmp' => 1,
        'image/gif' => 1,
    },
} );

my $image_info = try{
    $fetcher->fetch('http://www.google.com/logos/2011/trevithick11-hp.jpg');
} catch {
    #error gets caught here...
    warn $_; #this
    warn $_->error; #or this
};

use Data::Dumper;
warn Dumper( $image_info );

#the image is now a Temp::File in $image_info->{'temp_file'};

DESCRIPTION

Class that will fetch a remote image and return a hash of the image_info and the File::Temp

METHODS

new

takes 3 options

my $fetcher = Fetch::Image->new( {
    'max_filesize' => 524_288, #default value (bytes)
    'user_agent' => 'mozilla firefox or something...',
    'allowed_types' => {  #allowed content types (default all of these)
        'image/png' => 1,
        'image/jpg' => 1,
        'image/jpeg' => 1,
        'image/pjpeg' => 1,
        'image/bmp' => 1,
        'image/gif' => 1,
    },
} );

fetch

takes 1 argument, the url of the image to fetch

returns a hash of the image info, from Data::Validate::Image, with an extra property, 'temp_file' which is the File::Temp

AUTHORS

Mark Ellis <markellis@cpan.org>

SEE ALSO

Data::Validate::Image, File::Temp

LICENSE

Copyright 2014 by Mark Ellis <markellis@cpan.org>

This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself.