1
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.'); |
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
|
|
|
|
15
|
|
|
// Reference to the current image |
16
|
|
|
protected $image; |
17
|
|
|
|
18
|
|
|
// Reference to the temporary processing image |
19
|
|
|
protected $tmp_image; |
20
|
|
|
|
21
|
|
|
// Processing errors |
22
|
|
|
protected $errors = array(); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Executes a set of actions, defined in pairs. |
26
|
|
|
* |
27
|
|
|
* @param array actions |
28
|
|
|
* @return boolean |
29
|
|
|
*/ |
30
|
|
|
public function execute($actions) |
31
|
|
|
{ |
32
|
|
|
foreach ($actions as $func => $args) { |
33
|
|
|
if (! $this->$func($args)) { |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return true; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Sanitize and normalize a geometry array based on the temporary image |
43
|
|
|
* width and height. Valid properties are: width, height, top, left. |
44
|
|
|
* |
45
|
|
|
* @param array geometry properties |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
protected function sanitize_geometry(& $geometry) |
49
|
|
|
{ |
50
|
|
|
list($width, $height) = $this->properties(); |
51
|
|
|
|
52
|
|
|
// Turn off error reporting |
53
|
|
|
$reporting = error_reporting(0); |
54
|
|
|
|
55
|
|
|
// Width and height cannot exceed current image size |
56
|
|
|
$geometry['width'] = min($geometry['width'], $width); |
57
|
|
|
$geometry['height'] = min($geometry['height'], $height); |
58
|
|
|
|
59
|
|
|
// Set standard coordinates if given, otherwise use pixel values |
60
|
|
View Code Duplication |
if ($geometry['top'] === 'center') { |
|
|
|
|
61
|
|
|
$geometry['top'] = floor(($height / 2) - ($geometry['height'] / 2)); |
62
|
|
|
} elseif ($geometry['top'] === 'top') { |
63
|
|
|
$geometry['top'] = 0; |
64
|
|
|
} elseif ($geometry['top'] === 'bottom') { |
65
|
|
|
$geometry['top'] = $height - $geometry['height']; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Set standard coordinates if given, otherwise use pixel values |
69
|
|
View Code Duplication |
if ($geometry['left'] === 'center') { |
|
|
|
|
70
|
|
|
$geometry['left'] = floor(($width / 2) - ($geometry['width'] / 2)); |
71
|
|
|
} elseif ($geometry['left'] === 'left') { |
72
|
|
|
$geometry['left'] = 0; |
73
|
|
|
} elseif ($geometry['left'] === 'right') { |
74
|
|
|
$geometry['left'] = $width - $geometry['height']; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Restore error reporting |
78
|
|
|
error_reporting($reporting); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return the current width and height of the temporary image. This is mainly |
83
|
|
|
* needed for sanitizing the geometry. |
84
|
|
|
* |
85
|
|
|
* @return array width, height |
86
|
|
|
*/ |
87
|
|
|
abstract protected function properties(); |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Process an image with a set of actions. |
91
|
|
|
* |
92
|
|
|
* @param string image filename |
93
|
|
|
* @param array actions to execute |
94
|
|
|
* @param string destination directory path |
95
|
|
|
* @param string destination filename |
96
|
|
|
* @return boolean |
97
|
|
|
*/ |
98
|
|
|
abstract public function process($image, $actions, $dir, $file); |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Flip an image. Valid directions are horizontal and vertical. |
102
|
|
|
* |
103
|
|
|
* @param integer direction to flip |
104
|
|
|
* @return boolean |
105
|
|
|
*/ |
106
|
|
|
abstract public function flip($direction); |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Crop an image. Valid properties are: width, height, top, left. |
110
|
|
|
* |
111
|
|
|
* @param array new properties |
112
|
|
|
* @return boolean |
113
|
|
|
*/ |
114
|
|
|
abstract public function crop($properties); |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Resize an image. Valid properties are: width, height, and master. |
118
|
|
|
* |
119
|
|
|
* @param array new properties |
120
|
|
|
* @return boolean |
121
|
|
|
*/ |
122
|
|
|
abstract public function resize($properties); |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Rotate an image. Valid amounts are -180 to 180. |
126
|
|
|
* |
127
|
|
|
* @param integer amount to rotate |
128
|
|
|
* @return boolean |
129
|
|
|
*/ |
130
|
|
|
abstract public function rotate($amount); |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Sharpen and image. Valid amounts are 1 to 100. |
134
|
|
|
* |
135
|
|
|
* @param integer amount to sharpen |
136
|
|
|
* @return boolean |
137
|
|
|
*/ |
138
|
|
|
abstract public function sharpen($amount); |
139
|
|
|
} // End Image Driver |
140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.