## Apache Zeta Components Manual :: File Source for gd_base.php

## Source for file gd_base.php

Documentation is available at gd_base.php

1. <?php
2. /**
3. * This file contains the ezcImageGdBaseHandler class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @package ImageConversion
23. * @version //autogentag//
24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
25. * @filesource
26. */
28. /**
29. * ezcImageHandler implementation for the GD2 extension of PHP.
30. * This class only implements the base funtionality of handling GD images. If
31. * you want to manipulate images using ext/GD in your application, you should
32. * use the {@link ezcImageGdHandler}.
33. *
34. * You can use this base class to implement your own filter set on basis of
35. * ext/GD, but you can also use {@link ezcImageGdHandler} for this and profit
36. * from its already implemented filters.
37. *
38. * @see ezcImageConverter
39. * @see ezcImageHandler
40. *
41. * @package ImageConversion
42. * @version //autogentag//
43. */
44. class ezcImageGdBaseHandler extends ezcImageMethodcallHandler
45. {
46. /**
47. * Create a new image handler.
48. * Creates an image handler. This should never be done directly,
49. * but only through the manager for configuration reasons. One can
50. * get a direct reference through manager afterwards.
51. *
52. * @param ezcImageHandlerSettings $settings
53. * Settings for the handler.
54. *
55. * @throws ezcImageHandlerNotAvailableException
56. * If the precondition for the handler is not fulfilled.
57. */
58. public function __construct ( ezcImageHandlerSettings $settings )
59. {
60. if ( ! ezcBaseFeatures :: hasExtensionSupport ( 'gd' ) )
61. {
62. throw new ezcImageHandlerNotAvailableException ( "ezcImageGdHandler", "PHP extension 'GD' not available." );
63. }
64. $this->determineTypes();
65. parent::__construct( $settings );
66. }
68. /**
69. * Load an image file.
70. * Loads an image file and returns a reference to it.
71. *
72. * @param string $file File to load.
73. * @param string $mime The MIME type of the file.
74. *
75. * @return string Reference to the file in this handler.
76. *
77. * @see ezcImageAnalyzer
78. *
79. * @throws ezcBaseFileNotFoundException
80. * If the given file does not exist.
81. * @throws ezcImageMimeTypeUnsupportedException
82. * If the type of the given file is not recognized
83. * @throws ezcImageFileNotProcessableException
84. * If the given file is not processable using this handler.
85. * @throws ezcImageFileNameInvalidException
86. * If an invalid character (", ', $) is found in the file name.
87. */
88. public function load ( $file, $mime = null )
89. {
90. $this->checkFileName( $file );
91. $ref = $this->loadCommon( $file, isset( $mime ) ? $mime : null );
92. $loadFunction = $this->getLoadFunction( $this->getReferenceData( $ref, 'mime' ) );
93. if ( ! ezcBaseFeatures :: hasFunction ( $loadFunction ) || ( $handle = @$loadFunction( $file ) ) === false )
94. {
95. throw new ezcImageFileNotProcessableException ( $file, "File could not be opened using $loadFunction." );
96. }
97. $this->setReferenceData( $ref, $handle, 'resource' );
98. return $ref;
99. }
101. /**
102. * Save an image file.
103. * Saves a given open file. Can optionally save to a new file name.
104. *
105. * @see ezcImageHandler::load()
106. *
107. * @param string $image File reference created through load().
108. * @param string $newFile Filename to save the image to.
109. * @param string $mime New MIME type, if differs from initial one.
110. * @param ezcImageSaveOptions $options Save options.
111. * @return void
112. *
113. * @throws ezcImageFileNotProcessableException
114. * If the given file could not be saved with the given MIME type.
115. * @throws ezcBaseFilePermissionException
116. * If the desired file exists and is not writeable.
117. * @throws ezcImageMimeTypeUnsupportedException
118. * If the desired MIME type is not recognized
119. * @throws ezcImageFileNameInvalidException
120. * If an invalid character (", ', $) is found in the file name.
121. */
122. public function save ( $image, $newFile = null, $mime = null, ezcImageSaveOptions $options = null )
123. {
124. $options = ( $options === null ) ? new ezcImageSaveOptions () : $options;
126. if ( $newFile !== null )
127. {
128. $this->checkFileName( $newFile );
129. }
131. // Check is transparency must be converted
132. if ( $this->needsTransparencyConversion( $this->getReferenceData( $image, 'mime' ), $mime ) && $options->transparencyReplacementColor !== null )
133. {
134. $this-> replaceTransparency ( $image, $options->transparencyReplacementColor );
135. }
137. $this->saveCommon( $image, isset( $newFile ) ? $newFile : null, isset( $mime ) ? $mime : null );
138. $saveFunction = $this->getSaveFunction( $this->getReferenceData( $image, 'mime' ) );
140. $saveParams = array(
141. $this->getReferenceData( $image, 'resource' ),
142. $this->getReferenceData( $image, 'file' ),
143. );
144. switch ( $saveFunction )
145. {
146. case "imagejpeg":
147. if ( $options->quality !== null )
148. {
149. $saveParams[] = $options->quality;
150. }
151. break;
152. case "imagepng":
153. if ( $options->compression !== null )
154. {
155. $saveParams[] = $options->compression;
156. }
157. break;
158. }
160. if ( ! ezcBaseFeatures :: hasFunction ( $saveFunction ) ||
161. call_user_func_array ( $saveFunction, $saveParams ) === false )
162. {
163. throw new ezcImageFileNotProcessableException ( $file, "Unable to save file '{$file}' of type '{$mime}'." );
164. }
165. }
167. /**
168. * Replaces a transparent background with the given color.
169. *
170. * This method is used to replace the transparent background of an image
171. * with an opaque color when converting from a transparency supporting MIME
172. * type (e.g. image/png) to a MIME type that does not support transparency.
173. *
174. * The color
175. *
176. * @param mixed $image
177. * @param mixed $color
178. * @return void
179. */
180. protected function replaceTransparency ( $image, array $color )
181. {
182. $oldResource = $this->getReferenceData( $image, 'resource' );
183. $width = imagesx( $oldResource );
184. $height = imagesy( $oldResource );
185. if ( imageistruecolor( $oldResource ) )
186. {
187. $newResource = imagecreatetruecolor( $width, $height );
188. }
189. else
190. {
191. $newResource = imagecreate ( $width, $height );
192. }
194. $bgColor = imagecolorallocate ( $newResource, $color[0], $color[1], $color[2] );
195. imagefill ( $newResource, 0, 0, $bgColor );
197. // $res = imagecopyresampled(
198. $res = imagecopyresampled (
199. $newResource, // destination resource
200. $oldResource, // source resource
201. 0, // destination x coord
202. 0, // destination y coord
203. 0, // source x coord
204. 0, // source y coord
205. $width, // destination width
206. $height, // destination height
207. $width, // source witdh
208. $height // source height
209. );
210. if ( $res === false )
211. {
212. throw new ezcImageFilterFailedException ( 'crop', 'Resampling of image failed.' );
213. }
214. imagedestroy ( $oldResource );
215. $this->setReferenceData( $image, $newResource, 'resource' );
216. }
218. /**
219. * Close the file referenced by $image.
220. * Frees the image reference. You should call close() before.
221. *
222. * @see ezcImageHandler::load()
223. * @see ezcImageHandler::save()
224. *
225. * @param string $image The image reference.
226. * @return void
227. */
228. public function close ( $image )
229. {
230. $res = $this->getReferenceData( $image, 'resource' );
231. if ( is_resource ( $res ) )
232. {
233. imagedestroy ( $res );
234. }
235. $this->closeCommon( $image );
236. }
238. /**
239. * Determine, the image types the available GD extension is able to process.
240. *
241. * @return void
242. */
243. private function determineTypes()
244. {
245. $possibleTypes = array(
246. IMG_GIF => 'image/gif',
247. IMG_JPG => 'image/jpeg',
248. IMG_PNG => 'image/png',
249. IMG_WBMP => 'image/wbmp',
250. IMG_XPM => 'image/xpm',
251. );
252. $imageTypes = imagetypes ();
253. foreach ( $possibleTypes as $bit => $mime )
254. {
255. if ( $imageTypes & $bit )
256. {
257. $this->inputTypes[] = $mime;
258. $this->outputTypes[] = $mime;
259. }
260. }
261. }
263. /**
264. * Generate imagecreatefrom* function out of a MIME type.
265. *
266. * @param string $mime MIME type in format "image/<type>".
267. * @return string imagecreatefrom* function name.
268. *
269. * @throws ezcImageMimeTypeUnsupportedException
270. * If the load function for a given MIME type does not exist.
271. */
272. private function getLoadFunction( $mime )
273. {
274. if ( !$this->allowsInput( $mime ) )
275. {
276. throw new ezcImageMimeTypeUnsupportedException ( $mime, 'input' );
277. }
278. return 'imagecreatefrom' . substr ( strstr ( $mime, '/' ), 1 );
279. }
281. /**
282. * Generate image* function out of a MIME type.
283. *
284. * @param string $mime MIME type in format "image/<type>".
285. * @return string image* function name for saving.
286. *
287. * @throws ezcImageImagemagickHandler
288. * If the save function for a given MIME type does not exist.
289. */
290. private function getSaveFunction( $mime )
291. {
292. if ( !$this->allowsOutput( $mime ) )
293. {
294. throw new ezcImageMimeTypeUnsupportedException ( $mime, 'output' );
295. }
296. return 'image' . substr ( strstr ( $mime, '/' ), 1 );
297. }
299. /**
300. * Creates default settings for the handler and returns it.
301. * The reference name will be set to 'GD'.
302. *
303. * @return ezcImageHandlerSettings
304. */
305. static public function defaultSettings ()
306. {
307. return new ezcImageHandlerSettings ( 'GD', 'ezcImageGdHandler' );
308. }
311. }
313. ?>

Documentation generated by phpDocumentor 1.4.3