From: Tony Cook Date: Tue, 12 Apr 2005 14:30:35 +0000 (+0000) Subject: new samples X-Git-Tag: Imager-0.48^2~189 X-Git-Url: http://git.imager.perl.org/imager.git/commitdiff_plain/da4e02834c5896d938832b3735a39eeaa289bea9?ds=inline new samples --- diff --git a/samples/samp-scale.cgi b/samples/samp-scale.cgi new file mode 100755 index 00000000..411cb5e9 --- /dev/null +++ b/samples/samp-scale.cgi @@ -0,0 +1,88 @@ +#!/usr/bin/perl -w + +=head1 NAME + +samp-scale.cgi - sample CGI that takes an uploaded image to make a new image using Imager + +=head1 SYNOPSIS + + Copy samp-scale.html to your document tree. + Copy samp-scale.cgi to your /cgi-bin + Browse to samp-scale.html in your browser + Select an image file + Click on "Scale Image" + +=cut + + +use strict; +use Imager; +use CGI; + +my $cgi = CGI->new; + +my $filename = $cgi->param('image'); +if ($filename) { + my $fh = $cgi->upload('image'); + if ($fh) { + binmode $fh; + + my $image = Imager->new; + if ($image->read(fh=>$fh)) { + # scale it to max 200 x 200 + my $scaled = $image->scale(xpixels=>200, ypixels=>200, type=>'min'); + if ($scaled) { + # no line end conversion (or UTF or whatever) + binmode STDOUT; + + # send in the order we provide it + ++$|; + + # give it back to the user - as a JPEG + print "Content-Type: image/jpeg\n\n"; + $scaled->write(fd=>fileno(STDOUT), type=>'jpeg'); + } + else { + # this should only fail in strange circumstances + error("Cannot scale image: ", $image->errstr); + } + } + else { + error("Cannot read image: ".$image->errstr); + } + } + else { + error("Incorrect form or input tag - check enctype and that the file upload field is type file"); + } +} +else { + error("No image was supplied"); +} + +# simple error handler, ideally you'd display the form again with +# an error in the right place, but this is a sample +sub error { + my ($msg) = @_; + + print "Content-Type: text/plain\n\nError processing form:\n$msg\n"; + exit; +} + +=head1 DESCRIPTION + +This is a sample CGI program that accepts an image file from the +browser. + +Please read L for +cautions and explanations. + +=head1 AUTHOR + +Tony Cook + +=head1 REVISION + +$Revision$ + +=cut + diff --git a/samples/samp-scale.html b/samples/samp-scale.html new file mode 100644 index 00000000..374e89bc --- /dev/null +++ b/samples/samp-scale.html @@ -0,0 +1,12 @@ + + + Sample form for Imager handling of uploaded images + + +
+

File:

+

+
+ + \ No newline at end of file diff --git a/samples/samp-tags.cgi b/samples/samp-tags.cgi new file mode 100755 index 00000000..abd775e4 --- /dev/null +++ b/samples/samp-tags.cgi @@ -0,0 +1,78 @@ +#!/usr/bin/perl -w + +=head1 NAME + +samp-tags.cgi - sample CGI that takes an uploaded image to produce a report + +=head1 SYNOPSIS + + Copy samp-tags.html to your document tree. + Copy samp-tags.cgi to your /cgi-bin + Browse to samp-tags.html in your browser + Select an image file + Click on "Report Image Tags" + +=cut + + +use strict; +use Imager; +use CGI; + +my $cgi = CGI->new; + +my $filename = $cgi->param('image'); +if ($filename) { + my $fh = $cgi->upload('image'); + if ($fh) { + binmode $fh; + + my $image = Imager->new; + if ($image->read(fh=>$fh)) { + print "Content-Type: text/plain\n\n"; + print "File: $filename\n"; + my @tags = $image->tags; + for my $tag (sort { $a->[0] cmp $b->[0] } @tags) { + my $name = shift @$tag; + print " $name: @$tag\n"; + } + } + else { + error("Cannot read image: ".$image->errstr); + } + } + else { + error("Incorrect form or input tag - check enctype and that the file upload field is type file"); + } +} +else { + error("No image was supplied"); +} + +# simple error handler, ideally you'd display the form again with +# an error in the right place, but this is a sample +sub error { + my ($msg) = @_; + + print "Content-Type: text/plain\n\nError processing form:\n$msg\n"; + exit; +} + +=head1 DESCRIPTION + +This is a sample CGI program that accepts an image file from the +browser. + +Please read L for +cautions and explanations. + +=head1 AUTHOR + +Tony Cook + +=head1 REVISION + +$Revision$ + +=cut + diff --git a/samples/samp-tags.html b/samples/samp-tags.html new file mode 100644 index 00000000..cdf9f4d9 --- /dev/null +++ b/samples/samp-tags.html @@ -0,0 +1,12 @@ + + + Sample form for Imager handling of uploaded images + + +
+

File:

+

+
+ + \ No newline at end of file