]>
Commit | Line | Data |
---|---|---|
da4e0283 TC |
1 | #!/usr/bin/perl -w |
2 | ||
3 | =head1 NAME | |
4 | ||
5 | samp-tags.cgi - sample CGI that takes an uploaded image to produce a report | |
6 | ||
7 | =head1 SYNOPSIS | |
8 | ||
9 | Copy samp-tags.html to your document tree. | |
10 | Copy samp-tags.cgi to your /cgi-bin | |
11 | Browse to samp-tags.html in your browser | |
12 | Select an image file | |
13 | Click on "Report Image Tags" | |
14 | ||
15 | =cut | |
16 | ||
17 | ||
18 | use strict; | |
19 | use Imager; | |
20 | use CGI; | |
21 | ||
22 | my $cgi = CGI->new; | |
23 | ||
24 | my $filename = $cgi->param('image'); | |
25 | if ($filename) { | |
26 | my $fh = $cgi->upload('image'); | |
27 | if ($fh) { | |
28 | binmode $fh; | |
29 | ||
30 | my $image = Imager->new; | |
31 | if ($image->read(fh=>$fh)) { | |
32 | print "Content-Type: text/plain\n\n"; | |
33 | print "File: $filename\n"; | |
34 | my @tags = $image->tags; | |
35 | for my $tag (sort { $a->[0] cmp $b->[0] } @tags) { | |
36 | my $name = shift @$tag; | |
37 | print " $name: @$tag\n"; | |
38 | } | |
39 | } | |
40 | else { | |
41 | error("Cannot read image: ".$image->errstr); | |
42 | } | |
43 | } | |
44 | else { | |
45 | error("Incorrect form or input tag - check enctype and that the file upload field is type file"); | |
46 | } | |
47 | } | |
48 | else { | |
49 | error("No image was supplied"); | |
50 | } | |
51 | ||
52 | # simple error handler, ideally you'd display the form again with | |
53 | # an error in the right place, but this is a sample | |
54 | sub error { | |
55 | my ($msg) = @_; | |
56 | ||
57 | print "Content-Type: text/plain\n\nError processing form:\n$msg\n"; | |
58 | exit; | |
59 | } | |
60 | ||
61 | =head1 DESCRIPTION | |
62 | ||
63 | This is a sample CGI program that accepts an image file from the | |
64 | browser. | |
65 | ||
66 | Please read L<Imager::Cookbook/Parsing an image posted via CGI> for | |
67 | cautions and explanations. | |
68 | ||
69 | =head1 AUTHOR | |
70 | ||
5b480b14 | 71 | Tony Cook <tonyc@cpan.org> |
da4e0283 TC |
72 | |
73 | =head1 REVISION | |
74 | ||
75 | $Revision$ | |
76 | ||
77 | =cut | |
78 |