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