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 =head2 Converting from one file format to another
49 This is as simple as reading the original file and writing the new
50 file, for single images:
52 my $image = Imager->new;
53 # Imager auto-detects the input file type
54 $image->read(file => $input_filename)
55 or die $image->errstr;
56 # Imager derives the output file format from the filename
57 $image->write(file => $output_filename)
58 or die $image->errstr;
60 # or you can supply a type parameter:
61 $image->write(file => $output_filename, type => 'gif')
62 or die $image->errstr;
64 The main issue that can occur with this is if the input file has
65 transparency and the output file format doesn't support that. This
66 can be a problem when converting from GIFs to JPEGs for example.
68 To work around that you can compose the source image onto a background
71 if ($image->getchannels == 4 or $image->getchannels == 2) {
72 my $back = Imager->new(xsize => $image->getwidth,
73 ysize => $image->getheight);
74 # grey background for grayscale images, red for color
75 my $back_color = $image->getchannels == 2 ? [ 128 ] : 'red';
76 $back->box(filled => 1, color => $back_color);
77 $back->rubthrough(src => $image);
80 # now we can write safely to jpeg or pnm
82 Some formats support multiple files, so if you want to convert from
83 say tiff to jpeg, you'll need multiple output files:
85 my @images = Imager->read_multi(file => 'input.tif')
86 or die Imager->errstr;
88 for my $image (@images) {
89 $image->write(file => sprintf('output%02d.jpg', $index++))
90 or die $image->errstr;
93 =head1 IMAGE SYNTHESIS
95 =head2 Creating an image
97 To create a simple RGB image, supply the image width and height to the
100 my $rgb = Imager->new(xsize=>$width, ysize=>$height);
102 If you also want an alpha channel:
104 my $rgb_alpha = Imager->new(xsize=>$width, ysize=>$height, channels=>4);
106 To make a grayscale image:
108 my $gray = Imager->new(xsize=>$width, ysize=>$height, channels=>1);
110 and a grayscale image with an alpha channel:
112 my $gray_alpha = Imager->new(xsize=>$width, ysize=>$height, channels=>2);
114 When a new image is created this way all samples are set to zero -
115 black for 1 or 3 channel images, transparent black for 2 or 4 channel
118 You can also create paletted images and images with more than 8-bits
119 per channel, see L<Imager::ImageTypes> for more details.
121 =head2 Setting the background of a new image
123 To set the background of a new image to a solid color, use the box()
124 method with no limits, and C<< filled=>1 >>:
126 $image->box(filled=>1, color=>$color);
128 As always, a color can be specified as an L<Imager::Color> object:
130 my $white = Imager::Color->new(255, 255, 255);
131 $image->box(filled=>1, color=>$white);
133 or you supply any single scalar that Imager::Color's new() method
134 accepts as a color description:
136 $image->box(filled=>1, color=>'white');
137 $image->box(filled=>1, color=>'#FF0000');
138 $image->box(filled=>1, color=>[ 255, 255, 255 ]);
140 You can also fill the image with a fill object:
143 # create the fill object
144 my $fill = Imager::Fill->new(hatch=>'check1x1')
145 $image->box(fill=>$fill);
147 # let Imager create one automatically
148 $image->box(fill=>{ hatch=>'check1x1' });
150 See L<Imager::Fill> for information on Imager's fill objects.
152 =head1 WORLD WIDE WEB
154 As with any CGI script it's up to you to validate data and set limits
155 on any parameters supplied to Imager.
157 For example, if you allow the caller to set the size of an output
158 image you should limit the size to prevent the client from specifying
159 an image size that will consume all available memory.
161 This is beside any any other controls you need over access to data.
163 See L<CGI> for a module useful for processing CGI submitted data.
165 =head2 Returning an image from a CGI script
167 This is similar to writing to a file, but you also need to supply the
168 information needed by the web browser to identify the file format:
170 my $img = ....; # create the image and generate the contents
171 ++$|; # make sure the content type isn't buffered
172 print "Content-Type: image/png\n\n";
174 $img->write(fd=>fileno(STDOUT), type=>'png')
177 You need to set the Content-Type header depending on the file format
178 you send to the web browser.
180 If you want to supply a content-length header, write the image to a
183 my $img = ....; # create the image and generate the contents
185 $img->write(type=>'png', data=>\$data)
187 print "Content-Type: image/png\n";
188 print "Content-Length: ",length($data),"\n\n";
192 See C<samples/samp-scale.cgi> and C<samples/samp-image.cgi> for a
193 couple of simple examples of producing an image from CGI.
195 =head2 Inserting a CGI image in a page
197 There's occasionally confusion on how to display an image generated by
198 Imager in a page generated by a CGI.
200 Your web browser handles this process as two requests, one for the
201 HTML page, and another for the image itself.
203 Each request needs to perform validation since an attacker can control
204 the values supplied to both requests.
206 How you make the data available to the image generation code depends
209 See C<samples/samp-form.cgi> and C<samples/samp-image.cgi> in the
210 Imager distribution for one approach. The POD in C<samp-form.cgi>
211 also discusses some of the issues involved.
213 =head2 Parsing an image posted via CGI
215 C<WARNING>: file format attacks have become a common attack vector,
216 make sure you have up to date image file format libraries, otherwise
217 trying to parse uploaded files, whether with Imager or some other
218 tool, may result in a remote attacker being able to run their own code
221 If your HTML form uses the correct magic, it can upload files to your
222 CGI script, in particular, you need to use C< method="post" > and
223 C<enctype="multipart/form-data"> in the C<form> tag, and use
224 C<type="file"> in the C<input>, for example:
226 <form action="/cgi-bin/yourprogram" method="post"
227 enctype="multipart/form-data">
228 <input type="file" name="myimage" />
229 <input type="submit value="Upload Image" />
238 first check that the user supplied a file
246 have Imager read the image
250 # returns the client's name for the file, don't open this locally
252 # 1. check the user supplied a file
253 my $filename = $cgi->param('myimage');
255 # 2. get the file handle
256 my $fh = $cgi->upload('myimage');
260 # 3. have Imager read the image
261 my $img = Imager->new;
262 if ($img->read(fh=>$fh)) {
263 # we can now process the image
266 # else, you probably have an incorrect form or input tag
268 # else, the user didn't select a file
270 See C<samples/samp-scale.cgi> and C<samples/samp-tags.cgi> in the
271 Imager distribution for example code.
273 You may also want to set limits on the size of the image read, using
274 Imager's C<set_file_limits> method, documented in
275 L<Imager::Files/set_file_limits>. For example:
277 # limit to 10 million bytes of memory usage
278 Imager->set_file_limits(bytes => 10_000_000);
280 # limit to 1024 x 1024
281 Imager->set_file_limits(width => 1024, height => 1024);
285 =head2 Adding a border to an image
287 First make a new image with space for the border:
289 my $border_width = ...;
290 my $border_height = ...;
291 my $out = Imager->new(xsize => $source->getwidth() + 2 * $border_width,
292 ysize => $source->getheight() + 2 * $border_height,
293 bits => $source->bits,
294 channels => $source->getchannels);
296 Then paste the source image into the new image:
298 $out->paste(left => $border_width,
299 top => $border_height,
302 Whether you draw the border before or after pasting the original image
303 depends on whether you want the border to overlap the image, for
304 example a semi-tranparent border drawn after pasting the source image
305 could overlap the edge without hiding it.
307 If you want a solid border you could just fill the image before
308 pasting the source for simplicity:
310 $out->box(filled=>1, color=>'red');
311 $out->paste(left => $border_width,
312 top => $border_height,
322 =head2 Measuring text
324 =head2 Word wrapping text
326 =head2 Shearing (slanting) or Rotating text
328 This requires that you have Imager installed with Freetype 2.x support
329 installed, and that the font be created using the Freetype 2.x driver,
332 my $font = Imager::Font->new(file=>$fontfile, type=>'ft2');
334 First you need a transformation matrix, for shearing that could be:
336 my $angle_in_radians = ...;
337 my $tan_angle = sin($angle_rads) / cos($angle_rads);
338 # shear horizontally, supply this as y instead to do it vertically
339 my $matrix = Imager::Matrix2d->shear(x=>$tan_angle);
341 For rotation that would be:
343 my $matrix = Imager::Matrix2d->rotate(radians => $angle_in_radians);
347 my $matrix = Imager::Matrix2d->rotate(degrees => $angle_in_degrees);
349 Feed that to the font object:
351 $font->transform(matrix => $matrix);
353 and draw the text as normal:
355 $image->string(string => $text,
361 See samples/slant_text.pl for a comprehensive example, including
362 calculating the transformed bounding box to create an image to fit the
363 transformed text into.
365 =head1 IMAGE TRANSFORMATION
367 =head2 Shearing an image
373 When Imager reads a file it does a magic number check to determine the
374 file type, so "foo.png" could actually be a GIF image, and Imager will
377 You can check the actual format of the image by looking at the
380 my $format = $image->tags(name=>'i_format');
382 =head2 Image spatial resolution
384 Most image file formats store information about the physical size of
385 the pixels, though in some cases that information isn't useful.
387 Imager stores this information in the tags C<i_xres> and C<i_yres>,
388 and this is always stored in dots per inch.
390 Some formats, including TIFF and JPEG allow you to change the units
391 spatial resolution information is stored in, if you set the tag that
392 changes this the Imager will convert C<i_xres> and C<i_yres> to those
393 units when it writes the file.
395 For example to set the resolution to 300 dpi:
397 $image->settag(name => 'i_xres', value => 300);
398 $image->settag(name => 'i_yres', value => 300);
400 If you want the file format to store the resolution in some other
401 unit, for example you can write a TIFF file that stores the resolution
402 in pixels per centimeter, you would do:
405 $image->settag(name => 'i_xres', value => 150 * 2.54);
406 $image->settag(name => 'i_yres', value => 150 * 2.54);
407 $image->settag(name => 'tiff_resolutionunit', value => 3);
413 Tony Cook <tony@develop-help.com>
417 L<Imager>, L<Imager::Files>, L<Imager::Draw>.