File Coverage

blib/lib/Image/Imgur.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Image::Imgur;
2             # Imgur.pm
3             # simple perl module for uploading pics to imgur.com
4              
5 1     1   22090 use MIME::Base64;
  1         963  
  1         74  
6 1     1   1756 use LWP;
  1         61981  
  1         36  
7 1     1   11 use strict;
  1         7  
  1         31  
8 1     1   5 use warnings;
  1         1  
  1         36  
9 1     1   450 use Moose; # a Mouse is fine too
  0            
  0            
10              
11             has 'key' => (is => 'rw', isa => 'Str');
12              
13             # errors:
14             # 0 -- no api key
15             # -1 -- problem with the file
16             sub upload {
17             my $self = shift;
18             my $image_path = shift;
19             return 0 unless($self->key);
20             my $res;
21             if($image_path =~ /http:\/\//) {
22             $res = $image_path;
23             } else {
24             my $fh;
25             open $fh,"<", $image_path or return -1;
26             $res = _read_file($image_path);
27             }
28             $res = $self->_upload($res);
29             return $res;
30             }
31              
32              
33             # base64'd image
34             sub _read_file {
35             my $fname = shift;
36             my $fh;
37             open $fh, "<", $fname or return -1;
38             binmode $fh;
39             return encode_base64(join("" => <$fh>));
40             }
41              
42             # errors:
43             # 1000 No image selected
44             # 1001 Image failed to upload
45             # 1002 Image larger than 10MB
46             # 1003 Invalid image type or URL
47             # 1004 Invalid API Key
48             # 1005 Upload failed during process
49             # 1006 Upload failed during the copy process
50             # 1007 Upload failed during thumbnail process
51             # 1008 Upload limit reached
52             # 1009 Animated GIF is larger than 2MB
53             # 1010 Animated PNG is larger than 2MB
54             # 1011 Invalid URL
55             # 1012 Could not download the image from that URL
56             # 9000 Invalid API request
57             # 9001 Invalid response format
58             # -3 Something is really wrong...
59             # else: image url
60             sub _upload {
61             my $self = shift;
62             my $image = shift;
63             return undef unless($image);
64             my $user_a = LWP::UserAgent->new(agent => "Perl");
65             my $res = $user_a->post('http://imgur.com/api/upload.xml', ['image' => $image, 'key' => $self->key]);
66             if($res->content =~ /<original_image>(.*?)<\/original_image>/) {
67             return $1;
68             } elsif ($res->content =~ /<error_code>(\d+)<\/error_code>/) {
69             return $1;
70             } else {
71             return -3;
72             }
73             }
74              
75             1;
76             __END__
77              
78             =head1 NAME
79              
80             Image::Imgur - Perl extension for uploading images to http://imgur.com
81              
82             =head1 SYNOPSIS
83              
84             use Image::Imgur;
85             my $key = "IMGUR-API-KEY"; # dev key
86             # if you don't have imgur api key, you can get one here: http://imgur.com/register/api/
87             my $url1 = $img_up->upload('http://i.cdn.turner.com/cnn/.element/img/3.0/global/header/intl/hdr-globe-east.gif');
88             my $url2 = $img_up->upload('/usr/local/www/data/host.jpg');
89              
90             =head1 DESCRIPTION
91              
92             Image::Imgur intends to make programmatically possible to upload image files to the website http://imgur.com.
93              
94             The maximum non-animated file size you can upload is 10MB. However, if the image is over 1MB then it will automatically be compressed or resized to 1MB, for better viewing on the net. The maximum animated file size (both GIF and PNG) is 2MB.
95              
96             This module uses LWP and Moose (Mouse will work too).
97             Also you'll need a working internet connection (duh).
98              
99             =head2 Method Summary
100              
101             =over 4
102              
103             =item host($image)
104              
105             Given an url or a filename uploads the image to imagur.com and returns the url.
106              
107             =back
108              
109             =head1 SEE ALSO
110              
111             http://imgur.com
112             L<LWP::UserAgent|LWP::UserAgent>
113             L<Moose|Moose>
114              
115             =head1 AUTHOR
116              
117             D. Frumin, <lt>ohwow@cpan.org<gt>
118              
119             =head1 COPYRIGHT AND LICENSE
120              
121             Copyright (C) 2010 by Ivan Ivanov
122              
123             This library is free software; you can redistribute it and/or modify
124             it under the same terms as Perl itself, either Perl version 5.10.1 or,
125             at your option, any later version of Perl 5 you may have available.
126              
127              
128             =cut