File: samp-scale.cgi

package info (click to toggle)
libimager-perl 1.005%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,308 kB
  • ctags: 4,067
  • sloc: perl: 30,915; ansic: 27,680; makefile: 55; cpp: 4
file content (88 lines) | stat: -rwxr-xr-x 1,797 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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 <tonyc@cpan.org>

=head1 REVISION

$Revision$

=cut