1: <?php
2: /**
3: ##DOC-SIGNATURE##
4:
5: This file is part of WideImage.
6:
7: WideImage is free software; you can redistribute it and/or modify
8: it under the terms of the GNU Lesser General Public License as published by
9: the Free Software Foundation; either version 2.1 of the License, or
10: (at your option) any later version.
11:
12: WideImage is distributed in the hope that it will be useful,
13: but WITHOUT ANY WARRANTY; without even the implied warranty of
14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: GNU Lesser General Public License for more details.
16:
17: You should have received a copy of the GNU Lesser General Public License
18: along with WideImage; if not, write to the Free Software
19: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20:
21: * @package Internal/Operations
22: **/
23:
24: namespace WideImage\Operation;
25:
26: use WideImage\WideImage;
27: use WideImage\Coordinate;
28:
29: /**
30: * ResizeCanvas operation class
31: *
32: * @package Internal/Operations
33: */
34: class ResizeCanvas
35: {
36: /**
37: * Returns an image with a resized canvas
38: *
39: * The image is filled with $color. Use $scale to determine, when to resize.
40: *
41: * @param \WideImage\Image $img
42: * @param smart_coordinate $width
43: * @param smart_coordinate $height
44: * @param smart_coordinate $left
45: * @param smart_coordinate $top
46: * @param int $color
47: * @param string $scale 'up', 'down', 'any'
48: * @param boolean $merge
49: * @return \WideImage\Image
50: */
51: public function execute($img, $width, $height, $left, $top, $color, $scale, $merge)
52: {
53: $new_width = Coordinate::fix($width, $img->getWidth());
54: $new_height = Coordinate::fix($height, $img->getHeight());
55:
56: if ($scale == 'down') {
57: $new_width = min($new_width, $img->getWidth());
58: $new_height = min($new_height, $img->getHeight());
59: } elseif ($scale == 'up') {
60: $new_width = max($new_width, $img->getWidth());
61: $new_height = max($new_height, $img->getHeight());
62: }
63:
64: $new = WideImage::createTrueColorImage($new_width, $new_height);
65:
66: if ($img->isTrueColor()) {
67: if ($color === null) {
68: $color = $new->allocateColorAlpha(0, 0, 0, 127);
69: }
70: } else {
71: imagepalettecopy($new->getHandle(), $img->getHandle());
72:
73: if ($img->isTransparent()) {
74: $new->copyTransparencyFrom($img);
75: $tc_rgb = $img->getTransparentColorRGB();
76: $t_color = $new->allocateColorAlpha($tc_rgb);
77: }
78:
79: if ($color === null) {
80: if ($img->isTransparent()) {
81: $color = $t_color;
82: } else {
83: $color = $new->allocateColorAlpha(255, 0, 127, 127);
84: }
85:
86: imagecolortransparent($new->getHandle(), $color);
87: }
88: }
89:
90: $new->fill(0, 0, $color);
91:
92: $x = Coordinate::fix($left, $new->getWidth(), $img->getWidth());
93: $y = Coordinate::fix($top, $new->getHeight(), $img->getHeight());
94:
95: // blending for truecolor images
96: if ($img->isTrueColor()) {
97: $new->alphaBlending($merge);
98: }
99:
100: // not-blending for palette images
101: if (!$merge && !$img->isTrueColor() && isset($t_color)) {
102: $new->getCanvas()->filledRectangle($x, $y, $x + $img->getWidth(), $y + $img->getHeight(), $t_color);
103: }
104:
105: $img->copyTo($new, $x, $y);
106:
107: return $new;
108: }
109: }
110: