1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: |
|
23: |
|
24: | namespace WideImage\Operation;
|
25: |
|
26: | use WideImage\WideImage;
|
27: | use WideImage\Coordinate;
|
28: |
|
29: | |
30: | |
31: | |
32: | |
33: |
|
34: | class ResizeCanvas
|
35: | {
|
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
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: |
|
96: | if ($img->isTrueColor()) {
|
97: | $new->alphaBlending($merge);
|
98: | }
|
99: |
|
100: |
|
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: | |