]> git.imager.perl.org - imager.git/blob - lib/Imager/Files.pod
33706d0b57dcdfb1f9719d661b648dd5896c70b8
[imager.git] / lib / Imager / Files.pod
1 =head1 NAME
2
3 Imager::Files - working with image files
4
5 =head1 SYNOPSIS
6
7   my $img = ...;
8   $img->write(file=>$filename, type=>$type)
9     or die "Cannot write: ",$img->errstr;
10
11   $img = Imager->new;
12   $img->read(file=>$filename, type=>$type)
13     or die "Cannot read: ", $img->errstr;
14
15   Imager->write_multi({ file=> $filename, ... }, @images)
16     or die "Cannot write: ", Imager->errstr;
17
18   my @imgs = Imager->read_multi(file=>$filename)
19     or die "Cannot read: ", Imager->errstr;
20
21   Imager->set_file_limits(width=>$max_width, height=>$max_height)
22
23 =head1 DESCRIPTION
24
25 You can read and write a variety of images formats, assuming you have
26 the appropriate libraries, and images can be read or written to/from
27 files, file handles, file descriptors, scalars, or through callbacks.
28
29 To see which image formats Imager is compiled to support the following
30 code snippet is sufficient:
31
32   use Imager;
33   print join " ", keys %Imager::formats;
34
35 This will include some other information identifying libraries rather
36 than file formats.
37
38 =over 
39
40 =item read
41
42 Reading writing to and from files is simple, use the C<read()>
43 method to read an image:
44
45   my $img = Imager->new;
46   $img->read(file=>$filename, type=>$type)
47     or die "Cannot read $filename: ", $img->errstr;
48
49 =item write
50
51 and the C<write()> method to write an image:
52
53   $img->write(file=>$filename, type=>$type)
54     or die "Cannot write $filename: ", $img->errstr;
55
56 =item read_multi
57
58 If you're reading from a format that supports multiple images per
59 file, use the C<read_multi()> method:
60
61   my @imgs = Imager->read_multi(file=>$filename, type=>$type)
62     or die "Cannot read $filename: ", Imager->errstr;
63
64 =item write_multi
65
66 and if you want to write multiple images to a single file use the
67 C<write_multi()> method:
68
69   Imager->write_multi({ file=> $filename, type=>$type }, @images)
70     or die "Cannot write $filename: ", Imager->errstr;
71
72 =back
73
74 If the I<filename> includes an extension that Imager recognizes, then
75 you don't need the I<type>, but you may want to provide one anyway.
76 See L</Guessing types> for information on controlling this
77 recognition.
78
79 The C<type> parameter is a lowercase representation of the file type,
80 and can be any of the following:
81
82   bmp   Windows BitMaP (BMP)
83   gif   Graphics Interchange Format (GIF)
84   jpeg  JPEG/JFIF
85   png   Portable Network Graphics (PNG)
86   pnm   Portable aNyMap (PNM)
87   raw   Raw
88   rgb   SGI .rgb files
89   tga   TARGA
90   tiff  Tagged Image File Format (TIFF)
91
92 When you read an image, Imager may set some tags, possibly including
93 information about the spatial resolution, textual information, and
94 animation information.  See L<Imager::ImageTypes/Tags> for specifics.
95
96 The open() method is a historical alias for the read() method.
97
98 =head2 Input and output
99
100 When reading or writing you can specify one of a variety of sources or
101 targets:
102
103 =over
104
105 =item file
106
107 The C<file> parameter is the name of the image file to be written to
108 or read from.  If Imager recognizes the extension of the file you do
109 not need to supply a C<type>.
110
111 =item fh
112
113 C<fh> is a file handle, typically either returned from
114 C<<IO::File->new()>>, or a glob from an C<open> call.  You should call
115 C<binmode> on the handle before passing it to Imager.
116
117 Imager will set the handle to autoflush to make sure any buffered data
118 is flushed , since Imager will write to the file descriptor (from
119 fileno()) rather than writing at the perl level.
120
121 =item fd
122
123 C<fd> is a file descriptor.  You can get this by calling the
124 C<fileno()> function on a file handle, or by using one of the standard
125 file descriptor numbers.
126
127 If you get this from a perl file handle, you may need to flush any
128 buffered output, otherwise it may appear in the output stream after
129 the image.
130
131 =item data
132
133 When reading data, C<data> is a scalar containing the image file data,
134 when writing, C<data> is a reference to the scalar to save the image
135 file data too.  For GIF images you will need giflib 4 or higher, and
136 you may need to patch giflib to use this option for writing.
137
138 =item callback
139
140 Imager will make calls back to your supplied coderefs to read, write
141 and seek from/to/through the image file.
142
143 When reading from a file you can use either C<callback> or C<readcb>
144 to supply the read callback, and when writing C<callback> or
145 C<writecb> to supply the write callback.
146
147 When writing you can also supply the C<maxbuffer> option to set the
148 maximum amount of data that will be buffered before your write
149 callback is called.  Note: the amount of data supplied to your
150 callback can be smaller or larger than this size.
151
152 The read callback is called with 2 parameters, the minimum amount of
153 data required, and the maximum amount that Imager will store in it's C
154 level buffer.  You may want to return the minimum if you have a slow
155 data source, or the maximum if you have a fast source and want to
156 prevent many calls to your perl callback.  The read data should be
157 returned as a scalar.
158
159 Your write callback takes exactly one parameter, a scalar containing
160 the data to be written.  Return true for success.
161
162 The seek callback takes 2 parameters, a I<POSITION>, and a I<WHENCE>,
163 defined in the same way as perl's seek function.
164
165 You can also supply a C<closecb> which is called with no parameters
166 when there is no more data to be written.  This could be used to flush
167 buffered data.
168
169 =back
170
171 =head2 Guessing types
172
173 Imager uses the code reference in $Imager::FORMATGUESS to guess the
174 file type when you don't supply a C<type>.  The code reference is
175 called with a single parameter, the filename of the file.  The code
176 reference is only called if a C<file> parameter is supplied to the
177 file access method.
178
179 Return either a valid Imager file type, or undef.
180
181   # I'm writing jpegs to weird filenames
182   local $Imager::FORMATGUESS = sub { 'jpeg' };
183
184 =head2 Limiting the sizes of images you read
185
186 In some cases you will be receiving images from an untested source,
187 such as submissions via CGI.  To prevent such images from consuming
188 large amounts of memory, you can set limits on the dimensions of
189 images you read from files:
190
191 =over
192
193 =item *
194
195 width - limit the width in pixels of the image
196
197 =item *
198
199 height - limit the height in pixels of the image
200
201 =item *
202
203 bytes - limits the amount of storage used by the image.  This depends
204 on the width, height, channels and sample size of the image.  For
205 paletted images this is calculated as if the image was expanded to a
206 direct color image.
207
208 =back
209
210 To set the limits, call the class method set_file_limits:
211
212   Imager->set_file_limits(width=>$max_width, height=>$max_height);
213
214 You can pass any or all of the limits above, any limits you do not
215 pass are left as they were.
216
217 Any limit of zero is treated as unlimited.
218
219 By default, all of the limits are zero, or unlimited.
220
221 You can reset all of the limited to their defaults by passing in the
222 reset parameter as a true value:
223
224   # no limits
225   Imager->set_file_limits(reset=>1);
226
227 This can be used with the other limits to reset all but the limit you
228 pass:
229
230   # only width is limited
231   Imager->set_file_limits(reset=>1, width=>100);
232
233   # only bytes is limited
234   Imager->set_file_limits(reset=>1, bytes=>10_000_000);
235
236 You can get the current limits with the get_file_limits() method:
237
238   my ($max_width, $max_height, $max_bytes) =
239      Imager->get_file_limits();
240
241
242 =head1 TYPE SPECIFIC INFORMATION
243
244 The different image formats can write different image type, and some have
245 different options to control how the images are written.
246
247 When you call C<write()> or C<write_multi()> with an option that has
248 the same name as a tag for the image format you're writing, then the
249 value supplied to that option will be used to set the corresponding
250 tag in the image.  Depending on the image format, these values will be
251 used when writing the image.
252
253 This replaces the previous options that were used when writing GIF
254 images.  Currently if you use an obsolete option, it will be converted
255 to the equivalent tag and Imager will produced a warning.  You can
256 suppress these warnings by calling the C<Imager::init()> function with
257 the C<warn_obsolete> option set to false:
258
259   Imager::init(warn_obsolete=>0);
260
261 At some point in the future these obsolete options will no longer be
262 supported.
263
264 =head2 PNM (Portable aNy Map)
265
266 Imager can write PGM (Portable Gray Map) and PPM (Portable PixMaps)
267 files, depending on the number of channels in the image.  Currently
268 the images are written in binary formats.  Only 1 and 3 channel images
269 can be written, including 1 and 3 channel paletted images.
270
271   $img->write(file=>'foo.ppm') or die $img->errstr;
272
273 Imager can read both the ASCII and binary versions of each of the PBM
274 (Portable BitMap), PGM and PPM formats.
275
276   $img->read(file=>'foo.ppm') or die $img->errstr;
277
278 PNM does not support the spatial resolution tags.
279
280 =head2 JPEG
281
282 You can supply a C<jpegquality> parameter (0-100) when writing a JPEG
283 file, which defaults to 75%.  Only 1 and 3 channel images
284 can be written, including 1 and 3 channel paletted images.
285
286   $img->write(file=>'foo.jpg', jpegquality=>90) or die $img->errstr;
287
288 Imager will read a grayscale JPEG as a 1 channel image and a color
289 JPEG as a 3 channel image.
290
291   $img->read(file=>'foo.jpg') or die $img->errstr;
292
293 The following tags are set in a JPEG image when read, and can be set
294 to control output:
295
296 =over
297
298 =item jpeg_density_unit
299
300 The value of the density unit field in the JFIF header.  This is
301 ignored on writing if the i_aspect_only tag is non-zero.
302
303 The C<i_xres> and C<i_yres> tags are expressed in pixels per inch no
304 matter the value of this tag, they will be converted to/from the value
305 stored in the JPEG file.
306
307 =item jpeg_density_unit_name
308
309 This is set when reading a JPEG file to the name of the unit given by
310 C<jpeg_density_unit>.  Possible results include C<inch>,
311 C<centimeter>, C<none> (the C<i_aspect_only> tag is also set reading
312 these files).  If the value of jpeg_density_unit is unknown then this
313 tag isn't set.
314
315 =item jpeg_comment
316
317 Text comment.
318
319 =back
320
321 JPEG supports the spatial resolution tags C<i_xres>, C<i_yres> and
322 C<i_aspect_only>.
323
324 If an APP1 block containing EXIF information is found, then any of the
325 following tags can be set:
326
327 =over
328
329 exif_aperture exif_artist exif_brightness exif_color_space
330 exif_contrast exif_copyright exif_custom_rendered exif_date_time
331 exif_date_time_digitized exif_date_time_original
332 exif_digital_zoom_ratio exif_exposure_bias exif_exposure_index
333 exif_exposure_mode exif_exposure_program exif_exposure_time
334 exif_f_number exif_flash exif_flash_energy exif_flashpix_version
335 exif_focal_length exif_focal_length_in_35mm_film
336 exif_focal_plane_resolution_unit exif_focal_plane_x_resolution
337 exif_focal_plane_y_resolution exif_gain_control exif_image_description
338 exif_image_unique_id exif_iso_speed_rating exif_make exif_max_aperture
339 exif_metering_mode exif_model exif_orientation exif_related_sound_file
340 exif_resolution_unit exif_saturation exif_scene_capture_type
341 exif_sensing_method exif_sharpness exif_shutter_speed exif_software
342 exif_spectral_sensitivity exif_sub_sec_time
343 exif_sub_sec_time_digitized exif_sub_sec_time_original
344 exif_subject_distance exif_subject_distance_range
345 exif_subject_location exif_tag_light_source exif_user_comment
346 exif_version exif_white_balance exif_x_resolution exif_y_resolution
347
348 =back
349
350 The following derived tags can also be set:
351
352 =over
353
354 exif_color_space_name exif_contrast_name exif_custom_rendered_name
355 exif_exposure_mode_name exif_exposure_program_name exif_flash_name
356 exif_focal_plane_resolution_unit_name exif_gain_control_name
357 exif_light_source_name exif_metering_mode_name
358 exif_resolution_unit_name exif_saturation_name
359 exif_scene_capture_type_name exif_sensing_method_name
360 exif_sharpness_name exif_subject_distance_range_name
361 exif_white_balance_name
362
363 =back
364
365 The derived tags are for enumerated fields, when the value for the
366 base field is valid then the text that appears in the EXIF
367 specification for that value appears in the derived field.  So for
368 example if C<exf_metering_mode> is C<5> then
369 C<exif_metering_mode_name> is set to C<Pattern>.
370
371 =head2 GIF (Graphics Interchange Format)
372
373 When writing one of more GIF images you can use the same
374 L<Quantization Options|Imager::ImageTypes> as you can when converting
375 an RGB image into a paletted image.
376
377 When reading a GIF all of the sub-images are combined using the screen
378 size and image positions into one big image, producing an RGB image.
379 This may change in the future to produce a paletted image where possible.
380
381 When you read a single GIF with C<$img-E<gt>read()> you can supply a
382 reference to a scalar in the C<colors> parameter, if the image is read
383 the scalar will be filled with a reference to an anonymous array of
384 L<Imager::Color> objects, representing the palette of the image.  This
385 will be the first palette found in the image.  If you want the
386 palettes for each of the images in the file, use C<read_multi()> and
387 use the C<getcolors()> method on each image.
388
389 GIF does not support the spatial resolution tags.
390
391 Imager will set the following tags in each image when reading, and can
392 use most of them when writing to GIF:
393
394 =over
395
396 =item gif_left
397
398 the offset of the image from the left of the "screen" ("Image Left
399 Position")
400
401 =item gif_top
402
403 the offset of the image from the top of the "screen" ("Image Top Position")
404
405 =item gif_interlace
406
407 non-zero if the image was interlaced ("Interlace Flag")
408
409 =item gif_screen_width
410
411 =item gif_screen_height
412
413 the size of the logical screen. When writing this is used as the
414 minimum.  If any image being written would extend beyond this the
415 screen size is extended.  ("Logical Screen Width", "Logical Screen
416 Height").
417
418 When writing this is used as a minimum, if the combination of the
419 image size and the image's C<gif_left> and C<gif_top> is beyond this
420 size then the screen size will be expanded.
421
422 =item gif_local_map
423
424 Non-zero if this image had a local color map.  If set for an image
425 when writing the image is quantized separately from the other images
426 in the file.
427
428 =item gif_background
429
430 The index in the global colormap of the logical screen's background
431 color.  This is only set if the current image uses the global
432 colormap.  You can set this on write too, but for it to choose the
433 color you want, you will need to supply only paletted images and set
434 the C<gif_eliminate_unused> tag to 0.
435
436 =item gif_trans_index
437
438 The index of the color in the colormap used for transparency.  If the
439 image has a transparency then it is returned as a 4 channel image with
440 the alpha set to zero in this palette entry.  This value is not used
441 when writing. ("Transparent Color Index")
442
443 =item gif_trans_color
444
445 A reference to an Imager::Color object, which is the colour to use for
446 the palette entry used to represent transparency in the palette.  You
447 need to set the transp option (see L<Quantization options>) for this
448 value to be used.
449
450 =item gif_delay
451
452 The delay until the next frame is displayed, in 1/100 of a second. 
453 ("Delay Time").
454
455 =item gif_user_input
456
457 whether or not a user input is expected before continuing (view dependent) 
458 ("User Input Flag").
459
460 =item gif_disposal
461
462 how the next frame is displayed ("Disposal Method")
463
464 =item gif_loop
465
466 the number of loops from the Netscape Loop extension.  This may be zero.
467
468 =item gif_comment
469
470 the first block of the first gif comment before each image.
471
472 =item gif_eliminate_unused
473
474 If this is true, when you write a paletted image any unused colors
475 will be eliminated from its palette.  This is set by default.
476
477 =back
478
479 Where applicable, the ("name") is the name of that field from the GIF89 
480 standard.
481
482 The following gif writing options are obsolete, you should set the
483 corresponding tag in the image, either by using the tags functions, or
484 by supplying the tag and value as options.
485
486 =over
487
488 =item gif_each_palette
489
490 Each image in the gif file has it's own palette if this is non-zero.
491 All but the first image has a local colour table (the first uses the
492 global colour table.
493
494 Use C<gif_local_map> in new code.
495
496 =item interlace
497
498 The images are written interlaced if this is non-zero.
499
500 Use C<gif_interlace> in new code.
501
502 =item gif_delays
503
504 A reference to an array containing the delays between images, in 1/100
505 seconds.
506
507 Use C<gif_delay> in new code.
508
509 =item gif_positions
510
511 A reference to an array of references to arrays which represent screen
512 positions for each image.
513
514 New code should use the C<gif_left> and C<gif_top> tags.
515
516 =item gif_loop_count
517
518 If this is non-zero the Netscape loop extension block is generated,
519 which makes the animation of the images repeat.
520
521 This is currently unimplemented due to some limitations in giflib.
522
523 =back
524
525 You can supply a C<page> parameter to the C<read()> method to read
526 some page other than the first.  The page is 0 based:
527
528   # read the second image in the file
529   $image->read(file=>"example.gif", page=>1)
530     or die "Cannot read second page: ",$image->errstr,"\n";
531
532 Before release 0.46, Imager would read multi-image GIF image files
533 into a single image, overlaying each of the images onto the virtual
534 GIF screen.
535
536 As of 0.46 the default is to read the first image from the file, as if
537 called with C<< page => 0 >>.
538
539 You can return to the previous behaviour by calling read with the
540 C<gif_consolidate> parameter set to a true value:
541
542   $img->read(file=>$some_gif_file, gif_consolidate=>1);
543
544 =head2 TIFF (Tagged Image File Format)
545
546 Imager can write images to either paletted or RGB TIFF images,
547 depending on the type of the source image.  Currently if you write a
548 16-bit/sample or double/sample image it will be written as an
549 8-bit/sample image.  Only 1 or 3 channel images can be written.
550
551 If you are creating images for faxing you can set the I<class>
552 parameter set to C<fax>.  By default the image is written in fine
553 mode, but this can be overridden by setting the I<fax_fine> parameter
554 to zero.  Since a fax image is bi-level, Imager uses a threshold to
555 decide if a given pixel is black or white, based on a single channel.
556 For greyscale images channel 0 is used, for color images channel 1
557 (green) is used.  If you want more control over the conversion you can
558 use $img->to_paletted() to product a bi-level image.  This way you can
559 use dithering:
560
561   my $bilevel = $img->to_paletted(colors=>[ NC(0,0,0), NC(255,255,255) ],
562                                   make_colors => 'none',
563                                   translate => 'errdiff',
564                                   errdiff => 'stucki');
565
566 =over
567
568 =item class
569
570 If set to 'fax' the image will be written as a bi-level fax image.
571
572 =item fax_fine
573
574 By default when I<class> is set to 'fax' the image is written in fine
575 mode, you can select normal mode by setting I<fax_fine> to 0.
576
577 =back
578
579 Imager should be able to read any TIFF image you supply.  Paletted
580 TIFF images are read as paletted Imager images, since paletted TIFF
581 images have 16-bits/sample (48-bits/color) this means the bottom
582 8-bits are lost, but this shouldn't be a big deal.  Currently all
583 direct color images are read at 8-bits/sample.
584
585 TIFF supports the spatial resolution tags.  See the
586 C<tiff_resolutionunit> tag for some extra options.
587
588 The following tags are set in a TIFF image when read, and can be set
589 to control output:
590
591 =over
592
593 =item tiff_resolutionunit
594
595 The value of the ResolutionUnit tag.  This is ignored on writing if
596 the i_aspect_only tag is non-zero.
597
598 The C<i_xres> and C<i_yres> tags are expressed in pixels per inch no
599 matter the value of this tag, they will be converted to/from the value
600 stored in the TIFF file.
601
602 =item tiff_resolutionunit_name
603
604 This is set when reading a TIFF file to the name of the unit given by
605 C<tiff_resolutionunit>.  Possible results include C<inch>,
606 C<centimeter>, C<none> (the C<i_aspect_only> tag is also set reading
607 these files) or C<unknown>.
608
609 =item tiff_bitspersample
610
611 Bits per sample from the image.  This value is not used when writing
612 an image, it is only set on a read image.
613
614 =item tiff_photometric
615
616 Value of the PhotometricInterpretation tag from the image.  This value
617 is not used when writing an image, it is only set on a read image.
618
619 =item tiff_documentname
620
621 =item tiff_imagedescription
622
623 =item tiff_make
624
625 =item tiff_model
626
627 =item tiff_pagename
628
629 =item tiff_software
630
631 =item tiff_datetime
632
633 =item tiff_artist
634
635 =item tiff_hostcomputer
636
637 Various strings describing the image.  tiff_datetime must be formatted
638 as "YYYY:MM:DD HH:MM:SS".  These correspond directly to the mixed case
639 names in the TIFF specification.  These are set in images read from a
640 TIFF and saved when writing a TIFF image.
641
642 You can supply a C<page> parameter to the C<read()> method to read
643 some page other than the first.  The page is 0 based:
644
645   # read the second image in the file
646   $image->read(file=>"example.tif", page=>1)
647     or die "Cannot read second page: ",$image->errstr,"\n";
648
649 =back
650
651 =head2 BMP (BitMaP)
652
653 Imager can write 24-bit RGB, and 8, 4 and 1-bit per pixel paletted
654 Windows BMP files.  Currently you cannot write compressed BMP files
655 with Imager.
656
657 Imager can read 24-bit RGB, and 8, 4 and 1-bit perl pixel paletted
658 Windows BMP files.  There is some support for reading 16-bit per pixel
659 images, but I haven't found any for testing.
660
661 BMP has no support for multi-image files.
662
663 BMP files support the spatial resolution tags, but since BMP has no
664 support for storing only an aspect ratio, if C<i_aspect_only> is set
665 when you write the C<i_xres> and C<i_yres> values are scaled so the
666 smaller is 72 DPI.
667
668 The following tags are set when you read an image from a BMP file:
669
670 =over
671
672 =item bmp_compression
673
674 The type of compression, if any.  This can be any of the following
675 values:
676
677 =over
678
679 =item BI_RGB (0)
680
681 Uncompressed.
682
683 =item BI_RLE8 (1)
684
685 8-bits/pixel paletted value RLE compression.
686
687 =item BI_RLE4 (2)
688
689 4-bits/pixel paletted value RLE compression.
690
691 =item BI_BITFIELDS (3)
692
693 Packed RGB values.
694
695 =back
696
697 =item bmp_compression_name
698
699 The bmp_compression value as a BI_* string
700
701 =item bmp_important_colors
702
703 The number of important colors as defined by the writer of the image.
704
705 =item bmp_used_colors
706
707 Number of color used from the BMP header
708
709 =item bmp_filesize
710
711 The file size from the BMP header
712
713 =item bmp_bit_count
714
715 Number of bits stored per pixel. (24, 8, 4 or 1)
716
717 =back
718
719 =head2 TGA (TarGA)
720
721 When storing targa images rle compression can be activated with the
722 'compress' parameter, the 'idstring' parameter can be used to set the
723 targa comment field and the 'wierdpack' option can be used to use the
724 15 and 16 bit targa formats for rgb and rgba data.  The 15 bit format
725 has 5 of each red, green and blue.  The 16 bit format in addition
726 allows 1 bit of alpha.  The most significant bits are used for each
727 channel.
728
729
730 Tags:
731
732 =over
733
734 =item tga_idstring
735
736 =item tga_bitspp
737
738 =item compressed
739
740 =back
741
742 =head2 RAW
743
744 When reading raw images you need to supply the width and height of the
745 image in the xsize and ysize options:
746
747   $img->read(file=>'foo.raw', xsize=>100, ysize=>100)
748     or die "Cannot read raw image\n";
749
750 If your input file has more channels than you want, or (as is common),
751 junk in the fourth channel, you can use the datachannels and
752 storechannels options to control the number of channels in your input
753 file and the resulting channels in your image.  For example, if your
754 input image uses 32-bits per pixel with red, green, blue and junk
755 values for each pixel you could do:
756
757   $img->read(file=>'foo.raw', xsize=>100, ysize=>100, datachannels=>4,
758              storechannels=>3)
759     or die "Cannot read raw image\n";
760
761 Normally the raw image is expected to have the value for channel 1
762 immediately following channel 0 and channel 2 immediately following
763 channel 1 for each pixel.  If your input image has all the channel 0
764 values for the first line of the image, followed by all the channel 1
765 values for the first line and so on, you can use the interleave option:
766
767   $img->read(file=>'foo.raw', xsize=100, ysize=>100, interleave=>1)
768     or die "Cannot read raw image\n";
769
770 =head2 PNG
771
772 There are no PNG specific tags.
773
774 =head1 EXAMPLES
775
776 =head2 Producing an image from a CGI script
777
778 Once you have an image the basic mechanism is:
779
780 =over
781
782 =item 1.
783
784 set STDOUT to autoflush
785
786 =item 2.
787
788 output a content-type header, and optionally a content-length header
789
790 =item 3.
791
792 put STDOUT into binmode
793
794 =item 4.
795
796 call write() with the C<fd> or C<fh> parameter.  You will need to
797 provide the C<type> parameter since Imager can't use the extension to
798 guess the file format you want.
799
800 =back
801
802   # write an image from a CGI script
803   # using CGI.pm
804   use CGI qw(:standard);
805   $| = 1;
806   binmode STDOUT;
807   print header(-type=>'image/gif');
808   $img->write(type=>'gif', fd=>fileno(STDOUT))
809     or die $img->errstr;
810
811 If you want to send a content length you can send the output to a
812 scalar to get the length:
813
814   my $data;
815   $img->write(type=>'gif', data=>\$data)
816     or die $img->errstr;
817   binmode STDOUT;
818   print header(-type=>'image/gif', -content_length=>length($data));
819   print $data;
820
821 =head2 Writing an animated GIF
822
823 The basic idea is simple, just use write_multi():
824
825   my @imgs = ...;
826   Imager->write_multi({ file=>$filename, type=>'gif' }, @imgs);
827
828 If your images are RGB images the default quantization mechanism will
829 produce a very good result, but can take a long time to execute.  You
830 could either use the standard webmap:
831
832   Imager->write_multi({ file=>$filename, 
833                         type=>'gif',
834                         make_colors=>'webmap' },
835                       @imgs);
836
837 or use a median cut algorithm to built a fairly optimal color map:
838
839   Imager->write_multi({ file=>$filename,
840                         type=>'gif',
841                         make_colors=>'mediancut' },
842                       @imgs);
843
844 By default all of the images will use the same global colormap, which
845 will produce a smaller image.  If your images have significant color
846 differences, you may want to generate a new palette for each image:
847
848   Imager->write_multi({ file=>$filename,
849                         type=>'gif',
850                         make_colors=>'mediancut',
851                         gif_local_map => 1 },
852                       @imgs);
853
854 which will set the C<gif_local_map> tag in each image to 1.
855 Alternatively, if you know only some images have different colors, you
856 can set the tag just for those images:
857
858   $imgs[2]->settag(name=>'gif_local_map', value=>1);
859   $imgs[4]->settag(name=>'gif_local_map', value=>1);
860
861 and call write_multi() without a C<gif_local_map> parameter, or supply
862 an arrayref of values for the tag:
863
864   Imager->write_multi({ file=>$filename,
865                         type=>'gif',
866                         make_colors=>'mediancut',
867                         gif_local_map => [ 0, 0, 1, 0, 1 ] },
868                       @imgs);
869
870 Other useful parameters include C<gif_delay> to control the delay
871 between frames and C<transp> to control transparency.
872
873 =head2 Reading tags after reading an image
874
875 This is pretty simple:
876
877   # print the author of a TIFF, if any
878   my $img = Imager->new;
879   $img->read(file=>$filename, type='tiff') or die $img->errstr;
880   my $author = $img->tags(name=>'tiff_author');
881   if (defined $author) {
882     print "Author: $author\n";
883   }
884
885 =head1 BUGS
886
887 When saving Gif images the program does NOT try to shave of extra
888 colors if it is possible.  If you specify 128 colors and there are
889 only 2 colors used - it will have a 128 colortable anyway.
890
891 =head1 SEE ALSO
892
893 Imager(3)
894
895 =cut