3 Imager::Cookbook - recipes working with Imager
7 Various simple and not so simple ways to do things with Imager.
11 This is described in detail in L<Imager::Files>.
13 =head2 Reading an image from a file
15 my $image = Imager->new;
17 $image->read(file=>$filename) or die $image->errstr;
21 =head2 Writing an image to a file
23 $image->write(file=>$filename) or die $image->errstr;
25 =head2 Write an animated gif.
27 # build an array of images to use in the gif
29 # synthesize the images or read them from files, it doesn't matter
33 Imager->write_multi({ file=>$filename, type=>'gif' }, @images)
34 or die Imager->errstr;
36 See L<Imager::Files/"Writing an animated GIF"> for a more detailed
39 =head2 Reading multiple images from one file
41 Some formats, like GIF and TIFF support multiple images per file. Use
42 the L<read_multi()|Imager::Files> method to read them:
44 my @images = Imager->read_multi(file=>$filename)
45 or die Imager->errstr;
47 =head1 IMAGE SYNTHESIS
49 =head2 Creating an image
51 To create a simple RGB image, supply the image width and height to the
54 my $rgb = Imager->new(xsize=>$width, ysize=>$height);
56 If you also want an alpha channel:
58 my $rgb_alpha = Imager->new(xsize=>$width, ysize=>$height, channels=>4);
60 To make a grayscale image:
62 my $gray = Imager->new(xsize=>$width, ysize=>$height, channels=>1);
64 and a grayscale image with an alpha channel:
66 my $gray_alpha = Imager->new(xsize=>$width, ysize=>$height, channels=>2);
68 When a new image is created this way all samples are set to zero -
69 black for 1 or 3 channel images, transparent black for 2 or 4 channel
72 You can also create paletted images and images with more than 8-bits
73 per channel, see L<Imager::ImageTypes> for more details.
75 =head2 Setting the background of a new image
77 To set the background of a new image to a solid color, use the box()
78 method with no limits, and C<< filled=>1 >>:
80 $image->box(filled=>1, color=>$color);
82 As always, a color can be specified as an L<Imager::Color> object:
84 my $white = Imager::Color->new(255, 255, 255);
85 $image->box(filled=>1, color=>$white);
87 or you supply any single scalar that Imager::Color's new() method
88 accepts as a color description:
90 $image->box(filled=>1, color=>'white');
91 $image->box(filled=>1, color=>'#FF0000');
92 $image->box(filled=>1, color=>[ 255, 255, 255 ]);
94 You can also fill the image with a fill object:
97 # create the fill object
98 my $fill = Imager::Fill->new(hatch=>'check1x1')
99 $image->box(fill=>$fill);
101 # let Imager create one automatically
102 $image->box(fill=>{ hatch=>'check1x1' });
104 See L<Imager::Fill> for information on Imager's fill objects.
106 =head1 WORLD WIDE WEB
108 As with any CGI script it's up to you to validate data and set limits
109 on any parameters supplied to Imager.
111 For example, if you allow the caller to set the size of an output
112 image you should limit the size to prevent the client from specifying
113 an image size that will consume all available memory.
115 This is beside any any other controls you need over access to data.
117 See L<CGI> for a module useful for processing CGI submitted data.
119 =head2 Returning an image from a CGI script
121 This is similar to writing to a file, but you also need to supply the
122 information needed by the web browser to identify the file format:
124 my $img = ....; # create the image and generate the contents
125 print "Content-Type: image/png\n\n";
127 $img->write(fd=>fileno(STDOUT), type=>'png')
130 You need to set the Content-Type header depending on the file format
131 you send to the web browser.
133 If you want to supply a content-length header, write the image to a
136 my $img = ....; # create the image and generate the contents
138 $img->write(type=>'png', data=>\$data)
140 print "Content-Type: image/png\n";
141 print "Content-Length: ",length($data),"\n\n";
145 =head2 Inserting a CGI image in a page
147 There's occasionally confusion on how to display an image generated by
148 Imager in a page generated by a CGI.
150 Your web browser handles this process as two requests, one for the
151 HTML page, and another for the image itself.
153 Each request needs to perform validation since an attacker can control
154 the values supplied to both requests.
156 How you make the data available to the image generation code depends
159 =head2 Parsing an image posted via CGI
161 C<WARNING>: file format attacks have become a common attack vector,
162 make sure you have up to date image file format libraries, otherwise
163 trying to parse uploaded files, whether with Imager or some other
164 tool, may result in a remote attacker being able to run their own code
165 on your system. Currently Imager makes no attempt to place size
166 limits on a read image file. This may result in consumption of large
167 amounts of memory. Future versions of Imager may provide mechanisms
168 to limit the sizes of images read from files.
170 If your HTML form uses the correct magic, it can upload files to your
171 CGI script, in particular, you need to use C< method="post" > and
172 C<enctype="multipart/form-data"> in the C<form> tag, and use
173 C<type="file"> in the C<input>, for example:
175 <form action="/cgi-bin/yourprogram" method="post"
176 enctype="multipart/form-data">
177 <input type="file" name="myimage" />
178 <input type="submit value="Upload Image" />
187 first check that the user supplied a file
195 have Imager read the image
199 # returns the client's name for the file, don't open this locally
201 # 1. check the user supplied a file
202 my $filename = $cgi->param('myimage');
204 # 2. get the file handle
205 my $fh = $cgi->upload('myimage');
209 # 3. have Imager read the image
210 my $img = Imager->new;
211 if ($img->read(fh=>$fh)) {
212 # we can now process the image
215 # else, you probably have an incorrect form or input tag
217 # else, the user didn't select a file
227 =head2 Measuring text
229 =head2 Word wrapping text
233 =head2 Image spatial resolution.
239 Tony Cook <tony@develop-help.com>
243 L<Imager>, L<Imager::Files>, L<Imager::Draw>.