1 <?php
2 /**
3 * Image API driver.
4 *
5 * $Id: Image.php 3769 2008-12-15 00:48:56Z zombor $
6 *
7 * @package Image
8 * @author Kohana Team
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
11 */
12 abstract class Image_Driver {
13
14 // Reference to the current image
15 protected $image;
16
17 // Reference to the temporary processing image
18 protected $tmp_image;
19
20 // Processing errors
21 protected $errors = array();
22
23 /**
24 * Executes a set of actions, defined in pairs.
25 *
26 * @param array actions
27 * @return boolean
28 */
29 public function execute($actions)
30 {
31 foreach ($actions as $func => $args)
32 {
33 if ( ! $this->$func($args))
34 return FALSE;
35 }
36
37 return TRUE;
38 }
39
40 /**
41 * Sanitize and normalize a geometry array based on the temporary image
42 * width and height. Valid properties are: width, height, top, left.
43 *
44 * @param array geometry properties
45 * @return void
46 */
47 protected function sanitize_geometry( & $geometry)
48 {
49 list($width, $height) = $this->properties();
50
51 // Turn off error reporting
52 $reporting = error_reporting(0);
53
54 // Width and height cannot exceed current image size
55 $geometry['width'] = min($geometry['width'], $width);
56 $geometry['height'] = min($geometry['height'], $height);
57
58 // Set standard coordinates if given, otherwise use pixel values
59 if ($geometry['top'] === 'center')
60 {
61 $geometry['top'] = floor(($height / 2) - ($geometry['height'] / 2));
62 }
63 elseif ($geometry['top'] === 'top')
64 {
65 $geometry['top'] = 0;
66 }
67 elseif ($geometry['top'] === 'bottom')
68 {
69 $geometry['top'] = $height - $geometry['height'];
70 }
71
72 // Set standard coordinates if given, otherwise use pixel values
73 if ($geometry['left'] === 'center')
74 {
75 $geometry['left'] = floor(($width / 2) - ($geometry['width'] / 2));
76 }
77 elseif ($geometry['left'] === 'left')
78 {
79 $geometry['left'] = 0;
80 }
81 elseif ($geometry['left'] === 'right')
82 {
83 $geometry['left'] = $width - $geometry['height'];
84 }
85
86 // Restore error reporting
87 error_reporting($reporting);
88 }
89
90 /**
91 * Return the current width and height of the temporary image. This is mainly
92 * needed for sanitizing the geometry.
93 *
94 * @return array width, height
95 */
96 abstract protected function properties();
97
98 /**
99 * Process an image with a set of actions.
100 *
101 * @param string image filename
102 * @param array actions to execute
103 * @param string destination directory path
104 * @param string destination filename
105 * @return boolean
106 */
107 abstract public function process($image, $actions, $dir, $file);
108
109 /**
110 * Flip an image. Valid directions are horizontal and vertical.
111 *
112 * @param integer direction to flip
113 * @return boolean
114 */
115 abstract function flip($direction);
116
117 /**
118 * Crop an image. Valid properties are: width, height, top, left.
119 *
120 * @param array new properties
121 * @return boolean
122 */
123 abstract function crop($properties);
124
125 /**
126 * Resize an image. Valid properties are: width, height, and master.
127 *
128 * @param array new properties
129 * @return boolean
130 */
131 abstract public function resize($properties);
132
133 /**
134 * Rotate an image. Valid amounts are -180 to 180.
135 *
136 * @param integer amount to rotate
137 * @return boolean
138 */
139 abstract public function rotate($amount);
140
141 /**
142 * Sharpen and image. Valid amounts are 1 to 100.
143 *
144 * @param integer amount to sharpen
145 * @return boolean
146 */
147 abstract public function sharpen($amount);
148
149 } // End Image Driver