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_2';
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 METHODS
85
86 =head2 new
87
88 my $img = Image::Square->new('example.jpg');
89
90 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.
91
92 =head2 square
93
94 my $square_img = $img->square($size, $pos);
95
96 Creates a square image from the source image. The method takes two optional parameters:
97
98 =over 4
99
100 =item *
101
102 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)
103
104 =item *
105
106 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)
107
108 =back
109
110 The method returns a new L<GD::Image> object that represents the square image.
111
112 =head1 AUTHOR
113
114 Ian Boddison <ian at boddison.com>
115
116 =head1 BUGS
117
118 Please report any bugs or feature requests to C<bug-image-square at rt.cpan.org>, or through
119 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=image-square>. I will be notified, and then you'll
120 automatically be notified of progress on your bug as I make changes.
121
122 =head1 SUPPORT
123
124 You can find documentation for this module with the perldoc command.
125
126 perldoc Image::Square
127
128 You can also look for information at:
129
130 =over 4
131
132 =item * RT: CPAN's request tracker (report bugs here)
133
134 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Image-Square>
135
136 =item * Search CPAN
137
138 L<https://metacpan.org/release/Image::Square>
139
140 =back
141
142 =head1 COPYRIGHT AND LICENSE
143
144 This software is copyright (c) 2023 by Ian Boddison.
145
146 This is free software; you can redistribute it and/or modify it under
147 the same terms as the Perl 5 programming language system itself.
148
149 =cut
150
151
152 1;
153
154
155
↑ Summary