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\Coordinate;
|
27: | use WideImage\PaletteImage;
|
28: | use WideImage\TrueColorImage;
|
29: | use WideImage\Exception\GDFunctionResultException;
|
30: | use WideImage\Operation\Exception\InvalidFitMethodException;
|
31: | use WideImage\Operation\Exception\InvalidResizeDimensionException;
|
32: |
|
33: | |
34: | |
35: | |
36: | |
37: |
|
38: | class Resize
|
39: | {
|
40: | |
41: | |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | |
48: |
|
49: | protected function prepareDimensions($img, $width, $height, $fit)
|
50: | {
|
51: | if ($width === null && $height === null) {
|
52: | return array('width' => $img->getWidth(), 'height' => $img->getHeight());
|
53: | }
|
54: |
|
55: | if ($width !== null) {
|
56: | $width = Coordinate::fix($width, $img->getWidth());
|
57: | $rx = $img->getWidth() / $width;
|
58: | } else {
|
59: | $rx = null;
|
60: | }
|
61: |
|
62: | if ($height !== null) {
|
63: | $height = Coordinate::fix($height, $img->getHeight());
|
64: | $ry = $img->getHeight() / $height;
|
65: | } else {
|
66: | $ry = null;
|
67: | }
|
68: |
|
69: | if ($rx === null && $ry !== null) {
|
70: | $rx = $ry;
|
71: | $width = round($img->getWidth() / $rx);
|
72: | }
|
73: |
|
74: | if ($ry === null && $rx !== null) {
|
75: | $ry = $rx;
|
76: | $height = round($img->getHeight() / $ry);
|
77: | }
|
78: |
|
79: | if ($width === 0 || $height === 0) {
|
80: | return array('width' => 0, 'height' => 0);
|
81: | }
|
82: |
|
83: | if ($fit == null) {
|
84: | $fit = 'inside';
|
85: | }
|
86: |
|
87: | $dim = array();
|
88: |
|
89: | if ($fit == 'fill') {
|
90: | $dim['width'] = $width;
|
91: | $dim['height'] = $height;
|
92: | } elseif ($fit == 'inside' || $fit == 'outside') {
|
93: | if ($fit == 'inside') {
|
94: | $ratio = ($rx > $ry) ? $rx : $ry;
|
95: | } else {
|
96: | $ratio = ($rx < $ry) ? $rx : $ry;
|
97: | }
|
98: |
|
99: | $dim['width'] = round($img->getWidth() / $ratio);
|
100: | $dim['height'] = round($img->getHeight() / $ratio);
|
101: | } else {
|
102: | throw new InvalidFitMethodException("{$fit} is not a valid resize-fit method.");
|
103: | }
|
104: |
|
105: | return $dim;
|
106: | }
|
107: |
|
108: | |
109: | |
110: | |
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: |
|
118: | public function execute($img, $width, $height, $fit, $scale)
|
119: | {
|
120: | $dim = $this->prepareDimensions($img, $width, $height, $fit);
|
121: |
|
122: | if (($scale === 'down' && ($dim['width'] >= $img->getWidth() && $dim['height'] >= $img->getHeight())) ||
|
123: | ($scale === 'up' && ($dim['width'] <= $img->getWidth() && $dim['height'] <= $img->getHeight()))) {
|
124: | $dim = array('width' => $img->getWidth(), 'height' => $img->getHeight());
|
125: | }
|
126: |
|
127: | if ($dim['width'] <= 0 || $dim['height'] <= 0) {
|
128: | throw new InvalidResizeDimensionException("Both dimensions must be larger than 0.");
|
129: | }
|
130: |
|
131: | if ($img->isTransparent() || $img instanceof PaletteImage) {
|
132: | $new = PaletteImage::create($dim['width'], $dim['height']);
|
133: | $new->copyTransparencyFrom($img);
|
134: |
|
135: | if (!imagecopyresized(
|
136: | $new->getHandle(),
|
137: | $img->getHandle(),
|
138: | 0, 0, 0, 0,
|
139: | $new->getWidth(),
|
140: | $new->getHeight(),
|
141: | $img->getWidth(),
|
142: | $img->getHeight())) {
|
143: | throw new GDFunctionResultException("imagecopyresized() returned false");
|
144: | }
|
145: | } else {
|
146: | $new = TrueColorImage::create($dim['width'], $dim['height']);
|
147: | $new->alphaBlending(false);
|
148: | $new->saveAlpha(true);
|
149: |
|
150: | if (!imagecopyresampled(
|
151: | $new->getHandle(),
|
152: | $img->getHandle(),
|
153: | 0, 0, 0, 0,
|
154: | $new->getWidth(),
|
155: | $new->getHeight(),
|
156: | $img->getWidth(),
|
157: | $img->getHeight())) {
|
158: | throw new GDFunctionResultException("imagecopyresampled() returned false");
|
159: | }
|
160: |
|
161: | $new->alphaBlending(true);
|
162: | }
|
163: |
|
164: | return $new;
|
165: | }
|
166: | }
|
167: | |