#!/usr/bin/perl use strict; use warnings; use Image::Magick; use Image::Info qw(image_info); use IO::File; use CGI qw(:standard); use File::Basename; use Data::Dumper; unless( param('submit') ) { print < Image Magick Resize / Copy Test
HTML exit; } my $filename = param('image_file'); my $cgi = new CGI; my $fh = $cgi->upload('image_file'); my $outfile = new IO::File; my $buffer; # open the pipe to the output file open ($outfile,">/tmp/someimage") or die( "Could not write file: $!" ); while (my $bytesread=read($fh, $buffer, 1024)) { print $outfile $buffer; } close $outfile; my $info = image_info("/tmp/someimage"); my $extension = $info->{file_ext}; # now create a file with the proper extension, and zap the original rename( "/tmp/someimage", "/tmp/image_original".".$extension"); { my $oImageMagick = Image::Magick->new; $oImageMagick->Read("/tmp/image_original".".$extension"); my $img_width = $oImageMagick->Get('columns'); my $img_height = $oImageMagick->Get('rows'); my $longest = $img_width>=$img_height? $img_width:$img_height; my $ratio_main = 600 / $longest; $oImageMagick->Resize(width=>$img_width * $ratio_main, height=>$img_height * $ratio_main); $oImageMagick->Write('/tmp/image_main'.".$extension"); } { my $oImageMagick = Image::Magick->new; $oImageMagick->Read("/tmp/image_original".".$extension"); my $img_width = $oImageMagick->Get('columns'); my $img_height = $oImageMagick->Get('rows'); my $longest = $img_width>=$img_height? $img_width:$img_height; my $ratio_thumb = 200 / $longest; $oImageMagick->Resize(width=>$img_width * $ratio_thumb, height=>$img_height * $ratio_thumb); $oImageMagick->Write('/tmp/image_thumb'.".$extension"); } print "Content-type: text/html\n\n"; print "File uploaded\n";