1Imager::Tutorial(3)   User Contributed Perl Documentation  Imager::Tutorial(3)
2
3
4

NAME

6       Imager::Tutorial - an introduction to Imager.
7

DESCRIPTION

9   Before you start
10       If you have the necessary knowledge, install the image format libraries
11       you want Imager image file support for, and Imager itself, otherwise
12       arrange to have it done.
13
14       You will also want some sort of image viewer tool, whether an image
15       editor like Photoshop or the GIMP, or a web browser.
16
17   Hello Boxes! - A Simple Start
18       As with any perl program it's useful to start with a #! line, and to
19       enable strict mode:
20
21         #!/usr/bin/perl -w
22         # you might to 'use warnings;' instead of the -w above
23         use strict;
24
25       These lines will be omitted in further examples.
26
27       As with any module, you need to load it:
28
29         use Imager;
30
31       Now create a image to draw on:
32
33         my $image = Imager->new(xsize => 100, ysize => 100);
34
35       and draw a couple of filled rectangles on it:
36
37         $image->box(xmin => 0, ymin => 0, xmax => 99, ymax => 99,
38                     filled => 1, color => 'blue');
39         $image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
40                     filled => 1, color => 'green');
41
42       Since the first box fills the whole image, it can be simplified to:
43
44         $image->box(filled => 1, color => 'blue');
45
46       and save it to a file:
47
48         $image->write(file=>'tutorial1.ppm')
49             or die 'Cannot save tutorial1.ppm: ', $image->errstr;
50
51       So our completed program is:
52
53         use Imager;
54
55         my $image = Imager->new(xsize => 100, ysize => 100);
56
57         $image->box(filled => 1, color => 'blue');
58         $image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
59                     filled => 1, color => 'green');
60
61         $image->write(file=>'tutorial1.ppm')
62             or die 'Cannot save tutorial1.ppm: ', $image->errstr;
63
64   Adding some text
65       The first thing you need to draw text is a font object:
66
67         # use a different file, depending on the font support you have in
68         # your installed Imager.
69         my $font_filename = 'fontfiles/ImUgly.ttf';
70         my $font = Imager::Font->new(file=>$font_filename)
71           or die "Cannot load $font_filename: ", Imager->errstr;
72
73       If you're on Windows, you can supply a face name instead:
74
75         my $font = Imager::Font->new(face=>'Arial Bold')
76           or die "Cannot load 'Arial Bold: ", Imager->errstr;
77
78       and draw the text:
79
80         my $text = "Hello Boxes!";
81         my $text_size = 12;
82
83         $font->align(string => $text,
84                      size => $text_size,
85                      color => 'red',
86                      x => $image->getwidth/2,
87                      y => $image->getheight/2,
88                      halign => 'center',
89                      valign => 'center',
90                      image => $image);
91
92       So inserting this into our existing code we have:
93
94         use Imager;
95
96         my $image = Imager->new(xsize => 100, ysize => 100);
97
98         $image->box(xmin => 0, ymin => 0, xmax => 99, ymax => 99,
99                     filled => 1, color => 'blue');
100         $image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
101                     filled => 1, color => 'green');
102
103         # use a different file, depending on the font support you have in
104         # your installed Imager.
105         my $font_filename = 'fontfiles/ImUgly.ttf';
106         my $font = Imager::Font->new(file=>$font_filename)
107           or die "Cannot load $font_filename: ", Imager->errstr;
108
109         my $text = "Hello Boxes!";
110         my $text_size = 12;
111
112         $font->align(string => $text,
113                      size => $text_size,
114                      color => 'red',
115                      x => $image->getwidth/2,
116                      y => $image->getheight/2,
117                      halign => 'center',
118                      valign => 'center',
119                      image => $image);
120
121         $image->write(file=>'tutorial2.ppm')
122             or die 'Cannot save tutorial2.ppm: ', $image->errstr;
123
124   Using an existing image as a base
125       To load an image from a file, first create an empty image object:
126
127         my $read_image = Imager->new;
128
129       then call the read method:
130
131         my $image_source = shift; # from the command-line
132         $read_image->read(file=>$image_source)
133           or die "Cannot load $image_source: ", $image->errstr;
134
135       To keep to our working size, we'll scale the image:
136
137         # the scale() method always does a proportional scale, we don't want
138         # that here
139         my $scaled_image = $read_image->scaleX(pixels=>100)->scaleY(pixels=>100);
140
141       draw our inner box on that, and save the result:
142
143         $scaled_image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
144                     filled => 1, color => 'green');
145
146         $scaled_image->write(file=>'tutorial3.ppm')
147             or die 'Cannot save tutorial3.ppm: ', $image->errstr;
148
149       so the complete program is:
150
151         use Imager;
152
153         my $read_image = Imager->new;
154
155         my $image_source = shift; # from the command-line
156         $read_image->read(file=>$image_source)
157           or die "Cannot load $image_source: ", $image->errstr;
158
159         # the scale() method always does a proportional scale, we don't want
160         # that here
161         my $scaled_image = $read_image->scaleX(pixels=>100)->scaleY(pixels=>100);
162
163         $scaled_image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
164                     filled => 1, color => 'green');
165
166         $scaled_image->write(file=>'tutorial3.ppm')
167             or die 'Cannot save tutorial3.ppm: ', $image->errstr;
168

AUTHOR

170       Tony Cook <tonyc@cpan.org>
171

REVISION

173       $Revision$
174
175
176
177perl v5.34.0                      2021-07-22               Imager::Tutorial(3)
Impressum