ImageRenderFilter.pm untested

filter: Statementstmt 0.0% 0 / 210 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 277 CC 20 SCAR 51.0
CC
20
Cov
0%
handler
342.0
correct_number
6.0
loadConfigFile
6.0
CRAP 163.5
1 #file:Apache2/ImageRenderFilter.pm;
2 #--------------------------------
3  
4 #
5 # Created by Idel Fuschini
6 # Date: 15/12/09
7 # Site: http://www.idelfuschini.it
8 # Mail: idel.fuschini@gmail.com
9  
10  
11  
12 package Apache2::ImageRenderFilter;
13
14 use strict;
15 use warnings;
16 use Apache2::AMFCommonLib ();
17
18 use Apache2::RequestRec ();
19 use Apache2::RequestUtil ();
20 use Apache2::SubRequest ();
21 use Apache2::Log;
22 use Apache2::Filter ();
23 use APR::Table ();
24 use LWP::Simple;
25 use Image::Resize;
26 use Apache2::Const -compile => qw(OK REDIRECT DECLINED HTTP_MOVED_TEMPORARILY);
27 use constant BUFF_LEN => 1024;
28  
29  
30 #
31 # Define the global environment
32 #
33  
34 use vars qw($VERSION);
35 $VERSION= "2.21";
36 my $CommonLib = new Apache2::AMFCommonLib ();
37 my %Capability;
38 my %Array_fb;
39 my %Array_id;
40 my %Array_fullua_id;
41 my %Array_DDRcapability;
42  
43 my %XHTMLUrl;
44 my %WMLUrl;
45 my %CHTMLUrl;
46 my %ImageType;
47 my %cacheArray;
48 my %cacheArray_toview;
49
50  
51 my $intelliswitch="false";
52 my $mobileversionurl;
53 my $fullbrowserurl;
54 my $querystring="false";
55 my $showdefaultvariable="false";
56 my $wurflnetdownload="false";
57 my $downloadwurflurl="false";
58 my $resizeimagedirectory="";
59 my $downloadzipfile="true";
60 my $virtualdirectoryimages="false";
61 my $virtualdirectory="";
62 my $repasshanlder=0;
63 my $globalpassvariable="";
64 my $log4wurfl="";
65 my $loadwebpatch="false";
66 my $dirwebpatch="";
67 my $patchwurflnetdownload="false";
68 my $patchwurflurl="";
69 my $redirecttranscoder="true";
70 my $redirecttranscoderurl="";
71 my $detectaccuracy="false";
72 my $listall="false";
73
74 $ImageType{'image/png'}="png";
75 $ImageType{'image/gif'}="gif";
76 $ImageType{'image/jpg'}="jpg";
77 $ImageType{'image/jpeg'}="jpeg";
78
79 #
80 # Check if MOBILE_HOME is setting in apache httpd.conf file for example:
81 # PerlSetEnv MOBILE_HOME <apache_directory>/MobileFilter
82 #
83 $CommonLib->printLog("---------------------------------------------------------------------------");
84 $CommonLib->printLog("ImageRenderFilter Version $VERSION");
85 if ($ENV{MOBILE_HOME}) {
86 &loadConfigFile();
87 } else {
88 $CommonLib->printLog("MOBILE_HOME not exist. Please set the variable MOBILE_HOME into httpd.conf");
89 ModPerl::Util::exit();
90 }
91 sub Data {
92 my $_sec;
93 my $_min;
94 my $_hour;
95 my $_mday;
96 my $_day;
97 my $_mon;
98 my $_year;
99 my $_wday;
100 my $_yday;
101 my $_isdst;
102 my $_data;
103 ($_sec,$_min,$_hour,$_mday,$_mon,$_year,$_wday,$_yday,$_isdst) = localtime(time);
104 $_mon=$_mon+1;
105 $_year=substr($_year,1);
106 $_mon=&correct_number($_mon);
107 $_mday=&correct_number($_mday);
108 $_hour=&correct_number($_hour);
109 $_min=&correct_number($_min);
110 $_sec=&correct_number($_sec);
111 $_data="$_mday/$_mon/$_year - $_hour:$_min:$_sec";
112 return $_data;
113 }
114 sub correct_number {
115 my ($number) = @_;
116 if ($number < 10) {
117 $number="0$number";
118 }
119 return $number;
120 }
121 sub printLog {
122 my ($info) = @_;
123 my $data=Data();
124 print "$data - $info\n";
125 }
126 sub loadConfigFile {
127 my $null="";
128 my $null2="";
129 my $null3="";
130 my $val;
131 my $capability;
132 my $r_id;
133 my $dummy;
134 #The filter
135 $CommonLib->printLog("ResizeImageDirectory: Start read configuration from httpd.conf");
136 if ($ENV{ResizeImageDirectory}) {
137 $resizeimagedirectory=$ENV{ResizeImageDirectory};
138 $CommonLib->printLog("ResizeImageDirectory is: $resizeimagedirectory");
139 } else {
140 $CommonLib->printLog("ERROR: ResizeImageDirectory parameter must be setted");
141 ModPerl::Util::exit();
142 }
143 $CommonLib->printLog("Finish loading parameter");
144 }
145 sub handler {
146 my $f = shift;
147 my $capability2;
148 my $s = $f->r->server;
149 my $query_string=$f->r->args;
150 my $uri = $f->r->uri();
151 my $content_type=$f->r->content_type();
152 my @fileArray = split(/\//, $uri);
153 my $file=$fileArray[-1];
154 my $docroot = $f->r->document_root();
155 my $id="";
156 my $method="";
157 my $location;
158 my $width_toSearch;
159 my $type_redirect="internal";
160 my $return_value=Apache2::Const::DECLINED;
161 my $dummy="";
162 my $variabile2="";
163 my %ArrayCapFound;
164 my $controlCookie;
165 my $query_img="";
166 my %ArrayQuery;
167 my $var;
168 my $cookie = $f->r->headers_in->{Cookie} || '';
169 my $width=1000;
170 my $height=1000;
171 my $image2="";
172 $content_type=lc($content_type);
173 if ($f->r->pnotes('max_image_width')) {
174 $width=$f->r->pnotes('max_image_width')
175 }
176 if ($f->r->pnotes('max_image_height')) {
177 $height=$f->r->pnotes('max_image_height');
178 }
179 $repasshanlder=$repasshanlder + 1;
180 #
181 # Reading value of query string
182 #
183 if ($query_string) {
184 my @vars = split(/&/, $query_string);
185 foreach $var (sort @vars){
186 if ($var) {
187 my ($v,$i) = split(/=/, $var);
188 $v =~ tr/+/ /;
189 $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
190 $i =~ tr/+/ /;
191 $i =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
192 $i =~ s/<!--(.|\n)*-->//g;
193 $ArrayQuery{$v}=$i;
194 }
195 }
196 }
197 if ($ImageType{$content_type}) {
198 my $imageToConvert;
199 my $imagefile="";
200 if ($ArrayQuery{height}) {
201 if ( $ArrayQuery{height} =~ /^-?\d/) {
202 $height=$ArrayQuery{height};
203 }
204 }
205 if ($ArrayQuery{width}) {
206 if ( $ArrayQuery{width} =~ /^-?\d/) {
207 $width=$ArrayQuery{width};
208 }
209 }
210  
211 if ($ArrayQuery{dim}) {
212 if ( $ArrayQuery{dim} =~ /^-?\d/) {
213 $width=$ArrayQuery{dim} * $width / 100;
214 }
215 }
216 $imagefile="$resizeimagedirectory/$width.$file";
217 #
218 # control if image exist
219 #
220 $imageToConvert=$f->r->filename();
221 $return_value=Apache2::Const::DECLINED;
222 if ( -e "$imageToConvert") {
223 if ( -e "$docroot$imagefile") {
224 $dummy="";
225 } else {
226 my $image = Image::Resize->new("$imageToConvert");
227 my $gd = $image->resize($width, $height);
228
229 if (open(FH, ">$docroot$imagefile")) {
230 if ($content_type eq "image/gif") {
231 print FH $gd->gif();
232
233 }
234 if ($content_type eq "image/jpeg") {
235 print FH $gd->jpeg();
236 }
237 if ($content_type eq "image/png") {
238 $image2=$gd->png();
239 print FH $image2;
240 }
241 close(FH);
242 } else {
243 $s->warn("Can not create $docroot$imagefile");
244 }
245 }
246  
247 $f->r->headers_out->set(Pragma => 'no-cache');
248 $f->r->headers_out->set('Cache-control' => 'no-cache');
249 $f->r->headers_out->set(Expires => '-1');
250 $f->r->headers_out->set("Last-Modified" => time());
251 $f->r->headers_out->set("Cache-control"=>"max-age=0");
252
253 $f->r->internal_redirect_handler("$imagefile");
254 $return_value=Apache2::Const::DECLINED;
255 }
256 }
257 return $return_value;
258
259 }
260  
261 1;
262 =head1 NAME
263  
264 Apache2::ImageRenderFilter - Used to resize images on the fly to adapt to the screen size of the mobile device
265  
266  
267 =head1 COREQUISITES
268  
269 Apache2::RequestRec
270  
271 Apache2::RequestUtil
272  
273 Apache2::SubRequest
274  
275 Apache2::Log
276  
277 Apache2::Filter
278  
279 APR::Table
280  
281 LWP::Simple
282  
283 Image::Resize
284  
285 Apache2::Const
286  
287 File::Copy;
288  
289  
290 =head1 DESCRIPTION
291  
292 This module have the scope to manage with WURFLFilter.pm module the images for mobile devices.
293  
294 For more details: http://www.idelfuschini.it/apache-mobile-filter-v2x.html
295  
296 An example of how to set the httpd.conf is below:
297  
298 =over 4
299  
300 =item C<PerlSetEnv MOBILE_HOME server_root/MobileFilter>
301  
302 This indicate to the filter where put the transformated images (cache directory) this directory must be writeable
303  
304 =item C<PerlSetEnv ResizeImageDirectory /transform>
305  
306 =item C<PerlModule Apache2::WURFLFilter>
307 =item C<PerlTransHandler +Apache2::WURFLFilter>
308  
309 This is indicate to the filter were are stored the high definition images
310  
311 =item C<<Location /mobile/*>>
312  
313 =item C< SetHandler modperl>
314  
315 =item C< PerlInputFilterHandler Apache2::ImageRenderFilter >
316  
317 =item C<</Location>>
318  
319 =back
320  
321 NOTE: this software need wurfl.xml you can download it directly from this site: http://wurfl.sourceforge.net or you can set the filter to download it directly.
322  
323 =head1 SEE ALSO
324  
325 For more details: http://www.idelfuschini.it/apache-mobile-filter-v2x.html
326  
327 Mobile Demo page of the filter: http://apachemobilefilter.nogoogle.it (thanks Ivan alias sigmund)
328  
329 Demo page of the filter: http://apachemobilefilter.nogoogle.it/php_test.php (thanks Ivan alias sigmund)
330  
331 =head1 AUTHOR
332  
333 Idel Fuschini (idel.fuschini [at] gmail [dot] com)
334  
335 =cut