File Coverage

blib/lib/WebService/Images/Nofrag.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package WebService::Images::Nofrag;
2              
3 2     2   54463 use warnings;
  2         6  
  2         65  
4 2     2   11 use strict;
  2         4  
  2         63  
5 2     2   12 use Carp;
  2         7  
  2         184  
6 2     2   12250 use WWW::Mechanize;
  2         703300  
  2         101  
7 2     2   24 use base qw(Class::Accessor::Fast);
  2         6  
  2         2096  
8 2     2   116072 use Image::Magick;
  0            
  0            
9             use Image::Magick::Info qw( get_info );
10             use LWP::Simple;
11              
12             WebService::Images::Nofrag->mk_accessors( qw(thumb image url) );
13              
14             =head1 NAME
15              
16             WebService::Images::Nofrag - upload an image to http://pix.nofrag.com
17              
18             =head1 VERSION
19              
20             Version 0.06
21              
22             =cut
23              
24             our $VERSION = '0.06';
25             our $SITE = 'http://pix.nofrag.com/';
26              
27             =head1 SYNOPSIS
28              
29             my $pix = WebService::Images::Nofrag->new();
30             $pix->upload({file => '/path/to/the/file'});
31            
32             # or
33             $pix->upload({file => '/path/to/the/file'}, '800x600');
34            
35             # or
36             $pix->upload({url => 'http://test.com/my/file.jpg', '50%'});
37            
38             print "URL : " . $pix->url . "\n"; # print the url of the page
39             print "image : " . $pix->image . "\n";# print the url of the image
40             print "thumb : " . $pix->thumb . "\n";# print the url of the thumb
41            
42             =cut
43              
44             =head2 upload
45              
46             upload an image to http://pix.nofrag.com
47            
48             We need a filemane or an URL to an image.
49            
50             You can specify a resolution, so the image will be resized before being
51             uploaded.
52            
53             Set 3 accessors, thumb image & url, with the url to the different
54             data.
55            
56             =cut
57              
58             sub upload {
59             my ( $self, $params ) = @_;
60              
61             my $tempory_file = "WIN_temp_file";
62              
63             if ( !defined $$params{ file } && !defined $$params{ url } ) {
64             croak "Please, give me a file or an url";
65             }
66              
67             if ( defined $$params{ file } && !-r $$params{ file } ) {
68             croak "Problem, can't read this file";
69             }
70              
71             if ( defined $$params{ url } ) {
72             getstore( $$params{ url }, $tempory_file );
73             }
74              
75             # do we need to resize ?
76             if ( defined $$params{ resize } ) {
77             my $img = new Image::Magick;
78             if ( -f $tempory_file ) {
79             $img->Read( $tempory_file );
80             } else {
81             $img->Read( $$params{ file } );
82             }
83             $img->Resize( $$params{ resize } );
84             $img->Write( $tempory_file );
85             }
86              
87             if ( -f $tempory_file ) {
88             my $info = get_info( $tempory_file, ( "filesize" ) );
89             if ( $info->{ filesize } > 2000000 ) {
90             croak( "File can't be superior to 2MB" );
91             }
92             } else {
93             my $info = get_info( $$params{ file }, ( "filesize" ) );
94             if ( $info->{ filesize } > 2000000 ) {
95             croak( "File can't be superior to 2MB" );
96             }
97             }
98              
99             $self->{ options } = shift;
100             $self->{ mech } = WWW::Mechanize->new();
101              
102             $self->{ mech }->get( $SITE );
103              
104             if ( -f $tempory_file ) {
105             $self->{ mech }->field( 'monimage', $tempory_file );
106             } else {
107             $self->{ mech }->field( 'monimage', $$params{ file } );
108             }
109              
110             $self->{ mech }->click_button( input =>
111             $self->{ mech }->current_form()->find_input( undef, "submit" ) );
112              
113             if ( $self->{ mech }->content =~ /Impossible to process this picture!/ ) {
114             $self->url( "none" );
115             $self->image( "none" );
116             $self->thumb( "none" );
117             croak "\tProblem, can't upload this file\n";
118             }
119              
120             if ( $self->{ mech }->res->is_success ) {
121             my $content = $self->{ mech }->content;
122             $content =~ /\[url=(http:\/\/pix\.nofrag\.com\/.*\.html)\]/;
123             $self->url( $1 );
124             $content =~ /\[img\](http:\/\/pix\.nofrag\.com\/.*)\[\/img\]/;
125             $self->image( $1 );
126             my @img = $self->{ mech }->find_all_images();
127             foreach my $img ( @img ) {
128             last if $self->thumb;
129             if ( $img->url =~ /^$SITE/ ) {
130             $self->thumb( $img->url );
131             }
132             }
133             } else {
134             croak "Problem, can't upload this file.";
135             }
136              
137             if ( -f $tempory_file ) {
138             unlink $tempory_file;
139             }
140             }
141              
142             =head1 AUTHOR
143              
144             Franck Cuny, C<< <franck.cuny at gmail.com> >>
145              
146             =head1 BUGS
147              
148             Please report any bugs or feature requests to
149             C<bug-webservice-images-nofrag at rt.cpan.org>, or through the web interface at
150             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WebService-Images-Nofrag>.
151             I will be notified, and then you'll automatically be notified of progress on
152             your bug as I make changes.
153              
154             =head1 SUPPORT
155              
156             You can find documentation for this module with the perldoc command.
157              
158             perldoc WebService::Images::Nofrag
159              
160             You can also look for information at:
161              
162             =over 4
163              
164             =item * AnnoCPAN: Annotated CPAN documentation
165              
166             L<http://annocpan.org/dist/WebService-Images-Nofrag>
167              
168             =item * CPAN Ratings
169              
170             L<http://cpanratings.perl.org/d/WebService-Images-Nofrag>
171              
172             =item * RT: CPAN's request tracker
173              
174             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WebService-Images-Nofrag>
175              
176             =item * Search CPAN
177              
178             L<http://search.cpan.org/dist/WebService-Images-Nofrag>
179              
180             =back
181              
182             =head1 ACKNOWLEDGEMENTS
183              
184             =head1 COPYRIGHT & LICENSE
185              
186             Copyright 2006 Franck Cuny, all rights reserved.
187              
188             This program is free software; you can redistribute it and/or modify it
189             under the same terms as Perl itself.
190              
191             =cut
192              
193             1;
194              
195             __END__