Debian Codegraph @

README

raw
1 NAME
2 Image::Math::Constrain - Scaling math used in image size constraining
3 (such as thumbnails)
4
5 SYNOPSIS
6 use Image::Math::Constrain;
7
8 # Create the math object
9 my $math = Image::Math::Constrain->new(64, 48);
10
11 # Get the scaling values for an arbitrary image
12 my $Image = My::Image->load("myimage.jpg");
13 my $scaling = $math->constrain($Image->width, $Image->height);
14 die "Don't need to scale" if $scaling->{scale} == 1;
15
16 # Returns the three values as a list when called in array contect
17 my ($width, $height, $scale) = $math->constrain(800, 600);
18
19 # There are lots of different ways to specify the constrain
20
21 # Constrain based on width only
22 $math = Image::Math::Constrain->new(100, 0);
23
24 # Constrain based on height only
25 $math = Image::Math::Constrain->new(0, 100);
26
27 # Or you can provide the two values by ARRAY ref
28 $math = Image::Math::Constrain->new( [ 64, 48 ] );
29
30 # Constrain height and width by the same value
31 $math = Image::Math::Constrain->new(100);
32
33 # Various string forms to do the same thing
34 $math = Image::Math::Constrain->new('constrain(800x600)');
35 $math = Image::Math::Constrain->new('300x200');
36 $math = Image::Math::Constrain->new('300w200h');
37 $math = Image::Math::Constrain->new('100w');
38 $math = Image::Math::Constrain->new('100h');
39
40 # Serialises back to 'constrain(800x600)'.
41 # You can use this to store the object if you wish.
42 my $string = $math->as_string;
43
44 DESCRIPTION
45 There are a number of different modules and systems that constrain image
46 sizes, such as thumbnailing. Every one of these independantly implement
47 the same logic. That is, given a width and/or height constraint, they
48 check to see if the image is bigger than the constraint, and if so scale
49 the image down proportionally so that it fits withint the constraints.
50
51 Of course, they all do it slightly differnetly, and some do it better
52 than others.
53
54 "Image::Math::Constrain" has been created specifically to implement this
55 logic once, and implement it properly. Any module or script that does
56 image size constraining or thumbnailing should probably be using this
57 for its math.
58
59 METHODS
60 new $width, $height
61 -head2 new [ $width, $height ]
62
63 new $width_and_height
64 new $string
65 The "new" constructor takes the dimentions to which you wish to
66 constrain and creates a new math object.
67
68 You can feed a number of different height/width pairs to this object,
69 and it will returns the scaling you will need to do to shrink the image
70 down to the constraints, and the final width and height of the image
71 after scaling, at least one of which should match the constraint.
72
73 A value of zero is used to indicate that a dimension should not be
74 constrained. Thus, "->new(400, 0)" would indicate to constrain the width
75 to 400 pixels, but to ignore the height (only changing it to keep the
76 image proportional).
77
78 The constraint dimensions can be provided in a number of different
79 formats. See the Synopsis for a quick list of these. To stay compatible
80 with automated constraint generators, you can provide constrains as zero
81 width and zero height, and the math object will not attempt to do any
82 scaling, always returning the input width/height, and a scaling value of
83 1.
84
85 Once created, the object is fully Storable and re-usable and does not
86 store any state information from a single calculation run.
87
88 Returns a new Image::Math::Constrain object, or "undef" if the
89 constraints have been defined wrongly.
90
91 width
92 The "width" method gets the width constraint for the object.
93
94 Returns a positive integer, or zero if there is no width constraint.
95
96 height
97 The "height" method gets the height constrain for the object.
98
99 Returns a positive integer, or zero if there is no height constraint.
100
101 as_string
102 The "as_string" method returns the constrain rule as a string in the
103 format 'constrain(123x123)'. This string form is also supported by the
104 constructor and so it provides a good way to serialise the constrain
105 rule, should you ever need to do so.
106
107 As this value is not localisable, it should never really be shown to the
108 user directly, unless you are sure you will never add i18n to your app.
109
110 constrain $width, $height
111 The "constrain" method takes the height and width of an image and
112 applies the constrain math to them to get the final width, height and
113 the scaling value needed in order to get the your image from it's
114 current size to the final size.
115
116 The resulting size will be in proportion to the original (it will have
117 the same aspect ratio) and will never be larger than the original.
118
119 When called in array context, returns the new dimensions and scaling
120 value as a list, as in the following.
121
122 my ($width, $height, $scale) = $math->constrain(800, 600);
123
124 When called in scalar context, it returns a reference to a hash
125 containing the keys 'width', 'height', and 'scale'.
126
127 my $hash = $math->constrain(800, 600);
128
129 print "New Width : $hash->{width}\n";
130 print "New Height : $hash->{height}\n";
131 print "Scaling By : $hash->{scalar}\n";
132
133 Having been created correctly, the object will only return an error if
134 the width and height arguments are not correct (are not positive
135 integers).
136
137 In list context, returns a null list, so all three values will be
138 "undef".
139
140 In scalar context, just returns "undef".
141
142 TO DO
143 - Write more special-case unit tests
144
145 SUPPORT
146 Bugs should always be submitted via the CPAN bug tracker
147
148 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Image-Math-Constrain>
149
150 For other issues, contact the maintainer
151
152 AUTHORS
153 Adam Kennedy <[email protected]>
154
155 Thank you to Phase N (<http://phase-n.com/>) for permitting the open
156 sourcing and release of this distribution.
157
158 COPYRIGHT
159 Copyright 2004 - 2008 Adam Kennedy.
160
161 This program is free software; you can redistribute it and/or modify it
162 under the same terms as Perl itself.
163
164 The full text of the license can be found in the LICENSE file included
165 with this module.
166