Apache2-ApacheMobileFilter

 view release on metacpan or  search on metacpan

lib/Apache2/AMFImageRendering.pm  view on Meta::CPAN

#file:Apache2/AMFImageRendering.pm; 
#-------------------------------- 

#
# Created by Idel Fuschini 
# Date: 01/08/10
# Site: http://www.apachemobilefilter.org
# Mail: idel.fuschini@gmail.com


#https://www.imagemagick.org/script/perl-magick.php


package Apache2::AMFImageRendering; 
  
  use strict; 
  use warnings;
  use POSIX qw(ceil);
  use Apache2::AMFCommonLib ();
  use Apache2::RequestIO;
  
  use Apache2::RequestRec ();
  use Apache2::RequestUtil ();
  use Apache2::SubRequest ();
  use Apache2::Log;
  use Apache2::Filter (); 
  use APR::Table (); 
  use APR::Base64 (); 
  use LWP::Simple;
  use Image::Resize;
  use Image::Scale;
  use Image::Magick;
  use File::Copy;
  use Imager;
  use Apache2::Const -compile => qw(OK REDIRECT DECLINED HTTP_MOVED_TEMPORARILY);
  use constant BUFF_LEN => 1024;


  #
  # Define the global environment
  # 

  use vars qw($VERSION);
  $VERSION= "4.33";;;
  my $CommonLib = new Apache2::AMFCommonLib ();
  my %Capability;
  my %Array_fb;
  my %Array_id;
  my %Array_fullua_id;
  my %Array_DDRcapability;

  my %XHTMLUrl;
  my %WMLUrl;
  my %CHTMLUrl;
  my %ImageType;
  my %cacheArray;
  my %cacheArray_toview;
  my @arrayFilterMagick=("Point","Box","Triangle","Hermite","Hanning","Hamming","Blackman","Gaussian","Quadratic","Cubic","Catrom","Mitchell","Lanczos","Bessel","Sinc");


  

  my $intelliswitch="false";
  my $mobileversionurl;
  my $fullbrowserurl;
  my $querystring="false";
  my $showdefaultvariable="false";
  my $wurflnetdownload="false";
  my $downloadwurflurl="false";
  my $resizeimagedirectory="";
  my $downloadzipfile="true";
  my $virtualdirectoryimages="false";
  my $virtualdirectory="";
  my $repasshanlder=0;
  my $globalpassvariable="";
  my $log4wurfl="";
  my $loadwebpatch="false";
  my $dirwebpatch="";
  my $patchwurflnetdownload="false"; 
  my $patchwurflurl="";
  my $redirecttranscoder="true";
  my $redirecttranscoderurl="";
  my $detectaccuracy="false";
  my $listall="false";
  my $resizeimagesmall="false";
  my $par_height='height';
  my $par_width='width';
  my $par_perc='dim';
  my $typeGraphicLibrary='gd';
  my $filterMagick='Lanczos';
  my $qualityImage=90;
  my $maxAgeImage=99999999999999999;

lib/Apache2/AMFImageRendering.pm  view on Meta::CPAN

	  
						if ($ArrayQuery{$par_perc}) {
							 if ( $ArrayQuery{$par_perc} =~ /^-?\d/) {
								  $width=$ArrayQuery{$par_perc} * $width / 100;
							 }
						}
            $imagefile=$resizeimagedirectory."/".APR::Base64::encode($docroot."-".$uri.".".$width);
						#
						# control if image exist
						#
						
						if ( -e "$imageToConvert") {						  
							my $filesize;
							if ( -e "$imagefile" && time - (stat ($imagefile))[9] < $maxAgeImage) {
							} else {
								my $image = Image::Resize->new("$imageToConvert");
								if ($image->width() < $width && $resizeimagesmall eq 'false') {
								  copy($imageToConvert, $imagefile);
										} else {
								  if ($content_type eq "image/gif") {
										my @in = Imager->read_multi(file => $imageToConvert) or die "Cannot read image file: ", Imager->errstr, "\n";
										$in[0]->tags(name => 'i_format') eq 'gif' or die "File $imageToConvert is not a GIF image";
										my $src_screen_width = $in[0]->tags(name => 'gif_screen_width');
										my $src_screen_height = $in[0]->tags(name => 'gif_screen_height');
										my $factor=$width/$src_screen_width;
										my $out_screen_width = ceil($src_screen_width * $factor);
										my $out_screen_height = ceil($src_screen_height * $factor);
										my @out;
										for my $in (@in) {
										my $scaled = $in->scale(scalefactor => $factor, qtype=>'mixing');
										
										# roughly preserve the relative position
										$scaled->settag(name => 'gif_left', 
												value => $factor * $in->tags(name => 'gif_left'));
										$scaled->settag(name => 'gif_top', 
												value => $factor * $in->tags(name => 'gif_top'));
									  
										$scaled->settag(name => 'gif_screen_width', value => $out_screen_width);
										$scaled->settag(name => 'gif_screen_height', value => $out_screen_height);
									  
										# set some other tags from the source
										for my $tag (qw/gif_delay gif_user_input gif_loop gif_disposal/) {
									  $scaled->settag(name => $tag, value => $in->tags(name => $tag));
										}
										if ($in->tags(name => 'gif_local_map')) {
									  $scaled->settag(name => 'gif_local_map', value => 1);
										}
									  
										push @out, $scaled;
										}
										my $dummy=$imagefile.".gif";
										Imager->write_multi({ file => $dummy }, @out) or die "Cannot save $imagefile: ", Imager->errstr, "\n";
										rename($dummy, $imagefile);
								  } 
								  if ($content_type eq "image/png") {
                    if ($typeGraphicLibrary eq 'gd') {
                      my $img = Image::Scale->new("$imageToConvert") ;
                      $img->resize_gd( { width => $width } );
                      $img->save_png("$imagefile");
                    } else {
                        my $img = Image::Magick->new;
                        $img->Read($imageToConvert);
                        $img->Comment("Resized by AMF (http://www.apachemobilefilter.org)");
                        $img->Set(quality=>$qualityImage);
                        $img->Resize(geometry => $width);
                        $img->Write($imagefile);
                    }
								  }
								  if ($content_type eq "image/jpeg") {
                    if ($typeGraphicLibrary eq 'gd') {
                        my $img = Image::Scale->new("$imageToConvert");
                        $img->resize_gd( { width => $width } );
										    $img->save_jpeg("$imagefile",$qualityImage);
                    } else {
                        my $img = Image::Magick->new;
                        $img->Read($imageToConvert);
                        #unsharp  0x2+1+0
                        $img->Comment("Resized by AMF (http://www.apachemobilefilter.org)");
                        $img->UnsharpMask(radius=>0,sigma=>2,gain=>1,threshold=>0);
                        $img->Set(quality=>$qualityImage);
                        $img->Resize(geometry => $width);
                        $img->Write($imagefile);
                    }
								  }
							   }
	  
							}
							   unless( $f->ctx ) { 
								 $f->r->headers_out->unset('Content-Length'); 
								 $f->ctx(1); 
							  }
							$filesize = -s "$imagefile";
							$f->r->headers_out->set("Content-Length"=>$filesize);
							$f->r->content_type($content_type);
							open (FH,"$imagefile") or die ("couldn't open $imagefile\n");
								  read (FH,$image2,$filesize) ;
							close FH;	  
							$f->print($image2);
							$return_value=Apache2::Const::OK;
						}
			}
 	  }      
      return $return_value;
      
} 

1;


=head1 NAME

Apache2::AMFImageRendering - Used to resize images (jpg, png, gif gifanimated) on the fly to adapt to the screen size of the mobile device

=head1 DESCRIPTION

This module have the scope to manage with AMF51DegreesFilter, AMFDetectRightFilter and AMFWURFLFilter module the images for mobile devices. 

For more details: http://wiki.apachemobilefilter.org

=head1 AMF PROJECT SITE

http://www.apachemobilefilter.org

=head1 DOCUMENTATION

http://wiki.apachemobilefilter.org

Perl Module Documentation: http://wiki.apachemobilefilter.org/index.php/AMFImageRendering

=head1 AUTHOR

Idel Fuschini (idel.fuschini [at] gmail [dot] com)

=head1 COPYRIGHT



( run in 1.979 second using v1.01-cache-2.11-cpan-39bf76dae61 )