| 1 | package Imager::Filter::RoundedCorner; | ||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use Imager; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Imager::Fill; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | our $VERSION = '0.02'; | |||||||||
| 9 | |||||||||||
| 10 | 2 | Imager->register_filter( | |||||||||
| 11 | type => 'rounded_corner', | ||||||||||
| 12 | defaults => { | ||||||||||
| 13 | radius => '5', | ||||||||||
| 14 | bg => '#ffffff', | ||||||||||
| 15 | aa => 0, | ||||||||||
| 16 | border_width => 0, | ||||||||||
| 17 | border_color => '#000000' | ||||||||||
| 18 | }, | ||||||||||
| 19 | callseq => [qw/imager radius bg aa border_width border_color/], | ||||||||||
| 20 | callsub => \&round_corner, | ||||||||||
| 21 | ); | ||||||||||
| 22 | |||||||||||
| 23 | 0 | ▶ | sub round_corner { | ||||||||
|
Subroutine
round_corner: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 24 | 0 | my %args = @_; | |||||||||
| 25 | my ( $imager, $radius, $bg, $aa, $border_width, $border_color ) = | ||||||||||
| 26 | 0 | @args{qw/imager radius bg aa border_width border_color/}; | |||||||||
| 27 | |||||||||||
| 28 | 0 | my $transparent = Imager::Color->new( 0, 0, 0, 0 ); | |||||||||
| 29 | |||||||||||
| 30 | 0 | my $corner = Imager->new( | |||||||||
| 31 | xsize => $radius, | ||||||||||
| 32 | ysize => $radius, | ||||||||||
| 33 | channels => 4, | ||||||||||
| 34 | ); | ||||||||||
| 35 | 0 | $corner->box( filled => 1, color => Imager::Color->new( $bg ) ); | |||||||||
| 36 | |||||||||||
| 37 | 0 | ▶ | if ($border_width) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | $corner->circle( | |||||||||
| 39 | color => Imager::Color->new($border_color), | ||||||||||
| 40 | r => $radius, | ||||||||||
| 41 | x => $radius, | ||||||||||
| 42 | y => $radius, | ||||||||||
| 43 | aa => 0, | ||||||||||
| 44 | filled => 0, | ||||||||||
| 45 | ); | ||||||||||
| 46 | } | ||||||||||
| 47 | $corner->circle( | ||||||||||
| 48 | 0 | color => $transparent, | |||||||||
| 49 | r => $radius - $border_width, | ||||||||||
| 50 | x => $radius, | ||||||||||
| 51 | y => $radius, | ||||||||||
| 52 | aa => 0, | ||||||||||
| 53 | filled => 1 | ||||||||||
| 54 | ); | ||||||||||
| 55 | |||||||||||
| 56 | 0 | my $mask = Imager->new( | |||||||||
| 57 | xsize => $imager->getwidth, | ||||||||||
| 58 | ysize => $imager->getheight, | ||||||||||
| 59 | channels => 4, | ||||||||||
| 60 | ); | ||||||||||
| 61 | 0 | $mask->box( filled => 1, color => $transparent ); | |||||||||
| 62 | 0 | ▶ | if ($border_width) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | $mask->box( | ||||||||||
| 64 | filled => 0, | ||||||||||
| 65 | color => Imager::Color->new($border_color), | ||||||||||
| 66 | xmin => $_, | ||||||||||
| 67 | ymin => $_, | ||||||||||
| 68 | xmax => $imager->getwidth - 1 - $_, | ||||||||||
| 69 | ymax => $imager->getheight - 1 - $_, | ||||||||||
| 70 | ) | ||||||||||
| 71 | 0 | for 0 .. ($border_width-1); | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | # left top | ||||||||||
| 75 | 0 | $mask->paste( src => $corner ); | |||||||||
| 76 | |||||||||||
| 77 | # left bottom | ||||||||||
| 78 | 0 | $corner->flip( dir => 'v' ); | |||||||||
| 79 | 0 | $mask->paste( src => $corner, top => $imager->getheight - $radius ); | |||||||||
| 80 | |||||||||||
| 81 | # right bottom | ||||||||||
| 82 | 0 | $corner->flip( dir => 'h' ); | |||||||||
| 83 | 0 | $mask->paste( | |||||||||
| 84 | src => $corner, | ||||||||||
| 85 | top => $imager->getheight - $radius, | ||||||||||
| 86 | left => $imager->getwidth - $radius | ||||||||||
| 87 | ); | ||||||||||
| 88 | |||||||||||
| 89 | # right top | ||||||||||
| 90 | 0 | $corner->flip( dir => 'v' ); | |||||||||
| 91 | 0 | $mask->paste( src => $corner, left => $imager->getwidth - $radius ); | |||||||||
| 92 | |||||||||||
| 93 | 0 | $imager->box( | |||||||||
| 94 | fill => Imager::Fill->new( image => $mask, combine => 'normal' ) ); | ||||||||||
| 95 | |||||||||||
| 96 | 0 | ▶ | if ($aa) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 0 | my $copy = Imager->new( | |||||||||
| 98 | xsize => $imager->getwidth, | ||||||||||
| 99 | ysize => $imager->getheight, | ||||||||||
| 100 | channels => 4, | ||||||||||
| 101 | ); | ||||||||||
| 102 | 0 | $copy->box( fill => Imager::Fill->new( image => $imager, combine => 'normal' ) ); | |||||||||
| 103 | |||||||||||
| 104 | 0 | $copy->flood_fill( x => 0, y => 0, color => $transparent ); | |||||||||
| 105 | 0 | $copy->flood_fill( | |||||||||
| 106 | x => $copy->getwidth - 1, | ||||||||||
| 107 | y => 0, | ||||||||||
| 108 | color => $transparent | ||||||||||
| 109 | ); | ||||||||||
| 110 | 0 | $copy->flood_fill( | |||||||||
| 111 | x => 0, | ||||||||||
| 112 | y => $copy->getheight - 1, | ||||||||||
| 113 | color => $transparent | ||||||||||
| 114 | ); | ||||||||||
| 115 | 0 | $copy->flood_fill( | |||||||||
| 116 | x => $copy->getwidth - 1, | ||||||||||
| 117 | y => $copy->getheight - 1, | ||||||||||
| 118 | color => $transparent | ||||||||||
| 119 | ); | ||||||||||
| 120 | |||||||||||
| 121 | 0 | $imager->filter( type => 'conv', coef => [ 1, 2, 1 ] ); | |||||||||
| 122 | 0 | $imager->box( fill => Imager::Fill->new( image => $copy, combine => 'normal' ) ); | |||||||||
| 123 | } | ||||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | =head1 NAME | ||||||||||
| 127 | |||||||||||
| 128 | Imager::Filter::RoundedCorner - Make nifty images with Imager | ||||||||||
| 129 | |||||||||||
| 130 | =head1 SYNOPSIS | ||||||||||
| 131 | |||||||||||
| 132 | use Imager; | ||||||||||
| 133 | use Imager::Filter::RoundedCorner; | ||||||||||
| 134 | |||||||||||
| 135 | my $image = Imager->new; | ||||||||||
| 136 | $image->read( file => 'source.jpg' ); | ||||||||||
| 137 | |||||||||||
| 138 | $image->filter( | ||||||||||
| 139 | type => 'rounded_corner', | ||||||||||
| 140 | radius => 10, | ||||||||||
| 141 | bg => '#ffffff' | ||||||||||
| 142 | ); | ||||||||||
| 143 | |||||||||||
| 144 | $image->write( file => 'dest.jpg' ); | ||||||||||
| 145 | |||||||||||
| 146 | =head1 DESCRIPTION | ||||||||||
| 147 | |||||||||||
| 148 | This filter fill image's corner with 'bg' color as rounded corner. | ||||||||||
| 149 | |||||||||||
| 150 | Filter parameters are: | ||||||||||
| 151 | |||||||||||
| 152 | =over | ||||||||||
| 153 | |||||||||||
| 154 | =item radius | ||||||||||
| 155 | |||||||||||
| 156 | corner's radius | ||||||||||
| 157 | |||||||||||
| 158 | =item bg | ||||||||||
| 159 | |||||||||||
| 160 | background color | ||||||||||
| 161 | |||||||||||
| 162 | =item aa | ||||||||||
| 163 | |||||||||||
| 164 | antialias flag. 1 = on (default: 0) | ||||||||||
| 165 | |||||||||||
| 166 | =item border_width | ||||||||||
| 167 | |||||||||||
| 168 | border width (default: 0) | ||||||||||
| 169 | |||||||||||
| 170 | =item border_color | ||||||||||
| 171 | |||||||||||
| 172 | border color (default: #000000) | ||||||||||
| 173 | |||||||||||
| 174 | =back | ||||||||||
| 175 | |||||||||||
| 176 | =head1 SUBROUTINES | ||||||||||
| 177 | |||||||||||
| 178 | =head2 round_corner | ||||||||||
| 179 | |||||||||||
| 180 | =head1 AUTHOR | ||||||||||
| 181 | |||||||||||
| 182 | Daisuke Murase <typester@cpan.org> | ||||||||||
| 183 | |||||||||||
| 184 | =head1 COPYRIGHT | ||||||||||
| 185 | |||||||||||
| 186 | This program is free software; you can redistribute | ||||||||||
| 187 | it and/or modify it under the same terms as Perl itself. | ||||||||||
| 188 | |||||||||||
| 189 | The full text of the license can be found in the | ||||||||||
| 190 | LICENSE file included with this module. | ||||||||||
| 191 | |||||||||||
| 192 | =cut | ||||||||||
| 193 | |||||||||||
| 194 | 2 | 1; | |||||||||