1 <?php
2 /**
3 * @author Gasper Kozak
4 * @copyright 2007-2011
5
6 This file is part of WideImage.
7
8 WideImage is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 WideImage is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with WideImage; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 **/
22
23 /**
24 * A class for truecolor image objects
25 *
26 * @package WideImage
27 */
28 class WideImage_TrueColorImage extends WideImage_Image
29 {
30 /**
31 * Creates the object
32 *
33 * @param resource $handle
34 */
35 function __construct($handle)
36 {
37 parent::__construct($handle);
38 $this->alphaBlending(false);
39 $this->saveAlpha(true);
40 }
41
42 /**
43 * Factory method that creates a true-color image object
44 *
45 * @param int $width
46 * @param int $height
47 * @return WideImage_TrueColorImage
48 */
49 static function create($width, $height)
50 {
51 if ($width * $height <= 0 || $width < 0)
52 throw new WideImage_InvalidImageDimensionException("Can't create an image with dimensions [$width, $height].");
53
54 return new WideImage_TrueColorImage(imagecreatetruecolor($width, $height));
55 }
56
57 function doCreate($width, $height)
58 {
59 return self::create($width, $height);
60 }
61
62 function isTrueColor()
63 {
64 return true;
65 }
66
67 /**
68 * Sets alpha blending mode via imagealphablending()
69 *
70 * @param bool $mode
71 * @return bool
72 */
73 function alphaBlending($mode)
74 {
75 return imagealphablending($this->handle, $mode);
76 }
77
78 /**
79 * Toggle if alpha channel should be saved with the image via imagesavealpha()
80 *
81 * @param bool $on
82 * @return bool
83 */
84 function saveAlpha($on)
85 {
86 return imagesavealpha($this->handle, $on);
87 }
88
89 /**
90 * Allocates a color and returns its index
91 *
92 * This method accepts either each component as an integer value,
93 * or an associative array that holds the color's components in keys
94 * 'red', 'green', 'blue', 'alpha'.
95 *
96 * @param mixed $R
97 * @param int $G
98 * @param int $B
99 * @param int $A
100 * @return int
101 */
102 function allocateColorAlpha($R, $G = null, $B = null, $A = null)
103 {
104 if (is_array($R))
105 return imageColorAllocateAlpha($this->handle, $R['red'], $R['green'], $R['blue'], $R['alpha']);
106 else
107 return imageColorAllocateAlpha($this->handle, $R, $G, $B, $A);
108 }
109
110 /**
111 * @see WideImage_Image#asPalette($nColors, $dither, $matchPalette)
112 */
113 function asPalette($nColors = 255, $dither = null, $matchPalette = true)
114 {
115 $nColors = intval($nColors);
116 if ($nColors < 1)
117 $nColors = 1;
118 elseif ($nColors > 255)
119 $nColors = 255;
120
121 if ($dither === null)
122 $dither = $this->isTransparent();
123
124 $temp = $this->copy();
125 imagetruecolortopalette($temp->handle, $dither, $nColors);
126 if ($matchPalette == true && function_exists('imagecolormatch'))
127 imagecolormatch($this->handle, $temp->handle);
128
129 // The code below isn't working properly; it corrupts transparency on some palette->tc->palette conversions.
130 // Why is this code here?
131 /*
132 if ($this->isTransparent())
133 {
134 $trgb = $this->getTransparentColorRGB();
135 $tci = $temp->getClosestColor($trgb);
136 $temp->setTransparentColor($tci);
137 }
138 /**/
139
140 $temp->releaseHandle();
141 return new WideImage_PaletteImage($temp->handle);
142 }
143
144 /**
145 * Returns the index of the color that best match the given color components
146 *
147 * This method accepts either each component as an integer value,
148 * or an associative array that holds the color's components in keys
149 * 'red', 'green', 'blue', 'alpha'.
150 *
151 * @param mixed $R Red component value or an associative array
152 * @param int $G Green component
153 * @param int $B Blue component
154 * @param int $A Alpha component
155 * @return int The color index
156 */
157 function getClosestColorAlpha($R, $G = null, $B = null, $A = null)
158 {
159 if (is_array($R))
160 return imagecolorclosestalpha($this->handle, $R['red'], $R['green'], $R['blue'], $R['alpha']);
161 else
162 return imagecolorclosestalpha($this->handle, $R, $G, $B, $A);
163 }
164
165 /**
166 * Returns the index of the color that exactly match the given color components
167 *
168 * This method accepts either each component as an integer value,
169 * or an associative array that holds the color's components in keys
170 * 'red', 'green', 'blue', 'alpha'.
171 *
172 * @param mixed $R Red component value or an associative array
173 * @param int $G Green component
174 * @param int $B Blue component
175 * @param int $A Alpha component
176 * @return int The color index
177 */
178 function getExactColorAlpha($R, $G = null, $B = null, $A = null)
179 {
180 if (is_array($R))
181 return imagecolorexactalpha($this->handle, $R['red'], $R['green'], $R['blue'], $R['alpha']);
182 else
183 return imagecolorexactalpha($this->handle, $R, $G, $B, $A);
184 }
185
186 /**
187 * @see WideImage_Image#getChannels()
188 */
189 function getChannels()
190 {
191 $args = func_get_args();
192 if (count($args) == 1 && is_array($args[0]))
193 $args = $args[0];
194 return WideImage_OperationFactory::get('CopyChannelsTrueColor')->execute($this, $args);
195 }
196
197 /**
198 * (non-PHPdoc)
199 * @see WideImage_Image#copyNoAlpha()
200 */
201 function copyNoAlpha()
202 {
203 $prev = $this->saveAlpha(false);
204 $result = WideImage_Image::loadFromString($this->asString('png'));
205 $this->saveAlpha($prev);
206 //$result->releaseHandle();
207 return $result;
208 }
209
210 /**
211 * (non-PHPdoc)
212 * @see WideImage_Image#asTrueColor()
213 */
214 function asTrueColor()
215 {
216 return $this->copy();
217 }
218 }
219