Apache2-ApacheMobileFilter

 view release on metacpan or  search on metacpan

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

						  my $image = Image::Resize->new("$imageToConvert");
						  $width=$image->width();					
						}
						if ($ArrayQuery{$par_width}) {
							 if ( $ArrayQuery{$par_width} =~ /^-?\d/) {
								  $width=$ArrayQuery{$par_width};
							 }
						}
	  
						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



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