/
Image.class.php
211 lines (191 loc) · 5.42 KB
/
Image.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
// clean
namespace ImageEffects;
// fileinfo requirement
if (!in_array('fileinfo', get_loaded_extensions())) {
throw new \Exception('Fileinfo extension needs to be installed.');
}
// imagick requirement
if (!in_array('imagick', get_loaded_extensions())) {
throw new Exception('Imagick extension needs to be installed.');
}
// convert command (eg. php extension installed, but not Imagemagick)
if (strstr(exec('type convert'), 'found')) {
throw new \Exception(
'Imagick needs to be installed. Try <sudo apt-get update> and ' .
'<sudo apt-get install imagemagick>'
);
}
// dependencies
require_once 'Actions.class.php';
require_once 'Filters.class.php';
/**
* Image
*
* @author Oliver Nassar <onassar@gmail.com>
* @see <http://net.tutsplus.com/tutorials/php/create-instagram-filters-with-php/>
* @thanks <https://github.com/webarto/instagraph>
* @example
* <code>
* // library inclusion
* require_once APP . '/vendors/PHP-ImageEffects/Image.class.php';
*
* // instantiation with image path
* $image = (new ImageEffects\Image(APP . '/webroot/kittens.jpg'));
*
* // header definiton; squaring of image output
* header('Content-Type: image/jpeg');
* echo $image->lomo();
* exit(0);
* </code>
*/
class Image
{
/**
* _effects
*
* @var Array
* @access protected
*/
protected $_effects = array(
'bw',
'gotham',
'kelvin',
'lomo',
'nashville',
'tilt',
'toaster'
);
/**
* _path
*
* @var String
* @access protected
*/
protected $_path;
/**
* _range
*
* @var Array
* @access protected
*/
protected $_range = array(
'image/jpeg',
'image/jpg',
'image/png'
);
/**
* _reserved
*
* @var String
* @access protected
*/
protected $_reserved;
/**
* _type
*
* @var String
* @access protected
*/
protected $_type;
/**
* __construct
*
* Bails if the path contains invalid characters.
*
* @todo See description :P
* @public
* @param String $path
* @return void
*/
public function __construct($path)
{
if (!is_file($path)) {
throw new \Exception(
'Image *' . ($path) . '* is not a valid file.'
);
}
$this->_path = $path;
$this->_loadResource();
}
/**
* __call
*
* @access public
* @return String $name
* @return Array $arguments
* @return String the raw binary of the altered-image
*/
public function __call($name, Array $arguments)
{
// if it's an *invalid* effect
if (in_array($name, $this->_effects) === false) {
throw new \Exception(
'*' . ($name) . '* is an invalid effect.'
);
}
// apply through <Filters>
call_user_func_array(
array('ImageEffects\Filters', $name),
array($this->_reserved)
);
// read image from path
$resource = (new \Imagick());
$resource->readImage($this->_reserved);
$blob = $resource->getImageBlob();
// delete reserved path
unlink($this->_reserved);
// response with the raw binary
return $blob;
}
/**
* _getFileType
*
* @access protected
* @return String
*/
protected function _getFileType()
{
if (!is_null($this->_type)) {
return $this->_type;
}
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$this->_type = finfo_file($finfo, $this->_path);
return $this->_type;
}
/**
* _loadResource
*
* @access protected
* @return void
*/
protected function _loadResource()
{
// type enforcement
$type = $this->_getFileType();
$keys = array_keys($this->_range);
if (!in_array($type, $keys)) {
throw new \Exception(
'Invalid image type being processed. Image *' .
$this->_path . '* is of type *' . ($type) . '* and must ' .
'be one of the following: ' . implode(', ', $keys)
);
}
// reserve a file on the disk (for <exec> manipulation)
$this->_reserve();
}
/**
* _reserve
*
* @access protected
* @return void
*/
protected function _reserve()
{
// set up reserved path
$rand = rand(10000, 99999);
$reserved = ($this->_path) . '.' . ($rand);
copy($this->_path, $reserved);
$this->_reserved = $reserved;
}
}