| 1 | #file:Apache2/AMFImageRendering.pm; | ||
| 2 | #-------------------------------- | ||
| 3 | |||
| 4 | # | ||
| 5 | # Created by Idel Fuschini | ||
| 6 | # Date: 01/08/10 | ||
| 7 | # Site: http://www.apachemobilefilter.org | ||
| 8 | # Mail: idel.fuschini@gmail.com | ||
| 9 | |||
| 10 | |||
| 11 | #https://www.imagemagick.org/script/perl-magick.php | ||
| 12 | |||
| 13 | |||
| 14 | package Apache2::AMFImageRendering; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | use POSIX qw(ceil); | ||
| 19 | use Apache2::AMFCommonLib (); | ||
| 20 | use Apache2::RequestIO; | ||
| 21 | |||
| 22 | use Apache2::RequestRec (); | ||
| 23 | use Apache2::RequestUtil (); | ||
| 24 | use Apache2::SubRequest (); | ||
| 25 | use Apache2::Log; | ||
| 26 | use Apache2::Filter (); | ||
| 27 | use APR::Table (); | ||
| 28 | use APR::Base64 (); | ||
| 29 | use LWP::Simple; | ||
| 30 | use Image::Resize; | ||
| 31 | use Image::Scale; | ||
| 32 | use Image::Magick; | ||
| 33 | use File::Copy; | ||
| 34 | use Imager; | ||
| 35 | use Apache2::Const -compile => qw(OK REDIRECT DECLINED HTTP_MOVED_TEMPORARILY); | ||
| 36 | use constant BUFF_LEN => 1024; | ||
| 37 | |||
| 38 | |||
| 39 | # | ||
| 40 | # Define the global environment | ||
| 41 | # | ||
| 42 | |||
| 43 | use vars qw($VERSION); | ||
| 44 | $VERSION= "4.33";;; | ||
| 45 | my $CommonLib = new Apache2::AMFCommonLib (); | ||
| 46 | my %Capability; | ||
| 47 | my %Array_fb; | ||
| 48 | my %Array_id; | ||
| 49 | my %Array_fullua_id; | ||
| 50 | my %Array_DDRcapability; | ||
| 51 | |||
| 52 | my %XHTMLUrl; | ||
| 53 | my %WMLUrl; | ||
| 54 | my %CHTMLUrl; | ||
| 55 | my %ImageType; | ||
| 56 | my %cacheArray; | ||
| 57 | my %cacheArray_toview; | ||
| 58 | my @arrayFilterMagick=("Point","Box","Triangle","Hermite","Hanning","Hamming","Blackman","Gaussian","Quadratic","Cubic","Catrom","Mitchell","Lanczos","Bessel","Sinc"); | ||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | my $intelliswitch="false"; | ||
| 64 | my $mobileversionurl; | ||
| 65 | my $fullbrowserurl; | ||
| 66 | my $querystring="false"; | ||
| 67 | my $showdefaultvariable="false"; | ||
| 68 | my $wurflnetdownload="false"; | ||
| 69 | my $downloadwurflurl="false"; | ||
| 70 | my $resizeimagedirectory=""; | ||
| 71 | my $downloadzipfile="true"; | ||
| 72 | my $virtualdirectoryimages="false"; | ||
| 73 | my $virtualdirectory=""; | ||
| 74 | my $repasshanlder=0; | ||
| 75 | my $globalpassvariable=""; | ||
| 76 | my $log4wurfl=""; | ||
| 77 | my $loadwebpatch="false"; | ||
| 78 | my $dirwebpatch=""; | ||
| 79 | my $patchwurflnetdownload="false"; | ||
| 80 | my $patchwurflurl=""; | ||
| 81 | my $redirecttranscoder="true"; | ||
| 82 | my $redirecttranscoderurl=""; | ||
| 83 | my $detectaccuracy="false"; | ||
| 84 | my $listall="false"; | ||
| 85 | my $resizeimagesmall="false"; | ||
| 86 | my $par_height='height'; | ||
| 87 | my $par_width='width'; | ||
| 88 | my $par_perc='dim'; | ||
| 89 | my $typeGraphicLibrary='gd'; | ||
| 90 | my $filterMagick='Lanczos'; | ||
| 91 | my $qualityImage=90; | ||
| 92 | my $maxAgeImage=99999999999999999; | ||
| 93 | |||
| 94 | $ImageType{'image/png'}="png"; | ||
| 95 | $ImageType{'image/gif'}="gif"; | ||
| 96 | $ImageType{'image/jpg'}="jpg"; | ||
| 97 | $ImageType{'image/jpeg'}="jpeg"; | ||
| 98 | |||
| 99 | # | ||
| 100 | # Check if MOBILE_HOME is setting in apache httpd.conf file for example: | ||
| 101 | # PerlSetEnv MOBILE_HOME <apache_directory>/MobileFilter | ||
| 102 | # | ||
| 103 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 104 | $CommonLib->printLog("AMFImageRendering Version $VERSION"); | ||
| 105 | if ($ENV{AMFMobileHome}) { | ||
| 106 | &loadConfigFile(); | ||
| 107 | } else { | ||
| 108 | $CommonLib->printLog("AMFMobileHome not exist. Please set the variable AMFMobileHome into httpd.conf"); | ||
| 109 | ModPerl::Util::exit(); | ||
| 110 | } | ||
| 111 | sub loadConfigFile { | ||
| 112 | my $null=""; | ||
| 113 | my $null2=""; | ||
| 114 | my $null3=""; | ||
| 115 | my $val; | ||
| 116 | my $capability; | ||
| 117 | my $r_id; | ||
| 118 | my $dummy; | ||
| 119 | #The filter | ||
| 120 | $CommonLib->printLog("ResizeImageDirectory: Start read configuration from httpd.conf"); | ||
| 121 | if ($ENV{ResizeImageDirectory}) { | ||
| 122 | $resizeimagedirectory=$ENV{ResizeImageDirectory}; | ||
| 123 | $CommonLib->printLog("ResizeImageDirectory is: $resizeimagedirectory"); | ||
| 124 | } else { | ||
| 125 | $CommonLib->printLog("ERROR: ResizeImageDirectory parameter must be setted"); | ||
| 126 | ModPerl::Util::exit(); | ||
| 127 | } | ||
| 128 | if ($ENV{ResizeSmallImage}) { | ||
| 129 | $resizeimagesmall=$ENV{ResizeSmallImage}; | ||
| 130 | $CommonLib->printLog("ResizeSmallImage is: $resizeimagesmall. The image smallest of device screensize is also resized (low quality)"); | ||
| 131 | } | ||
| 132 | if ($ENV{TypeGraphicLibrary}) { | ||
| 133 | if ($ENV{TypeGraphicLibrary} eq 'gd' || $ENV{TypeGraphicLibrary} eq 'magick') { | ||
| 134 | $typeGraphicLibrary=$ENV{TypeGraphicLibrary}; | ||
| 135 | $CommonLib->printLog("TypeGraphicLibrary is: $typeGraphicLibrary"); | ||
| 136 | } else { | ||
| 137 | $CommonLib->printLog("ERROR: TypeGraphicLibrary parameter must be 'gd' or 'magick'"); | ||
| 138 | ModPerl::Util::exit(); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | if ($ENV{QualityImage}) { | ||
| 142 | if (($ENV{QualityImage} =~ /^\d+?$/) && $ENV{QualityImage} > -1 && $ENV{QualityImage} < 101) { | ||
| 143 | $qualityImage=$ENV{QualityImage}; | ||
| 144 | } else { | ||
| 145 | $CommonLib->printLog("ERROR: QualityImage must be a number from 0 (lower) to 100 (higher), default is: ".$qualityImage); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | if ($ENV{MaxAgeImage}) { | ||
| 149 | if (($ENV{MaxAgeImage} =~ /^\d+?$/) && $ENV{MaxAgeImage} > 0 ) { | ||
| 150 | $maxAgeImage=$ENV{MaxAgeImage}; | ||
| 151 | $CommonLib->printLog("MaxAgeImage is: ".$maxAgeImage." (seconds)"); | ||
| 152 | } else { | ||
| 153 | $CommonLib->printLog("ERROR: MaxAgeImage must be a number > 0"); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | $CommonLib->printLog("QualityImage is: ".$qualityImage); | ||
| 157 | if ($ENV{TypeMagickFilter}) { | ||
| 158 | my %params = map { $_ => 1 } @arrayFilterMagick; | ||
| 159 | if(exists($params{$ENV{TypeMagickFilter}})) { | ||
| 160 | $filterMagick=$ENV{TypeMagickFilter}; | ||
| 161 | $CommonLib->printLog("TypeMagickFilter is: $filterMagick"); | ||
| 162 | } else { | ||
| 163 | $CommonLib->printLog("ERROR: TypeMagickFilter filter ".$ENV{TypeMagickFilter}." not available, the default is ".$filterMagick); | ||
| 164 | my $stringFilters=''; | ||
| 165 | foreach my $filter (@arrayFilterMagick){ | ||
| 166 | $stringFilters=$stringFilters."'".$filter."' "; | ||
| 167 | } | ||
| 168 | $CommonLib->printLog("The available filters are: ".$stringFilters."for more info http://www.imagemagick.org/Usage/filter/"); | ||
| 169 | } | ||
| 170 | } else { | ||
| 171 | if ($ENV{TypeGraphicLibrary} eq 'magick') { | ||
| 172 | $CommonLib->printLog("TypeMagickFilter is not set, the default is ".$filterMagick); | ||
| 173 | } | ||
| 174 | |||
| 175 | } | ||
| 176 | if ($ENV{ImageParamWidth}) { | ||
| 177 | $par_width=$ENV{ImageParamWidth}; | ||
| 178 | $CommonLib->printLog("ImageParamWidth is: $par_width. To force the width of image the url must be <url image>?$par_width=<width>"); | ||
| 179 | } | ||
| 180 | if ($ENV{ImageParamHeight}) { | ||
| 181 | $par_height=$ENV{ImageParamHeight}; | ||
| 182 | $CommonLib->printLog("ImageParamHeight is: $par_height. To force the height of image the url must be <url image>?$par_width=<height>"); | ||
| 183 | } | ||
| 184 | if ($ENV{ImageParamPerc}) { | ||
| 185 | $par_perc=$ENV{ImageParamPerc}; | ||
| 186 | $CommonLib->printLog("ImageParamPerc is: $par_perc. To force the percentage of image the url must be <url image>?$par_perc=<percentage>"); | ||
| 187 | } | ||
| 188 | |||
| 189 | $CommonLib->printLog("Finish loading parameter"); | ||
| 190 | } | ||
| 191 | sub handler { | ||
| 192 | my $f = shift; | ||
| 193 | my $capability2; | ||
| 194 | my $s = $f->r->server; | ||
| 195 | my $query_string=$f->r->args; | ||
| 196 | my $uri = $f->r->uri(); | ||
| 197 | #$uri =~ s/\//_/g; | ||
| 198 | my $content_type=$f->r->content_type(); | ||
| 199 | my @fileArray = split(/\//, $uri); | ||
| 200 | my $file=$fileArray[-1]; | ||
| 201 | my $docroot = $f->r->document_root(); | ||
| 202 | #$docroot =~ s/\//_/g; | ||
| 203 | my $servername=$f->r->get_server_name(); | ||
| 204 | my $id=""; | ||
| 205 | my $method=""; | ||
| 206 | my $location; | ||
| 207 | my $width_toSearch; | ||
| 208 | my $type_redirect="internal"; | ||
| 209 | my $return_value=Apache2::Const::DECLINED; | ||
| 210 | my $dummy=""; | ||
| 211 | my $variabile2=""; | ||
| 212 | my %ArrayCapFound; | ||
| 213 | my $controlCookie; | ||
| 214 | my $query_img=""; | ||
| 215 | my %ArrayQuery; | ||
| 216 | my $var; | ||
| 217 | my $cookie = $f->r->headers_in->{Cookie} || ''; | ||
| 218 | my $width=1000; | ||
| 219 | my $height=1000; | ||
| 220 | my $image2=""; | ||
| 221 | my $device_claims_web_support='null'; | ||
| 222 | my $is_wireless_device='null'; | ||
| 223 | my $isMobile='true'; | ||
| 224 | |||
| 225 | $content_type=lc($content_type); | ||
| 226 | if ($f->r->pnotes('max_image_width')) { | ||
| 227 | $width=$f->r->pnotes('max_image_width') | ||
| 228 | } | ||
| 229 | if ($f->r->pnotes('max_image_height')) { | ||
| 230 | $height=$f->r->pnotes('max_image_height'); | ||
| 231 | } | ||
| 232 | if ($f->r->pnotes('amf_device_ismobile')) { | ||
| 233 | $isMobile=$f->r->pnotes('amf_device_ismobile'); | ||
| 234 | } | ||
| 235 | |||
| 236 | $repasshanlder=$repasshanlder + 1; | ||
| 237 | $return_value=Apache2::Const::DECLINED; | ||
| 238 | # | ||
| 239 | # Reading value of query string | ||
| 240 | # | ||
| 241 | if ($query_string) { | ||
| 242 | my @vars = split(/&/, $query_string); | ||
| 243 | foreach $var (sort @vars){ | ||
| 244 | if ($var) { | ||
| 245 | my ($v,$i) = split(/=/, $var); | ||
| 246 | $v =~ tr/+/ /; | ||
| 247 | $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; | ||
| 248 | $i =~ tr/+/ /; | ||
| 249 | $i =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; | ||
| 250 | $i =~ s/<!--(.|\n)*-->//g; | ||
| 251 | $ArrayQuery{$v}=$i; | ||
| 252 | } | ||
| 253 | } | ||
| 254 | if ($ImageType{$content_type}) { | ||
| 255 | my $imageToConvert; | ||
| 256 | my $imagefile=""; | ||
| 257 | if ($ArrayQuery{par_height}) { | ||
| 258 | if ( $ArrayQuery{$par_height} =~ /^-?\d/) { | ||
| 259 | $height=$ArrayQuery{par_height}; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | $imageToConvert=$f->r->filename(); | ||
| 263 | if ($isMobile eq 'false') { | ||
| 264 | my $image = Image::Resize->new("$imageToConvert"); | ||
| 265 | $width=$image->width(); | ||
| 266 | } | ||
| 267 | if ($ArrayQuery{$par_width}) { | ||
| 268 | if ( $ArrayQuery{$par_width} =~ /^-?\d/) { | ||
| 269 | $width=$ArrayQuery{$par_width}; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | |||
| 273 | if ($ArrayQuery{$par_perc}) { | ||
| 274 | if ( $ArrayQuery{$par_perc} =~ /^-?\d/) { | ||
| 275 | $width=$ArrayQuery{$par_perc} * $width / 100; | ||
| 276 | } | ||
| 277 | } | ||
| 278 | $imagefile=$resizeimagedirectory."/".APR::Base64::encode($docroot."-".$uri.".".$width); | ||
| 279 | # | ||
| 280 | # control if image exist | ||
| 281 | # | ||
| 282 | |||
| 283 | if ( -e "$imageToConvert") { | ||
| 284 | my $filesize; | ||
| 285 | if ( -e "$imagefile" && time - (stat ($imagefile))[9] < $maxAgeImage) { | ||
| 286 | } else { | ||
| 287 | my $image = Image::Resize->new("$imageToConvert"); | ||
| 288 | if ($image->width() < $width && $resizeimagesmall eq 'false') { | ||
| 289 | copy($imageToConvert, $imagefile); | ||
| 290 | } else { | ||
| 291 | if ($content_type eq "image/gif") { | ||
| 292 | my @in = Imager->read_multi(file => $imageToConvert) or die "Cannot read image file: ", Imager->errstr, "\n"; | ||
| 293 | $in[0]->tags(name => 'i_format') eq 'gif' or die "File $imageToConvert is not a GIF image"; | ||
| 294 | my $src_screen_width = $in[0]->tags(name => 'gif_screen_width'); | ||
| 295 | my $src_screen_height = $in[0]->tags(name => 'gif_screen_height'); | ||
| 296 | my $factor=$width/$src_screen_width; | ||
| 297 | my $out_screen_width = ceil($src_screen_width * $factor); | ||
| 298 | my $out_screen_height = ceil($src_screen_height * $factor); | ||
| 299 | my @out; | ||
| 300 | for my $in (@in) { | ||
| 301 | my $scaled = $in->scale(scalefactor => $factor, qtype=>'mixing'); | ||
| 302 | |||
| 303 | # roughly preserve the relative position | ||
| 304 | $scaled->settag(name => 'gif_left', | ||
| 305 | value => $factor * $in->tags(name => 'gif_left')); | ||
| 306 | $scaled->settag(name => 'gif_top', | ||
| 307 | value => $factor * $in->tags(name => 'gif_top')); | ||
| 308 | |||
| 309 | $scaled->settag(name => 'gif_screen_width', value => $out_screen_width); | ||
| 310 | $scaled->settag(name => 'gif_screen_height', value => $out_screen_height); | ||
| 311 | |||
| 312 | # set some other tags from the source | ||
| 313 | for my $tag (qw/gif_delay gif_user_input gif_loop gif_disposal/) { | ||
| 314 | $scaled->settag(name => $tag, value => $in->tags(name => $tag)); | ||
| 315 | } | ||
| 316 | if ($in->tags(name => 'gif_local_map')) { | ||
| 317 | $scaled->settag(name => 'gif_local_map', value => 1); | ||
| 318 | } | ||
| 319 | |||
| 320 | push @out, $scaled; | ||
| 321 | } | ||
| 322 | my $dummy=$imagefile.".gif"; | ||
| 323 | Imager->write_multi({ file => $dummy }, @out) or die "Cannot save $imagefile: ", Imager->errstr, "\n"; | ||
| 324 | rename($dummy, $imagefile); | ||
| 325 | } | ||
| 326 | if ($content_type eq "image/png") { | ||
| 327 | if ($typeGraphicLibrary eq 'gd') { | ||
| 328 | my $img = Image::Scale->new("$imageToConvert") ; | ||
| 329 | $img->resize_gd( { width => $width } ); | ||
| 330 | $img->save_png("$imagefile"); | ||
| 331 | } else { | ||
| 332 | my $img = Image::Magick->new; | ||
| 333 | $img->Read($imageToConvert); | ||
| 334 | $img->Comment("Resized by AMF (http://www.apachemobilefilter.org)"); | ||
| 335 | $img->Set(quality=>$qualityImage); | ||
| 336 | $img->Resize(geometry => $width); | ||
| 337 | $img->Write($imagefile); | ||
| 338 | } | ||
| 339 | } | ||
| 340 | if ($content_type eq "image/jpeg") { | ||
| 341 | if ($typeGraphicLibrary eq 'gd') { | ||
| 342 | my $img = Image::Scale->new("$imageToConvert"); | ||
| 343 | $img->resize_gd( { width => $width } ); | ||
| 344 | $img->save_jpeg("$imagefile",$qualityImage); | ||
| 345 | } else { | ||
| 346 | my $img = Image::Magick->new; | ||
| 347 | $img->Read($imageToConvert); | ||
| 348 | #unsharp 0x2+1+0 | ||
| 349 | $img->Comment("Resized by AMF (http://www.apachemobilefilter.org)"); | ||
| 350 | $img->UnsharpMask(radius=>0,sigma=>2,gain=>1,threshold=>0); | ||
| 351 | $img->Set(quality=>$qualityImage); | ||
| 352 | $img->Resize(geometry => $width); | ||
| 353 | $img->Write($imagefile); | ||
| 354 | } | ||
| 355 | } | ||
| 356 | } | ||
| 357 | |||
| 358 | } | ||
| 359 | unless( $f->ctx ) { | ||
| 360 | $f->r->headers_out->unset('Content-Length'); | ||
| 361 | $f->ctx(1); | ||
| 362 | } | ||
| 363 | $filesize = -s "$imagefile"; | ||
| 364 | $f->r->headers_out->set("Content-Length"=>$filesize); | ||
| 365 | $f->r->content_type($content_type); | ||
| 366 | open (FH,"$imagefile") or die ("couldn't open $imagefile\n"); | ||
| 367 | read (FH,$image2,$filesize) ; | ||
| 368 | close FH; | ||
| 369 | $f->print($image2); | ||
| 370 | $return_value=Apache2::Const::OK; | ||
| 371 | } | ||
| 372 | } | ||
| 373 | } | ||
| 374 | return $return_value; | ||
| 375 | |||
| 376 | } | ||
| 377 | |||
| 378 | 1; | ||
| 379 | |||
| 380 | |||
| 381 | =head1 NAME | ||
| 382 | |||
| 383 | Apache2::AMFImageRendering - Used to resize images (jpg, png, gif gifanimated) on the fly to adapt to the screen size of the mobile device | ||
| 384 | |||
| 385 | =head1 DESCRIPTION | ||
| 386 | |||
| 387 | This module have the scope to manage with AMF51DegreesFilter, AMFDetectRightFilter and AMFWURFLFilter module the images for mobile devices. | ||
| 388 | |||
| 389 | For more details: http://wiki.apachemobilefilter.org | ||
| 390 | |||
| 391 | =head1 AMF PROJECT SITE | ||
| 392 | |||
| 393 | http://www.apachemobilefilter.org | ||
| 394 | |||
| 395 | =head1 DOCUMENTATION | ||
| 396 | |||
| 397 | http://wiki.apachemobilefilter.org | ||
| 398 | |||
| 399 | Perl Module Documentation: http://wiki.apachemobilefilter.org/index.php/AMFImageRendering | ||
| 400 | |||
| 401 | =head1 AUTHOR | ||
| 402 | |||
| 403 | Idel Fuschini (idel.fuschini [at] gmail [dot] com) | ||
| 404 | |||
| 405 | =head1 COPYRIGHT | ||
| 406 | |||
| 407 | You may distribute under the terms of either the GNU General Public | ||
| 408 | License or the Artistic License, as specified in the Perl README file. | ||
| 409 | |||
| 410 | =cut |