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