| 1 | package Image::ButtonMaker::TemplateCanvas; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use utf8; | ||
| 5 | use Image::Magick; | ||
| 6 | |||
| 7 | my @default_publ = ( | ||
| 8 | |||
| 9 | template => undef, | ||
| 10 | |||
| 11 | ### Cut parameters | ||
| 12 | cut_left => 4, | ||
| 13 | cut_right => 4, | ||
| 14 | cut_top => 3, | ||
| 15 | cut_bottom => 3, | ||
| 16 | |||
| 17 | ### Matte color | ||
| 18 | matte_color => '#000000', | ||
| 19 | ); | ||
| 20 | |||
| 21 | |||
| 22 | my @default_priv = ( | ||
| 23 | slices => undef, | ||
| 24 | ); | ||
| 25 | |||
| 26 | |||
| 27 | our $error; | ||
| 28 | our $errorstr; | ||
| 29 | |||
| 30 | |||
| 31 | #### Public('ish) Methods ############################# | ||
| 32 | |||
| 33 | sub new { | ||
| 34 | my $nobody = shift; | ||
| 35 | my @param = @_; | ||
| 36 | |||
| 37 | my $data = {@default_publ, @param, @default_priv}; | ||
| 38 | |||
| 39 | die "No template given" unless($data->{template}); | ||
| 40 | |||
| 41 | my $slices = slice_it_up($data->{template}, | ||
| 42 | $data->{cut_left}, | ||
| 43 | $data->{cut_right}, | ||
| 44 | $data->{cut_top}, | ||
| 45 | $data->{cut_bottom}, | ||
| 46 | $data->{matte_color}, | ||
| 47 | ); | ||
| 48 | |||
| 49 | return undef unless($slices); | ||
| 50 | |||
| 51 | $data->{slices} = $slices; | ||
| 52 | bless $data; | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | sub render($$) { | ||
| 57 | my $self = shift; | ||
| 58 | my($tot_w, $tot_h) = @_; | ||
| 59 | my $slc = $self->{slices}; | ||
| 60 | my $cur; | ||
| 61 | |||
| 62 | my ($left, $right, $top, $bottom) = ($self->{cut_left}, $self->{cut_right}, | ||
| 63 | $self->{cut_top}, $self->{cut_bottom}); | ||
| 64 | |||
| 65 | ## Calculate width & height for center tile | ||
| 66 | my $c_width = $tot_w - $left - $right; | ||
| 67 | my $c_height = $tot_h - $top - $bottom; | ||
| 68 | |||
| 69 | ## Create the button image | ||
| 70 | my $matte_color = $self->{matte_color}; | ||
| 71 | my $res = Image::Magick->new(size => $tot_w.'x'.$tot_h, matte => 1, mattecolor => $matte_color); | ||
| 72 | $res->Read("xc:$matte_color"); | ||
| 73 | |||
| 74 | ## Top left corner | ||
| 75 | $cur = $slc->[0]->Clone; | ||
| 76 | $cur->Resize(width=> $left, height => $top); | ||
| 77 | $res->Composite(image => $cur, compose => 'Over', geometry=>fromtop(0,0)); | ||
| 78 | |||
| 79 | ## Top middle | ||
| 80 | $cur = $slc->[1]->Clone; | ||
| 81 | $cur->Resize(width=> $c_width, height => $top); | ||
| 82 | $res->Composite(image => $cur, compose => 'Over', geometry=>fromtop($left,0)); | ||
| 83 | |||
| 84 | ## Top right corner | ||
| 85 | $cur = $slc->[2]->Clone; | ||
| 86 | $cur->Resize(width=> $right, height => $top); | ||
| 87 | $res->Composite(image => $cur, compose => 'Over', geometry=>fromtop($left+$c_width, 0)); | ||
| 88 | |||
| 89 | |||
| 90 | ## Middle left part | ||
| 91 | $cur = $slc->[3]->Clone; | ||
| 92 | $cur->Resize(width=> $left, height => $c_height, , blur => 0); | ||
| 93 | $res->Composite(image => $cur, compose => 'Over', geometry=>fromtop(0, $top)); | ||
| 94 | |||
| 95 | ## Middle middle part (Canvas) | ||
| 96 | $cur = $slc->[4]->Clone; | ||
| 97 | $cur->Set(matte => 'True', mattecolor=> $matte_color); | ||
| 98 | $cur->Resize(width=> $c_width, height => $c_height, blur => 1, filter => 'Point'); | ||
| 99 | $res->Composite(image => $cur, compose => 'Over', geometry=>fromtop($left, $top)); | ||
| 100 | |||
| 101 | ## Middle right part | ||
| 102 | $cur = $slc->[5]->Clone; | ||
| 103 | $cur->Resize(width=> $right, height => $c_height, , blur => 0); | ||
| 104 | $res->Composite(image => $cur, compose => 'Over', geometry=>fromtop($left+$c_width, $top)); | ||
| 105 | |||
| 106 | |||
| 107 | ## Bottom left part | ||
| 108 | $cur = $slc->[6]->Clone; | ||
| 109 | $cur->Resize(width=> $left, height => $bottom); | ||
| 110 | $res->Composite(image => $cur, compose => 'Over', geometry=>fromtop(0, $top+$c_height)); | ||
| 111 | |||
| 112 | ## Bottom middle part (Canvas) | ||
| 113 | $cur = $slc->[7]->Clone; | ||
| 114 | $cur->Resize(width=> $c_width, height => $bottom); | ||
| 115 | $res->Composite(image => $cur, compose => 'Over', geometry=>fromtop($left, $top+$c_height)); | ||
| 116 | |||
| 117 | ## Bottom right part | ||
| 118 | $cur = $slc->[8]->Clone; | ||
| 119 | $cur->Resize(width=> $right, height => $bottom); | ||
| 120 | $res->Composite(image => $cur, compose => 'Over', geometry=>fromtop($left+$c_width, $top+$c_height)); | ||
| 121 | |||
| 122 | return $res; | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | #### Privatish methods ################################ | ||
| 127 | ## | ||
| 128 | |||
| 129 | |||
| 130 | #### Class Method: slice_it_up($image, $left, $right, $top, $bottom) | ||
| 131 | ## return value: Image::Magick object containing nine slices | ||
| 132 | sub slice_it_up($$$$$$) { | ||
| 133 | my $image = shift; | ||
| 134 | |||
| 135 | my ($left, $right, $top, $bottom, $mattecolor) = @_; | ||
| 136 | |||
| 137 | my($img_h, $img_w) = $image->Get('rows', 'columns'); | ||
| 138 | |||
| 139 | my $middle_h = $img_h - $top - $bottom; | ||
| 140 | my $middle_w = $img_w - $left - $right; | ||
| 141 | |||
| 142 | return | ||
| 143 | set_error(1000, "Canvas Cut leaves no space for artwork") | ||
| 144 | if($middle_h < 1 || $middle_w < 1); | ||
| 145 | |||
| 146 | my $result = Image::Magick->new(mattecolor => $mattecolor, matte => 'True'); | ||
| 147 | my $cur; | ||
| 148 | |||
| 149 | #### Top left slice | ||
| 150 | $cur = $image->Clone; | ||
| 151 | $cur->Crop(width => $left, height => $top, x => 0, y => 0); | ||
| 152 | $result->[0] = $cur; | ||
| 153 | |||
| 154 | #### Top middle | ||
| 155 | $cur = $image->Clone; | ||
| 156 | $cur->Crop(width => $middle_w, height => $top, x => $left, y => 0); | ||
| 157 | $result->[1] = $cur; | ||
| 158 | |||
| 159 | #### Top right | ||
| 160 | $cur = $image->Clone; | ||
| 161 | $cur->Crop(width => $right, height => $top, x => $left+$middle_w, y => 0); | ||
| 162 | $result->[2] = $cur; | ||
| 163 | |||
| 164 | ### Middle left | ||
| 165 | $cur = $image->Clone; | ||
| 166 | $cur->Crop(width => $left, height => $middle_h, x => 0, y => $top); | ||
| 167 | $result->[3] = $cur; | ||
| 168 | |||
| 169 | ### Middle middle | ||
| 170 | $cur = $image->Clone; | ||
| 171 | $cur->Crop(width => $middle_w, height => $middle_h, x => $left, y => $top); | ||
| 172 | #$cur->Crop(geometry => "${middle_w}x${middle_h}+${left}+${top}"); | ||
| 173 | $result->[4] = $cur; | ||
| 174 | |||
| 175 | ### Middle right | ||
| 176 | $cur = $image->Clone; | ||
| 177 | $cur->Crop(width => $right, height => $middle_h, x => $left+$middle_w, y => $top); | ||
| 178 | $result->[5] = $cur; | ||
| 179 | |||
| 180 | ### Bottom left | ||
| 181 | $cur = $image->Clone; | ||
| 182 | $cur->Crop(width => $left, height => $bottom, x => 0, y => $top+$middle_h); | ||
| 183 | $result->[6] = $cur; | ||
| 184 | |||
| 185 | ### Bottom middle | ||
| 186 | $cur = $image->Clone; | ||
| 187 | $cur->Crop(width => $middle_w, height => $bottom, x => $left, y => $top+$middle_h); | ||
| 188 | $result->[7] = $cur; | ||
| 189 | |||
| 190 | ### Bottom right | ||
| 191 | $cur = $image->Clone; | ||
| 192 | $cur->Crop(width => $right, height => $bottom, x => $left+$middle_w, y => $top+$middle_h); | ||
| 193 | $result->[8] = $cur; | ||
| 194 | |||
| 195 | return $result; | ||
| 196 | |||
| 197 | } | ||
| 198 | |||
| 199 | #### Format a geometry string #### | ||
| 200 | sub fromtop($$) { | ||
| 201 | my $x = shift; | ||
| 202 | my $y = shift; | ||
| 203 | return '+'.$x.'+'.$y; | ||
| 204 | } | ||
| 205 | |||
| 206 | #### Error Handling | ||
| 207 | sub reset_error { | ||
| 208 | $error = 0; | ||
| 209 | $errorstr = ''; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub set_error { | ||
| 213 | $error = shift; | ||
| 214 | $errorstr = shift; | ||
| 215 | return @_; | ||
| 216 | } | ||
| 217 | |||
| 218 | 1; |