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

## Source for file imagemagick.php

Documentation is available at imagemagick.php

1. <?php
2. /**
3. * This file contains the ezcImageImagemagickHandler 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 ImageMagick.
30. *
31. * @see ezcImageConverter
32. * @see ezcImageHandler
33. *
34. * @package ImageConversion
35. * @version //autogentag//
36. */
37. class ezcImageImagemagickHandler extends ezcImageImagemagickBaseHandler
38. implements ezcImageGeometryFilters ,
39. ezcImageColorspaceFilters ,
40. ezcImageEffectFilters ,
41. ezcImageWatermarkFilters ,
42. ezcImageThumbnailFilters
43. {
45. /**
46. * Scale filter.
47. * General scale filter. Scales the image to fit into a given box size,
48. * determined by a given width and height value, measured in pixel. This
49. * method maintains the aspect ratio of the given image. Depending on the
50. * given direction value, this method performs the following scales:
51. *
52. * - ezcImageGeometryFilters::SCALE_BOTH:
53. * The image will be scaled to fit exactly into the given box
54. * dimensions, no matter if it was smaller or larger as the box
55. * before.
56. * - ezcImageGeometryFilters::SCALE_DOWN:
57. * The image will be scaled to fit exactly into the given box
58. * only if it was larger than the given box dimensions before. If it
59. * is smaller, the image will not be scaled at all.
60. * - ezcImageGeometryFilters::SCALE_UP:
61. * The image will be scaled to fit exactly into the given box
62. * only if it was smaller than the given box dimensions before. If it
63. * is larger, the image will not be scaled at all. ATTENTION:
64. * In this case, the image does not necessarily fit into the given box
65. * afterwards.
66. *
67. * ATTENTION: Using this filter method directly results in the filter being
68. * applied to the image which is internally marked as "active" (most
69. * commonly this is the last recently loaded one). It is highly recommended
70. * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
71. * method, which enables you to specify the image a filter is applied to.
72. *
73. * @param int $width Scale to width
74. * @param int $height Scale to height
75. * @param int $direction Scale to which direction.
76. * @return void
77. *
78. * @throws ezcImageInvalidReferenceException
79. * No loaded file could be found or an error destroyed a loaded reference.
80. * @throws ezcBaseValueException
81. * If a submitted parameter was out of range or type.
82. */
83. public function scale ( $width, $height, $direction = ezcImageGeometryFilters::SCALE_BOTH )
84. {
85. if ( ! is_int ( $width ) || $width < 1 )
86. {
87. throw new ezcBaseValueException ( 'width', $width, 'int > 0' );
88. }
89. if ( ! is_int ( $height ) || $height < 1 )
90. {
91. throw new ezcBaseValueException ( 'height', $height, 'int > 0' );
92. }
94. $dirMod = $this-> getDirectionModifier ( $direction );
95. $this-> addFilterOption (
96. $this->getActiveReference(),
97. '-resize',
98. $width.$dirMod.'x'.$height.$dirMod
99. );
100. }
102. /**
103. * Scale after width filter.
104. * Scales the image to a give width, measured in pixel. Scales the height
105. * automatically while keeping the ratio. The direction dictates, if an
106. * image may only be scaled {@link self::SCALE_UP}, {@link self::SCALE_DOWN}
107. * or if the scale may work in {@link self::SCALE_BOTH} directions.
108. *
109. * ATTENTION: Using this filter method directly results in the filter being
110. * applied to the image which is internally marked as "active" (most
111. * commonly this is the last recently loaded one). It is highly recommended
112. * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
113. * method, which enables you to specify the image a filter is applied to.
114. *
115. * @param int $width Scale to width
116. * @param int $direction Scale to which direction
117. * @return void
118. *
119. * @throws ezcImageInvalidReferenceException
120. * No loaded file could be found or an error destroyed a loaded reference
121. * @throws ezcBaseValueException
122. * If a submitted parameter was out of range or type.
123. */
124. public function scaleWidth ( $width, $direction )
125. {
126. if ( ! is_int ( $width ) || $width < 1 )
127. {
128. throw new ezcBaseValueException ( 'width', $width, 'int > 0' );
129. }
131. $dirMod = $this-> getDirectionModifier ( $direction );
132. $this-> addFilterOption (
133. $this->getActiveReference(),
134. '-resize ',
135. $width.$dirMod
136. );
137. }
139. /**
140. * Scale after height filter.
141. * Scales the image to a give height, measured in pixel. Scales the width
142. * automatically while keeping the ratio. The direction dictates, if an
143. * image may only be scaled {@link self::SCALE_UP}, {@link self::SCALE_DOWN}
144. * or if the scale may work in {@link self::SCALE_BOTH} directions.
145. *
146. * ATTENTION: Using this filter method directly results in the filter being
147. * applied to the image which is internally marked as "active" (most
148. * commonly this is the last recently loaded one). It is highly recommended
149. * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
150. * method, which enables you to specify the image a filter is applied to.
151. *
152. * @param int $height Scale to height
153. * @param int $direction Scale to which direction
154. * @return void
155. *
156. * @throws ezcImageInvalidReferenceException
157. * No loaded file could be found or an error destroyed a loaded reference
158. * @throws ezcBaseValueException
159. * If a submitted parameter was out of range or type.
160. */
161. public function scaleHeight ( $height, $direction )
162. {
163. if ( ! is_int ( $height ) || $height < 1 )
164. {
165. throw new ezcBaseValueException ( 'height', $height, 'int > 0' );
166. }
167. $dirMod = $this-> getDirectionModifier ( $direction );
168. $this-> addFilterOption (
169. $this->getActiveReference(),
170. '-resize ',
171. 'x'.$height.$dirMod
172. );
173. }
175. /**
176. * Scale percent measures filter.
177. * Scale an image to a given percentage value size.
178. *
179. * ATTENTION: Using this filter method directly results in the filter being
180. * applied to the image which is internally marked as "active" (most
181. * commonly this is the last recently loaded one). It is highly recommended
182. * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
183. * method, which enables you to specify the image a filter is applied to.
184. *
185. * @param int $width Scale to width
186. * @param int $height Scale to height
187. * @return void
188. *
189. * @throws ezcImageInvalidReferenceException
190. * No loaded file could be found or an error destroyed a loaded reference
191. * @throws ezcBaseValueException
192. * If a submitted parameter was out of range or type.
193. */
194. public function scalePercent ( $width, $height )
195. {
196. if ( ! is_int ( $height ) || $height < 1 )
197. {
198. throw new ezcBaseValueException ( 'height', $height, 'int > 0' );
199. }
200. if ( ! is_int ( $width ) || $width < 1 )
201. {
202. throw new ezcBaseValueException ( 'width', $width, 'int > 0' );
203. }
204. $this-> addFilterOption (
205. $this->getActiveReference(),
206. '-resize',
207. $width.'%x'.$height.'%'
208. );
209. }
211. /**
212. * Scale exact filter.
213. * Scale the image to a fixed given pixel size, no matter to which
214. * direction.
215. *
216. * ATTENTION: Using this filter method directly results in the filter being
217. * applied to the image which is internally marked as "active" (most
218. * commonly this is the last recently loaded one). It is highly recommended
219. * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
220. * method, which enables you to specify the image a filter is applied to.
221. *
222. * @param int $width Scale to width
223. * @param int $height Scale to height
224. * @return void
225. *
226. * @throws ezcImageInvalidReferenceException
227. * No loaded file could be found or an error destroyed a loaded reference.
228. * @throws ezcBaseValueException
229. * If a submitted parameter was out of range or type.
230. */
231. public function scaleExact ( $width, $height )
232. {
233. if ( ! is_int ( $height ) || $height < 1 )
234. {
235. throw new ezcBaseValueException ( 'height', $height, 'int > 0' );
236. }
237. if ( ! is_int ( $width ) || $width < 1 )
238. {
239. throw new ezcBaseValueException ( 'width', $width, 'int > 0' );
240. }
241. $this-> addFilterOption (
242. $this->getActiveReference(),
243. '-resize',
244. $width.'!x'.$height.'!'
245. );
246. }
248. /**
249. * Crop filter.
250. * Crop an image to a given size. This takes cartesian coordinates of a
251. * rect area to crop from the image. The cropped area will replace the old
252. * image resource (not the input image immediately, if you use the
253. * {@link ezcImageConverter}). Coordinates are given as integer values and
254. * are measured from the top left corner.
255. *
256. * ATTENTION: Using this filter method directly results in the filter being
257. * applied to the image which is internally marked as "active" (most
258. * commonly this is the last recently loaded one). It is highly recommended
259. * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
260. * method, which enables you to specify the image a filter is applied to.
261. *
262. * @param int $x X offset of the cropping area.
263. * @param int $y Y offset of the cropping area.
264. * @param int $width Width of cropping area.
265. * @param int $height Height of cropping area.
266. * @return void
267. *
268. * @throws ezcImageInvalidReferenceException
269. * No loaded file could be found or an error destroyed a loaded reference.
270. * @throws ezcBaseValueException
271. * If a submitted parameter was out of range or type.
272. */
273. public function crop ( $x, $y, $width, $height )
274. {
275. if ( ! is_int ( $x ) )
276. {
277. throw new ezcBaseValueException ( 'x', $x, 'int' );
278. }
279. if ( ! is_int ( $y ) )
280. {
281. throw new ezcBaseValueException ( 'y', $y, 'int' );
282. }
283. if ( ! is_int ( $height ) )
284. {
285. throw new ezcBaseValueException ( 'height', $height, 'int' );
286. }
287. if ( ! is_int ( $width ) )
288. {
289. throw new ezcBaseValueException ( 'width', $width, 'int' );
290. }
292. $data = getimagesize ( $this->getActiveResource() );
293. $x = ( $x >= 0 ) ? $x : $data[0] + $x;
294. $y = ( $y >= 0 ) ? $y : $data[1] + $y;
296. $xStart = ( $xStart = min ( $x, $x + $width ) ) >= 0 ? '+'.$xStart : $xStart;
297. $yStart = ( $yStart = min ( $y, $y + $height ) ) >= 0 ? '+'.$yStart : $yStart;
298. $this-> addFilterOption (
299. $this->getActiveReference(),
300. '-crop ',
301. abs ( $width ).'x'.abs ( $height ).$xStart.$yStart.'!'
302. );
303. }
305. /**
306. * Colorspace filter.
307. * Transform the color space of the picture. The following color space are
308. * supported:
309. *
310. * - {@link self::COLORSPACE_GREY} - 255 grey colors
311. * - {@link self::COLORSPACE_SEPIA} - Sepia colors
312. * - {@link self::COLORSPACE_MONOCHROME} - 2 colors black and white
313. *
314. * ATTENTION: Using this filter method directly results in the filter being
315. * applied to the image which is internally marked as "active" (most
316. * commonly this is the last recently loaded one). It is highly recommended
317. * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
318. * method, which enables you to specify the image a filter is applied to.
319. *
320. * @param int $space Colorspace, one of self::COLORSPACE_* constants.
321. * @return void
322. *
323. * @throws ezcImageInvalidReferenceException
324. * No loaded file could be found or an error destroyed a loaded reference
325. * @throws ezcBaseValueException
326. * If the parameter submitted as the colorspace was not within the
327. * self::COLORSPACE_* constants.
328. */
329. public function colorspace ( $space )
330. {
331. switch ( $space )
332. {
333. case self::COLORSPACE_GREY:
334. $this-> addFilterOption (
335. $this->getActiveReference(),
336. '-colorspace',
337. 'GRAY'
338. );
339. $this-> addFilterOption (
340. $this->getActiveReference(),
341. '-colors',
342. '255'
343. );
344. break;
345. case self::COLORSPACE_MONOCHROME:
346. $this-> addFilterOption (
347. $this->getActiveReference(),
348. '-monochrome'
349. );
350. break;
351. case self::COLORSPACE_SEPIA:
352. $this-> addFilterOption (
353. $this->getActiveReference(),
354. '-sepia-tone',
355. '80%'
356. );
357. break;
358. return;
359. default:
360. throw new ezcBaseValueException ( 'space', $space, 'self::COLORSPACE_GREY, self::COLORSPACE_SEPIA, self::COLORSPACE_MONOCHROME' );
361. break;
362. }
363. }
365. /**
366. * Noise filter.
367. * Apply a noise transformation to the image. Valid values are the following
368. * strings:
369. * - 'Uniform'
370. * - 'Gaussian'
371. * - 'Multiplicative'
372. * - 'Impulse'
373. * - 'Laplacian'
374. * - 'Poisson'
375. *
376. * ATTENTION: Using this filter method directly results in the filter being
377. * applied to the image which is internally marked as "active" (most
378. * commonly this is the last recently loaded one). It is highly recommended
379. * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
380. * method, which enables you to specify the image a filter is applied to.
381. *
382. * @param strings $value Noise value as described above.
383. * @return void
384. *
385. * @throws ezcBaseValueException
386. * If the noise value is out of range.
387. * @throws ezcImageInvalidReferenceException
388. * No loaded file could be found or an error destroyed a loaded reference.
389. */
390. public function noise ( $value )
391. {
392. $value = ucfirst ( strtolower ( $value ) );
393. $possibleValues = array(
394. 'Uniform',
395. 'Gaussian',
396. 'Multiplicative',
397. 'Impulse',
398. 'Laplacian',
399. 'Poisson',
400. );
401. if ( ! in_array ( $value, $possibleValues ) )
402. {
403. throw new ezcBaseValueException ( 'value', $value, 'Uniform, Gaussian, Multiplicative, Impulse, Laplacian, Poisson' );
404. }
405. $this-> addFilterOption (
406. $this->getActiveReference(),
407. '+noise',
408. $value
409. );
410. }
412. /**
413. * Swirl filter.
414. * Applies a swirl with the given intense to the image.
415. *
416. * ATTENTION: Using this filter method directly results in the filter being
417. * applied to the image which is internally marked as "active" (most
418. * commonly this is the last recently loaded one). It is highly recommended
419. * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
420. * method, which enables you to specify the image a filter is applied to.
421. *
422. * @param int $value Intense of swirl.
423. * @return void
424. *
425. * @throws ezcImageInvalidReferenceException
426. * No loaded file could be found or an error destroyed a loaded reference.
427. * @throws ezcBaseValueException
428. * If the swirl value is out of range.
429. */
430. public function swirl ( $value )
431. {
432. if ( ! is_int ( $value ) || $value < 0 )
433. {
434. throw new ezcBaseValueException ( 'value', $value, 'int >= 0' );
435. }
436. $this-> addFilterOption (
437. $this->getActiveReference(),
438. '-swirl',
439. $value
440. );
441. }
443. /**
444. * Border filter.
445. * Adds a border to the image. The width is measured in pixel. The color is
446. * defined in an array of hex values:
447. *
448. * <code>
449. * array(
450. * 0 => <red value>,
451. * 1 => <green value>,
452. * 2 => <blue value>,
453. * );
454. * </code>
455. *
456. * ATTENTION: Using this filter method directly results in the filter being
457. * applied to the image which is internally marked as "active" (most
458. * commonly this is the last recently loaded one). It is highly recommended
459. * to apply filters through the {@link ezcImageImagemagickHandler::applyFilter()}
460. * method, which enables you to specify the image a filter is applied to.
461. *
462. * @param int $width Width of the border.
463. * @param array(int) $color Color.
464. * @return void
465. *
466. * @throws ezcImageInvalidReferenceException
467. * No loaded file could be found or an error destroyed a loaded reference.
468. * @throws ezcBaseValueException
469. * If a submitted parameter was out of range or type.
470. */
471. public function border ( $width, array $color )
472. {
473. if ( !is_int( $width ) )
474. {
475. throw new ezcBaseValueException ( 'width', $width, 'int' );
476. }
477. $colorString = $this-> colorArrayToString ( $color );
478. $this-> addFilterOption (
479. $this->getActiveReference(),
480. '-bordercolor',
481. $colorString
482. );
483. $this-> addFilterOption (
484. $this->getActiveReference(),
485. '-border',
486. $width
487. );
488. }
490. /**
491. * Watermark filter.
492. * Places a watermark on the image. The file to use as the watermark image
493. * is given as $image. The $posX, $posY and $size values are given in
494. * percent, related to the destination image. A $size value of 10 will make
495. * the watermark appear in 10% of the destination image size.
496. * $posX = $posY = 10 will make the watermark appear in the top left corner
497. * of the destination image, 10% of its size away from its borders. If
498. * $size is ommitted, the watermark image will not be resized.
499. *
500. * @param string $image The image file to use as the watermark
501. * @param int $posX X position in the destination image in percent.
502. * @param int $posY Y position in the destination image in percent.
503. * @param int|bool$size Percentage size of the watermark, false for none.
504. * @return void
505. *
506. * @throws ezcImageInvalidReferenceException
507. * If no valid resource for the active reference could be found.
508. * @throws ezcImageFilterFailedException
509. * If the operation performed by the the filter failed.
510. * @throws ezcBaseValueException
511. * If a submitted parameter was out of range or type.
512. */
513. public function watermarkPercent ( $image, $posX, $posY, $size = false )
514. {
515. if ( ! is_string ( $image ) || ! file_exists ( $image ) || ! is_readable ( $image ) )
516. {
517. throw new ezcBaseValueException ( 'image', $image, 'string, path to an image file' );
518. }
519. if ( ! is_int ( $posX ) || $posX < 0 || $posX > 100 )
520. {
521. throw new ezcBaseValueException ( 'posX', $posX, 'int percentage value' );
522. }
523. if ( ! is_int ( $posY ) || $posY < 0 || $posY > 100 )
524. {
525. throw new ezcBaseValueException ( 'posY', $posY, 'int percentage value' );
526. }
527. if ( ! is_bool ( $size ) && ( ! is_int ( $size ) || $size < 0 || $size > 100 ) )
528. {
529. throw new ezcBaseValueException ( 'size', $size, 'int percentage value / bool' );
530. }
532. $data = getimagesize ( $this->getReferenceData( $this->getActiveReference(), "resource" ) );
534. $originalWidth = $data[0];
535. $originalHeight = $data[1];
537. $watermarkWidth = false;
538. $watermarkHeight = false;
540. if ( $size !== false )
541. {
542. $watermarkWidth = (int) round ( $originalWidth * $size / 100 );
543. $watermarkHeight = (int) round ( $originalHeight * $size / 100 );
544. }
546. $watermarkPosX = (int) round ( $originalWidth * $posX / 100 );
547. $watermarkPosY = (int) round ( $originalHeight * $posY / 100 );
549. $this-> watermarkAbsolute ( $image, $watermarkPosX, $watermarkPosY, $watermarkWidth, $watermarkHeight );
551. }
553. /**
554. * Watermark filter.
555. * Places a watermark on the image. The file to use as the watermark image
556. * is given as $image. The $posX, $posY and $size values are given in
557. * pixel. The watermark appear at $posX, $posY in the destination image
558. * with a size of $size pixel. If $size is ommitted, the watermark image
559. * will not be resized.
560. *
561. * @param string $image The image file to use as the watermark
562. * @param int $posX X position in the destination image in pixel.
563. * @param int $posY Y position in the destination image in pixel.
564. * @param int|bool$width Pixel size of the watermark, false to keep size.
565. * @param int|bool$height Pixel size of the watermark, false to keep size.
566. * @return void
567. *
568. * @throws ezcImageInvalidReferenceException
569. * If no valid resource for the active reference could be found.
570. * @throws ezcImageFilterFailedException
571. * If the operation performed by the the filter failed.
572. * @throws ezcBaseValueException
573. * If a submitted parameter was out of range or type.
574. */
575. public function watermarkAbsolute ( $image, $posX, $posY, $width = false, $height = false )
576. {
577. if ( ! is_string ( $image ) || ! file_exists ( $image ) || ! is_readable ( $image ) )
578. {
579. throw new ezcBaseValueException ( 'image', $image, 'string, path to an image file' );
580. }
581. if ( ! is_int ( $posX ) )
582. {
583. throw new ezcBaseValueException ( 'posX', $posX, 'int' );
584. }
585. if ( ! is_int ( $posY ) )
586. {
587. throw new ezcBaseValueException ( 'posY', $posY, 'int' );
588. }
589. if ( ! is_int ( $width ) && ! is_bool ( $width ) )
590. {
591. throw new ezcBaseValueException ( 'width', $width, 'int/bool' );
592. }
593. if ( ! is_int ( $height ) && ! is_bool ( $height ) )
594. {
595. throw new ezcBaseValueException ( 'height', $height, 'int/bool' );
596. }
598. $data = getimagesize ( $this->getActiveResource() );
600. // Negative offsets
601. $posX = ( $posX >= 0 ) ? $posX : $data[0] + $posX;
602. $posY = ( $posY >= 0 ) ? $posY : $data[1] + $posY;
604. $this-> addFilterOption (
605. $this->getActiveReference(),
606. '-composite',
607. ''
608. );
610. $this-> addFilterOption (
611. $this->getActiveReference(),
612. '-geometry',
613. ( $width !== false ? $width : "" ) . ( $height !== false ? "x$height" : "" ) . "+$posX+$posY"
614. );
616. $this-> addCompositeImage ( $this->getActiveReference(), $image );
617. }
619. /**
620. * Creates a thumbnail, and crops parts of the given image to fit the range best.
621. * This filter creates a thumbnail of the given image. The image is scaled
622. * down, keeping the original ratio and keeping the image larger as the
623. * given range, if necessary. Overhead for the target range is cropped from
624. * both sides equally.
625. *
626. * If you are looking for a filter that just resizes your image to
627. * thumbnail size, you should consider the {@link }
628. * ezcImageImagemagickHandler::scale()} filter.
629. *
630. * @param int $width Width of the thumbnail.
631. * @param int $height Height of the thumbnail.
632. */
633. public function croppedThumbnail ( $width, $height )
634. {
635. if ( ! is_int ( $width ) || $width < 1 )
636. {
637. throw new ezcBaseValueException ( 'width', $width, 'int > 0' );
638. }
639. if ( ! is_int ( $height ) || $height < 1 )
640. {
641. throw new ezcBaseValueException ( 'height', $height, 'int > 0' );
642. }
643. $data = getimagesize ( $this->getReferenceData( $this->getActiveReference(), "resource" ) );
645. $scaleRatio = max ( $width / $data[0], $height / $data[1] );
646. $scaleWidth = round ( $data[0] * $scaleRatio );
647. $scaleHeight = round ( $data[1] * $scaleRatio );
649. $cropOffsetX = ( $scaleWidth > $width ) ? "+" . round ( ( $scaleWidth - $width ) / 2 ) : "+0";
650. $cropOffsetY = ( $scaleHeight > $height ) ? "+" . round ( ( $scaleHeight - $height ) / 2 ) : "+0";
652. $this-> addFilterOption (
653. $this->getActiveReference(),
654. '-resize',
655. $scaleWidth . "x" . $scaleHeight
656. );
657. $this-> addFilterOption (
658. $this->getActiveReference(),
659. '-crop',
660. $width . "x" . $height . $cropOffsetX . $cropOffsetY . "!"
661. );
662. }
664. /**
665. * Creates a thumbnail, and fills up the image to fit the given range.
666. * This filter creates a thumbnail of the given image. The image is scaled
667. * down, keeping the original ratio and scaling the image smaller as the
668. * given range, if necessary. Overhead for the target range is filled with the given
669. * color on both sides equally.
670. *
671. * The color is defined by the following array format (integer values 0-255):
672. *
673. * <code>
674. * array(
675. * 0 => <red value>,
676. * 1 => <green value>,
677. * 2 => <blue value>,
678. * );
679. * </code>
680. *
681. * If you are looking for a filter that just resizes your image to
682. * thumbnail size, you should consider the {@link }
683. * ezcImageImagemagickHandler::scale()} filter.
684. *
685. * @param int $width Width of the thumbnail.
686. * @param int $height Height of the thumbnail.
687. * @param array $color Fill color.
688. * @return void
689. */
690. public function filledThumbnail ( $width, $height, $color = array() )
691. {
692. if ( ! is_int ( $width ) || $width < 1 )
693. {
694. throw new ezcBaseValueException ( 'width', $width, 'int > 0' );
695. }
696. if ( ! is_int ( $height ) || $height < 1 )
697. {
698. throw new ezcBaseValueException ( 'height', $height, 'int > 0' );
699. }
700. $data = getimagesize ( $this->getReferenceData( $this->getActiveReference(), "resource" ) );
702. $scaleRatio = min ( $width / $data[0], $height / $data[1] );
703. $scaleWidth = round ( $data[0] * $scaleRatio );
704. $scaleHeight = round ( $data[1] * $scaleRatio );
706. $cropOffsetX = ( $scaleWidth < $width ) ? "-" . round ( ( $width - $scaleWidth ) / 2 ) : "+0";
707. $cropOffsetY = ( $scaleHeight < $height ) ? "-" . round ( ( $height - $scaleHeight ) / 2 ) : "+0";
709. $colorString = '#';
710. $i = 0;
711. foreach ( $color as $id => $colorVal )
712. {
713. if ( $i++ > 2 )
714. {
715. break;
716. }
717. if ( ! is_int ( $colorVal ) || $colorVal < 0 || $colorVal > 255 )
718. {
719. throw new ezcBaseValueException ( "color[$id]", $color[$id], 'int > 0 and < 256' );
720. }
721. $colorString .= sprintf ( '%02x', $colorVal );
722. }
724. $this-> addFilterOption (
725. $this->getActiveReference(),
726. '-resize',
727. $width . "x" . $height
728. );
729. $this-> addFilterOption (
730. $this->getActiveReference(),
731. '-crop',
732. $width . "x" . $height . $cropOffsetX . $cropOffsetY . "!"
733. );
734. $this-> addFilterOption (
735. $this->getActiveReference(),
736. '-background',
737. $colorString
738. );
739. $this-> addFilterOption (
740. $this->getActiveReference(),
741. '-flatten'
742. );
743. }
745. /**
746. * Returns the ImageMagick direction modifier for a direction constant.
747. * ImageMagick supports the following modifiers to determine if an
748. * image should be scaled up only, down only or in both directions:
749. *
750. * <code>
751. * SCALE_UP: >
752. * SCALE_DOWN: <
753. * </code>
754. *
755. * This method returns the correct modifier for the internal direction
756. * constants.
757. *
758. * @param int $direction One of ezcImageGeometryFilters::SCALE_*
759. * @return string The correct modifier.
760. *
761. * @throws ezcBaseValueException
762. * If a submitted parameter was out of range or type.
763. */
764. protected function getDirectionModifier ( $direction )
765. {
766. $dirMod = '';
767. switch ( $direction )
768. {
769. case self::SCALE_DOWN:
770. $dirMod = '>';
771. break;
772. case self::SCALE_UP:
773. $dirMod = '<';
774. break;
775. case self::SCALE_BOTH:
776. $dirMod = '';
777. break;
778. default:
779. throw new ezcBaseValueException ( 'direction', $direction, 'self::SCALE_BOTH, self::SCALE_UP, self::SCALE_DOWN' );
780. break;
781. }
782. return $dirMod;
783. }
784. }
785. ?>

Documentation generated by phpDocumentor 1.4.3