1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: |
|
21: |
|
22: | namespace WideImage;
|
23: |
|
24: | use WideImage\Exception\InvalidImageDimensionException;
|
25: |
|
26: | |
27: | |
28: | |
29: | |
30: |
|
31: | class TrueColorImage extends Image
|
32: | {
|
33: | |
34: | |
35: | |
36: | |
37: |
|
38: | public function __construct($handle)
|
39: | {
|
40: | parent::__construct($handle);
|
41: |
|
42: | $this->alphaBlending(false);
|
43: | $this->saveAlpha(true);
|
44: | }
|
45: |
|
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: |
|
53: | public static function create($width, $height)
|
54: | {
|
55: | if ($width * $height <= 0 || $width < 0) {
|
56: | throw new InvalidImageDimensionException("Can't create an image with dimensions [$width, $height].");
|
57: | }
|
58: |
|
59: | return new TrueColorImage(imagecreatetruecolor($width, $height));
|
60: | }
|
61: |
|
62: | public function doCreate($width, $height)
|
63: | {
|
64: | return static::create($width, $height);
|
65: | }
|
66: |
|
67: | public function isTrueColor()
|
68: | {
|
69: | return true;
|
70: | }
|
71: |
|
72: | |
73: | |
74: | |
75: | |
76: | |
77: |
|
78: | public function alphaBlending($mode)
|
79: | {
|
80: | return imagealphablending($this->handle, $mode);
|
81: | }
|
82: |
|
83: | |
84: | |
85: | |
86: | |
87: | |
88: |
|
89: | public function saveAlpha($on)
|
90: | {
|
91: | return imagesavealpha($this->handle, $on);
|
92: | }
|
93: |
|
94: | |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | |
104: | |
105: | |
106: |
|
107: | public function allocateColorAlpha($R, $G = null, $B = null, $A = null)
|
108: | {
|
109: | if (is_array($R)) {
|
110: | return imageColorAllocateAlpha($this->handle, $R['red'], $R['green'], $R['blue'], $R['alpha']);
|
111: | }
|
112: |
|
113: | return imageColorAllocateAlpha($this->handle, $R, $G, $B, $A);
|
114: | }
|
115: |
|
116: | |
117: | |
118: |
|
119: | public function asPalette($nColors = 255, $dither = null, $matchPalette = true)
|
120: | {
|
121: | $nColors = intval($nColors);
|
122: |
|
123: | if ($nColors < 1) {
|
124: | $nColors = 1;
|
125: | } elseif ($nColors > 255) {
|
126: | $nColors = 255;
|
127: | }
|
128: |
|
129: | if ($dither === null) {
|
130: | $dither = $this->isTransparent();
|
131: | }
|
132: |
|
133: | $temp = $this->copy();
|
134: |
|
135: | imagetruecolortopalette($temp->handle, $dither, $nColors);
|
136: |
|
137: | if ($matchPalette == true && function_exists('imagecolormatch')) {
|
138: | imagecolormatch($this->handle, $temp->handle);
|
139: | }
|
140: |
|
141: |
|
142: |
|
143: | |
144: | |
145: | |
146: | |
147: | |
148: | |
149: | |
150: |
|
151: |
|
152: | $temp->releaseHandle();
|
153: |
|
154: | return new PaletteImage($temp->handle);
|
155: | }
|
156: |
|
157: | |
158: | |
159: | |
160: | |
161: | |
162: | |
163: | |
164: | |
165: | |
166: | |
167: | |
168: | |
169: |
|
170: | public function getClosestColorAlpha($R, $G = null, $B = null, $A = null)
|
171: | {
|
172: | if (is_array($R)) {
|
173: | return imagecolorclosestalpha($this->handle, $R['red'], $R['green'], $R['blue'], $R['alpha']);
|
174: | }
|
175: |
|
176: | return imagecolorclosestalpha($this->handle, $R, $G, $B, $A);
|
177: | }
|
178: |
|
179: | |
180: | |
181: | |
182: | |
183: | |
184: | |
185: | |
186: | |
187: | |
188: | |
189: | |
190: | |
191: |
|
192: | public function getExactColorAlpha($R, $G = null, $B = null, $A = null)
|
193: | {
|
194: | if (is_array($R)) {
|
195: | return imagecolorexactalpha($this->handle, $R['red'], $R['green'], $R['blue'], $R['alpha']);
|
196: | }
|
197: |
|
198: | return imagecolorexactalpha($this->handle, $R, $G, $B, $A);
|
199: | }
|
200: |
|
201: | |
202: | |
203: |
|
204: | public function getChannels()
|
205: | {
|
206: | $args = func_get_args();
|
207: |
|
208: | if (count($args) == 1 && is_array($args[0])) {
|
209: | $args = $args[0];
|
210: | }
|
211: |
|
212: | return OperationFactory::get('CopyChannelsTrueColor')->execute($this, $args);
|
213: | }
|
214: |
|
215: | |
216: | |
217: | |
218: |
|
219: | public function copyNoAlpha()
|
220: | {
|
221: | $prev = $this->saveAlpha(false);
|
222: | $result = WideImage::loadFromString($this->asString('png'));
|
223: | $this->saveAlpha($prev);
|
224: |
|
225: | return $result;
|
226: | }
|
227: |
|
228: | |
229: | |
230: | |
231: | |
232: |
|
233: | public function asTrueColor()
|
234: | {
|
235: | return $this->copy();
|
236: | }
|
237: |
|
238: | |
239: | |
240: | |
241: | |
242: |
|
243: | public function asProgressive()
|
244: | {
|
245: | $dest = $this->asTrueColor();
|
246: |
|
247: | imageinterlace($dest->getHandle(), true);
|
248: |
|
249: | return $dest;
|
250: | }
|
251: |
|
252: | |
253: | |
254: | |
255: | |
256: | |
257: | |
258: | |
259: | |
260: | |
261: | |
262: | |
263: | |
264: | |
265: | |
266: | |
267: | |
268: |
|
269: | public function resizeInsideRect($width, $height, $fit = 'inside', $scale = 'down', $alignLeft = 'center',
|
270: | $alignTop = 'center', $mergeOpacity = 100, $fillColor = null)
|
271: | {
|
272: |
|
273: | if ($fillColor) {
|
274: | if (is_numeric($fillColor)) {
|
275: | $fillColor = $this->getColorRGB($fillColor);
|
276: | }
|
277: | } else {
|
278: | $fillColor = $this->getColorRGB($this->getColorAt(0, 0));
|
279: | }
|
280: |
|
281: | $rect = \WideImage::createTrueColorImage($width, $height);
|
282: | $rect->fill(0, 0, $rect->allocateColor($fillColor));
|
283: |
|
284: | $img = $this;
|
285: |
|
286: | for ($i = 0; $i < 4; $i++) {
|
287: | $img = $img->resize($width, $height, $fit, $scale);
|
288: | }
|
289: |
|
290: | return $rect->merge($img, $alignLeft, $alignTop, $mergeOpacity);
|
291: | }
|
292: | }
|
293: |
|
294: | |