blib/lib/Image/Square.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Image::Square;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp ('croak');
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use GD;
Subroutine BEGIN: called CC=1 SCAR=2.6
7
8 our $VERSION = '0.01_3';
9
10 GD::Image->trueColor(1);
11
12 sub new {
13 my ($class, $image) = @_;
14
15 unless ( $class and defined $image) {
16 croak 'Image::Square->new(): image required';
17 }
18
19 my $gd;
20
21 if (ref $image eq 'GD::Image') {
22 $gd = $image;
23 } else {
24 unless ( -e $image ) {
25 croak "Image::Square->new(): file '$image' not found";
26 }
27 $gd = GD::Image->new($image) or die $@;
28 }
29
30 return bless {
31 'gd' => $gd,
32 }, $class;
33 }
34
35 sub square {
36 my $self = shift;
37 my ($size, $pos) = @_;
38
39 $pos = 0.5 unless defined $pos;
40
41 if ($pos < 0 or $pos > 1) {
42 croak "Image::Square->square(): pos out of range! 0-1 allowed";
43 }
44
45 my $width = $self->{'gd'}->width;
46 $width = $self->{'gd'}->height if $self->{'gd'}->height < $width;
47 my $source = $width;
48 $width = $size if $size and $size > 0;
49
50 my $square = GD::Image->new($width, $width);
51 if ($self->{'gd'}->width > $self->{'gd'}->height) {
52 $square->copyResampled($self->{'gd'}, 0, 0, ($self->{'gd'}->width - $source) * $pos, 0, $width, $width, $source, $source);
53 } else {
54 $square->copyResampled($self->{'gd'}, 0, 0, 0, ($self->{'gd'}->height - $source) * $pos, $width, $width, $source, $source);
55 }
56
57 return $square;
58 }
59
60 =head1 NAME
61
62 Image::Square - Crop and resize an image to create a square image
63
64 =head1 SYNOPSIS
65
66 use Image::Square;
67
68 # Create a new Image::Square object from an image file
69 my $img = Image::Square->new('example.jpg');
70
71 # Create a square image from the source image with default settings
72 my $square_img = $img->square();
73
74 # Create a square image of a specific size (e.g., 200x200 pixels)
75 my $custom_size_square = $img->square(200);
76
77 # Create a square image from a specific position (e.g., center)
78 my $center_square = $img->square(0, 0.5);
79
80 =head1 DESCRIPTION
81
82 The C<Image::Square> module provides a simple way to crop and resize an image to create a square image. This can be useful when you need to prepare images for applications that require square thumbnails or avatars.
83
84 =head1 ERROR HANDLING
85
86 C<Image::Square> will C<croak> if it detects a problem.
87
88 =head1 METHODS
89
90 =head2 new
91
92 my $img = Image::Square->new('example.jpg');
93
94 Creates a new C<Image::Square> object from an image file specified by the file path. The image file should be in a format supported by the L<GD> module. Alternatively, a L<GD::Image> object is accepted.
95
96 =head2 square
97
98 my $square_img = $img->square($size, $pos);
99
100 Creates a square image from the source image. The method takes two optional parameters:
101
102 =over 4
103
104 =item *
105
106 C<$size> - The size of the output square image in pixels. If not provided or set to 0, the size will be determined based on the smaller dimension of the source image. (default: 0)
107
108 =item *
109
110 C<$pos> - The position in the source image to create the square image from. This parameter accepts values between 0 and 1, where 0 represents the left or top edge, 0.5 represents the middle, and 1 represents the right or bottom edge. (default: 0.5)
111
112 =back
113
114 The method returns a new L<GD::Image> object that represents the square image.
115
116 =head1 AUTHOR
117
118 Ian Boddison <ian at boddison.com>
119
120 =head1 BUGS
121
122 Please report any bugs or feature requests to C<bug-image-square at rt.cpan.org>, or through
123 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=image-square>. I will be notified, and then you'll
124 automatically be notified of progress on your bug as I make changes.
125
126 =head1 SUPPORT
127
128 You can find documentation for this module with the perldoc command.
129
130 perldoc Image::Square
131
132 You can also look for information at:
133
134 =over 4
135
136 =item * RT: CPAN's request tracker (report bugs here)
137
138 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Image-Square>
139
140 =item * Search CPAN
141
142 L<https://metacpan.org/release/Image::Square>
143
144 =item * GitHub
145
146 L<https://github.com/IanBod/Image-Square>
147
148 =back
149
150 =head1 COPYRIGHT AND LICENSE
151
152 This software is copyright (c) 2023 by Ian Boddison.
153
154 This is free software; you can redistribute it and/or modify it under
155 the same terms as the Perl 5 programming language system itself.
156
157 =cut
158
159
160 1;
161
162
163
↑ Summary