1: <?php
2: /**
3: ##DOC-SIGNATURE##
4:
5: This file is part of WideImage.
6:
7: WideImage is free software; you can redistribute it and/or modify
8: it under the terms of the GNU Lesser General Public License as published by
9: the Free Software Foundation; either version 2.1 of the License, or
10: (at your option) any later version.
11:
12: WideImage is distributed in the hope that it will be useful,
13: but WITHOUT ANY WARRANTY; without even the implied warranty of
14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: GNU Lesser General Public License for more details.
16:
17: You should have received a copy of the GNU Lesser General Public License
18: along with WideImage; if not, write to the Free Software
19: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20:
21: * @package WideImage
22: **/
23:
24: namespace WideImage;
25:
26: use WideImage\Exception\InvalidImageDimensionException;
27: use WideImage\Exception\GDFunctionResultException;
28:
29: /**
30: * @package WideImage
31: */
32: class PaletteImage extends Image
33: {
34: /**
35: * Create a palette image
36: *
37: * @param int $width
38: * @param int $height
39: * @return \WideImage\PaletteImage
40: */
41: public static function create($width, $height)
42: {
43: if ($width * $height <= 0 || $width < 0) {
44: throw new InvalidImageDimensionException("Can't create an image with dimensions [$width, $height].");
45: }
46:
47: return new PaletteImage(imagecreate($width, $height));
48: }
49:
50: public function doCreate($width, $height)
51: {
52: return static::create($width, $height);
53: }
54:
55: /**
56: * (non-PHPdoc)
57: * @see \WideImage\Image#isTrueColor()
58: */
59: public function isTrueColor()
60: {
61: return false;
62: }
63:
64: /**
65: * (non-PHPdoc)
66: * @see \WideImage\Image#asPalette($nColors, $dither, $matchPalette)
67: */
68: public function asPalette($nColors = 255, $dither = null, $matchPalette = true)
69: {
70: return $this->copy();
71: }
72:
73: /**
74: * Returns a copy of the image
75: *
76: * @param $trueColor True if the new image should be truecolor
77: * @return \WideImage\Image
78: */
79: protected function copyAsNew($trueColor = false)
80: {
81: $width = $this->getWidth();
82: $height = $this->getHeight();
83:
84: if ($trueColor) {
85: $new = TrueColorImage::create($width, $height);
86: } else {
87: $new = PaletteImage::create($width, $height);
88: }
89:
90: // copy transparency of source to target
91: if ($this->isTransparent()) {
92: $rgb = $this->getTransparentColorRGB();
93:
94: if (is_array($rgb)) {
95: $tci = $new->allocateColor($rgb['red'], $rgb['green'], $rgb['blue']);
96: $new->fill(0, 0, $tci);
97: $new->setTransparentColor($tci);
98: }
99: }
100:
101: imageCopy($new->getHandle(), $this->handle, 0, 0, 0, 0, $width, $height);
102:
103: return $new;
104: }
105:
106: /**
107: * (non-PHPdoc)
108: * @see \WideImage\Image#asTrueColor()
109: */
110: public function asTrueColor()
111: {
112: $width = $this->getWidth();
113: $height = $this->getHeight();
114: $new = WideImage::createTrueColorImage($width, $height);
115:
116: if ($this->isTransparent()) {
117: $new->copyTransparencyFrom($this);
118: }
119:
120: if (!imageCopy($new->getHandle(), $this->handle, 0, 0, 0, 0, $width, $height)) {
121: throw new GDFunctionResultException("imagecopy() returned false");
122: }
123:
124: return $new;
125: }
126:
127: /**
128: * (non-PHPdoc)
129: * @see \WideImage\Image#getChannels()
130: */
131: public function getChannels()
132: {
133: $args = func_get_args();
134:
135: if (count($args) == 1 && is_array($args[0])) {
136: $args = $args[0];
137: }
138:
139: return OperationFactory::get('CopyChannelsPalette')->execute($this, $args);
140: }
141:
142: /**
143: * (non-PHPdoc)
144: * @see \WideImage\Image#copyNoAlpha()
145: */
146: public function copyNoAlpha()
147: {
148: return WideImage::loadFromString($this->asString('png'));
149: }
150: }
151: