-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathphmagick.php
More file actions
355 lines (278 loc) · 10.5 KB
/
phmagick.php
File metadata and controls
355 lines (278 loc) · 10.5 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/*
+--------------------------------------------------------------------------------------------+
| DISCLAIMER - LEGAL NOTICE - |
+--------------------------------------------------------------------------------------------+
| |
| This program is free for non comercial use, see the license terms available at |
| http://www.francodacosta.com/licencing/ for more information |
| |
| This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| |
| USE IT AT YOUR OWN RISK |
| |
| |
+--------------------------------------------------------------------------------------------+
*/
/**
* phMagick - Image manipulation with Image Magick
*
* @version 0.4.1
* @author Nuno Costa - sven@francodacosta.com
* @copyright Copyright (c) 2007
* @license LGPL
* @link http://www.francodacosta.com/phmagick
* @since 2008-03-13
*/
if (! class_exists('phmagick')) {
class phmagick{
private $availableMethods = array();
private $loadedPlugins = array();
private $escapeChars = null ;
private $history = array();
private $originalFile = '';
private $source = '';
private $destination = '';
private $imageMagickPath = '';
private $imageQuality = 80 ;
public $debug = false;
private $log = array();
function __construct($sourceFile='', $destinationFile=''){
$this->originalFile = $sourceFile;
$this->source = $sourceFile ;
$this->destination = $destinationFile;
if(is_null($this->escapeChars) ){
$this->escapeChars = !( strtolower ( substr( php_uname('s'), 0, 3)) == "win" ) ;
}
$this->loadPlugins();
}
public function getLog(){
return $this->log;
}
public function getBinary($binName){
return $this->getImageMagickPath() . $binName ;
}
//-----------------
function setSource ($path){
$this->source = str_replace(' ','\ ',$path) ;
return $this ;
}
function getSource (){
return $this->source ;
}
//-----------------
function setDestination ($path){
$path = str_replace(' ','\ ',$path) ;
$this->destination = $path ;
return $this;
}
function getDestination (){
if( ($this->destination == '')){
$source = $this->getSource() ;
$ext = end (explode('.', $source)) ;
$this->destinationFile = dirname($source) . '/' . md5(microtime()) . '.' . $ext;
}
return $this->destination ;
}
//-----------------
function setImageMagickPath ($path){
if($path != '')
if ( strpos($path, '/') < strlen($path))
$path .= '/';
$this->imageMagickPath = str_replace(' ','\ ',$path) ;
}
function getImageMagickPath (){
return $this->imageMagickPath;
}
//-----------------
function setImageQuality($value){
$this->imageQuality = intval($value);
return $this;
}
function getImageQuality(){
return $this->imageQuality;
}
//-----------------
function getHistory( $type = Null ){
switch ($type){
case phMagickHistory::returnCsv:
return explode(',', array_unique($this->history));
break;
default:
case phMagickHistory::returnArray :
return array_unique($this->history) ;
break;
}
}
public function setHistory($path){
$this->history[] = $path ;
return $this;
}
public function clearHistory(){
unset ($this->history);
$this->history = array();
}
public function requirePlugin($name, $version=null){
if(key_exists($name, $this->loadedPlugins)) {
if(! is_null($version)) {
if( property_exists($this->loadedPlugins[$name], 'version') ){
if($this->loadedPlugins[$name]->version > $version)
return true;
if($this->debug) throw new phMagickException ('Plugin "'.$name.'" version ='.$this->loadedPlugins[$name]->version . ' required >= ' . $version);
}
}
return true ;
}
if($this->debug) throw new phMagickException ('Plugin "'.$name.'" not found!');
return false;
}
//-----------------
private function loadPlugins(){
$base = dirname(__FILE__) . '/plugins';
$plugins = glob($base . '/*.php');
foreach($plugins as $plugin){
include_once $plugin ;
$name = basename($plugin, '.php');
$className = 'phMagick_'.$name ;
$obj = new $className();
$this->loadedPlugins[$name] = $obj ;
foreach (get_class_methods($obj) as $method )
$this->availableMethods[$method] = $name ;
}
}
public function execute($cmd){
$ret = null ;
$out = array();
if($this->escapeChars) {
$cmd= str_replace ('(','\(',$cmd);
$cmd= str_replace (')','\)',$cmd);
}
exec( $cmd .' 2>&1', $out, $ret);
if($ret != 0)
if($this->debug) trigger_error (new phMagickException ('Error executing "'. $cmd.'" <br>return code: '. $ret .' <br>command output :"'. implode("<br>", $out).'"' ), E_USER_NOTICE );
$this->log[] = array(
'cmd' => $cmd
,'return' => $ret
,'output' => $out
);
return $ret ;
}
public function __call($method, $args){
if(! key_exists($method, $this->availableMethods))
throw new Exception ('Call to undefined method : ' . $method);
array_unshift($args, $this);
$ret = call_user_func_array(array($this->loadedPlugins[$this->availableMethods[$method]], $method), $args);
if($ret === false)
throw new Exception ('Error executing method "' . $method ."'");
return $ret ;
}
}
class phMagickHistory{
const returnArray = 0 ;
const returnCsv = 1 ;
private function __construct(){}
}
class phMagickException extends Exception {
function __construct($message, $code=1){
//parent::__construct('', $code);
$this->message($message);
}
function message($message){
echo '<br><b>phMagick</b>: ' . $message ;
}
}
class phMagickGravity{
const None = 'None' ;
const Center = 'Center' ;
const East = 'East' ;
const Forget = 'Forget' ;
const NorthEast = 'NorthEast' ;
const North = 'North' ;
const NorthWest = 'NorthWest' ;
const SouthEast = 'SouthEast' ;
const South = 'South' ;
const SouthWest = 'SouthWest' ;
const West = 'West' ;
private function __construct(){}
}
class phMagickTextObjectDefaults{
public static $fontSize ='12';
public static $font = false;
public static $color = '#000';
public static $background = false;
public static $gravity = phMagickGravity::Center; //ignored in fromString()
public $Text = '';
private function __construct(){}
}
class phMagickTextObject {
protected $fontSize;
protected $font;
//If font size isn't specified
//it may be specified by the width / height
protected $width;
protected $height;
protected $color;
protected $background;
protected $pGravity; //ignored in fromString()
protected $pText = '';
public function __construct(){
$this->fontSize = phMagickTextObjectDefaults::$fontSize;
$this->font = phMagickTextObjectDefaults::$font;
$this->color = phMagickTextObjectDefaults::$color ;
$this->background = phMagickTextObjectDefaults::$background;
$this->pGravity = phMagickTextObjectDefaults::$gravity;
}
function defaultFontSize($value){
phMagickTextObjectDefaults::$fontSize = $value;
}
function defaultFont($value){
phMagickTextObjectDefaults::$font = $value;
}
function defaultColor($value){
phMagickTextObjectDefaults::$color = $value;
}
function defaultBackground($value){
phMagickTextObjectDefaults::$background = $value;
}
function defaultGravity($value){
phMagickTextObjectDefaults::$gravity = $value;
}
function width($w) {
$this->width = $w;
return $this;
}
function height($h) {
$this->height = $h;
return $this;
}
function fontSize($i){
$this->fontSize = $i ;
return $this;
}
function font($i){
$this->font = $i ;
return $this;
}
function color($i){
$this->color = $i ;
return $this;
}
function background($i){
$this->background = $i ;
return $this;
}
function __get($var){
return $this->$var ;
}
function gravity( $gravity){
$this->pGravity = $gravity;
return $this ;
}
function text( $text){
$this->pText = $text;
return $this ;
}
}
}
?>