]> git.imager.perl.org - imager.git/commitdiff
new samples
authorTony Cook <tony@develop=help.com>
Tue, 12 Apr 2005 14:30:35 +0000 (14:30 +0000)
committerTony Cook <tony@develop=help.com>
Tue, 12 Apr 2005 14:30:35 +0000 (14:30 +0000)
samples/samp-scale.cgi [new file with mode: 0755]
samples/samp-scale.html [new file with mode: 0644]
samples/samp-tags.cgi [new file with mode: 0755]
samples/samp-tags.html [new file with mode: 0644]

diff --git a/samples/samp-scale.cgi b/samples/samp-scale.cgi
new file mode 100755 (executable)
index 0000000..411cb5e
--- /dev/null
@@ -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<Imager::Cookbook/Parsing an image posted via CGI> for
+cautions and explanations.
+
+=head1 AUTHOR
+
+Tony Cook <tony@imager.perl.org>
+
+=head1 REVISION
+
+$Revision$
+
+=cut
+  
diff --git a/samples/samp-scale.html b/samples/samp-scale.html
new file mode 100644 (file)
index 0000000..374e89b
--- /dev/null
@@ -0,0 +1,12 @@
+<html>
+  <head>
+    <title>Sample form for Imager handling of uploaded images</title>
+  </head>
+  <body>
+    <form action="/cgi-bin/samp-scale.cgi" method="post" 
+     enctype="multipart/form-data">
+     <p>File: <input type="file" name="image" /></p>
+     <p><input type="submit" value="Scale Image" /></p>
+    </form>
+  </body>
+</html>
\ No newline at end of file
diff --git a/samples/samp-tags.cgi b/samples/samp-tags.cgi
new file mode 100755 (executable)
index 0000000..abd775e
--- /dev/null
@@ -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<Imager::Cookbook/Parsing an image posted via CGI> for
+cautions and explanations.
+
+=head1 AUTHOR
+
+Tony Cook <tony@imager.perl.org>
+
+=head1 REVISION
+
+$Revision$
+
+=cut
+  
diff --git a/samples/samp-tags.html b/samples/samp-tags.html
new file mode 100644 (file)
index 0000000..cdf9f4d
--- /dev/null
@@ -0,0 +1,12 @@
+<html>
+  <head>
+    <title>Sample form for Imager handling of uploaded images</title>
+  </head>
+  <body>
+    <form action="/cgi-bin/samp-tags.cgi" method="post" 
+     enctype="multipart/form-data">
+     <p>File: <input type="file" name="image" /></p>
+     <p><input type="submit" value="Report Image Tags" /></p>
+    </form>
+  </body>
+</html>
\ No newline at end of file