XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
phmagick.php
Go to the documentation of this file.
1 <?php
2 /*
3  +--------------------------------------------------------------------------------------------+
4  | DISCLAIMER - LEGAL NOTICE - |
5  +--------------------------------------------------------------------------------------------+
6  | |
7  | This program is free for non comercial use, see the license terms available at |
8  | http://www.francodacosta.com/licencing/ for more information |
9  | |
10  | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
11  | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
12  | |
13  | USE IT AT YOUR OWN RISK |
14  | |
15  | |
16  +--------------------------------------------------------------------------------------------+
17 
18 */
29 class phmagick{
30  private $availableMethods = array();
31  private $loadedPlugins = array();
32 
33  private $escapeChars = null ;
34 
35  private $history = array();
36  private $originalFile = '';
37  private $source = '';
38  private $destination = '';
39  private $imageMagickPath = '';
40  private $imageQuality = 80 ;
41 
42  public $debug = false;
43  private $log = array();
44 
45  function __construct($sourceFile='', $destinationFile=''){
46  $this->originalFile = $sourceFile;
47 
48  $this->source = $sourceFile ;
49  $this->destination = $destinationFile;
50 
51  if(is_null($this->escapeChars) ){
52  $this->escapeChars = !( strtolower ( substr( php_uname('s'), 0, 3)) == "win" ) ;
53  }
54 
55  $this->loadPlugins();
56  }
57 
58 
59  public function getLog(){
60  return $this->log;
61  }
62  public function getBinary($binName){
63  return $this->getImageMagickPath() . $binName ;
64  }
65 
66  //-----------------
67  function setSource ($path){
68  $this->source = str_replace(' ','\ ',$path) ;
69  return $this ;
70  }
71 
72  function getSource (){
73  return $this->source ;
74  }
75 
76  //-----------------
77  function setDestination ($path){
78  $path = str_replace(' ','\ ',$path) ;
79  $this->destination = $path ;
80  return $this;
81  }
82 
83  function getDestination (){
84  if( ($this->destination == '')){
85  $source = $this->getSource() ;
86  $ext = end (explode('.', $source)) ;
87  $this->destinationFile = dirname($source) . '/' . md5(microtime()) . '.' . $ext;
88  }
89  return $this->destination ;
90  }
91 
92  //-----------------
93 
95  if($path != '')
96  if ( strpos($path, '/') < strlen($path))
97  $path .= '/';
98  $this->imageMagickPath = str_replace(' ','\ ',$path) ;
99  }
100 
101  function getImageMagickPath (){
102  return $this->imageMagickPath;
103  }
104  //-----------------
105  function setImageQuality($value){
106  $this->imageQuality = intval($value);
107  return $this;
108  }
109 
110  function getImageQuality(){
111  return $this->imageQuality;
112  }
113 
114  //-----------------
115 
116  function getHistory( $type = Null ){
117  switch ($type){
118 
120  return explode(',', array_unique($this->history));
121  break;
122 
123  default:
125  return array_unique($this->history) ;
126  break;
127 
128  }
129  }
130 
131  public function setHistory($path){
132  $this->history[] = $path ;
133  return $this;
134  }
135 
136  public function clearHistory(){
137  unset ($this->history);
138  $this->history = array();
139  }
140 
141 
142  public function requirePlugin($name, $version=null){
143 
144  if(key_exists($name, $this->loadedPlugins)) {
145  if(! is_null($version)) {
146  if( property_exists($this->loadedPlugins[$name], 'version') ){
147  if($this->loadedPlugins[$name]->version > $version)
148  return true;
149 
150  if($this->debug) throw new phMagickException ('Plugin "'.$name.'" version ='.$this->loadedPlugins[$name]->version . ' required >= ' . $version);
151  }
152  }
153  return true ;
154  }
155 
156  if($this->debug) throw new phMagickException ('Plugin "'.$name.'" not found!');
157  return false;
158  }
159 
160  //-----------------
161 
162  private function loadPlugins(){
163  $base = dirname(__FILE__) . '/plugins';
164  $plugins = glob($base . '/*.php');
165  foreach($plugins as $plugin){
166  include_once $plugin ;
167  $name = basename($plugin, '.php');
168  $className = 'phMagick_'.$name ;
169  $obj = new $className();
170  $this->loadedPlugins[$name] = $obj ;
171  foreach (get_class_methods($obj) as $method )
172  $this->availableMethods[$method] = $name ;
173  }
174  }
175 
176 
177  public function execute($cmd){
178 
179  $ret = null ;
180  $out = array();
181 
182  if($this->escapeChars) {
183  $cmd= str_replace ('(','\(',$cmd);
184  $cmd= str_replace (')','\)',$cmd);
185  }
186  exec( $cmd .' 2>&1', $out, $ret);
187 
188  if($ret != 0)
189  if($this->debug) trigger_error (new phMagickException ('Error executing "'. $cmd.'" <br>return code: '. $ret .' <br>command output :"'. implode("<br>", $out).'"' ), E_USER_NOTICE );
190 
191  $this->log[] = array(
192  'cmd' => $cmd
193  ,'return' => $ret
194  ,'output' => $out
195  );
196 
197  return $ret ;
198  }
199 
200  public function __call($method, $args){
201  if(! key_exists($method, $this->availableMethods))
202  throw new Exception ('Call to undefined method : ' . $method);
203 
204  array_unshift($args, $this);
205  $ret = call_user_func_array(array($this->loadedPlugins[$this->availableMethods[$method]], $method), $args);
206 
207  if($ret === false)
208  throw new Exception ('Error executing method "' . $method ."'");
209 
210  return $ret ;
211  }
212 }
213 
215  const returnArray = 0 ;
216  const returnCsv = 1 ;
217 
218  private function __construct(){}
219 }
220 
221 
222 class phMagickException extends Exception {
223 
224  function __construct($message, $code=1){
225  //parent::__construct('', $code);
226  $this->message($message);
227  }
228  function message($message){
229  echo '<br><b>phMagick</b>: ' . $message ;
230  }
231 }
232 
233 
235  const None = 'None' ;
236  const Center = 'Center' ;
237  const East = 'East' ;
238  const Forget = 'Forget' ;
239  const NorthEast = 'NorthEast' ;
240  const North = 'North' ;
241  const NorthWest = 'NorthWest' ;
242  const SouthEast = 'SouthEast' ;
243  const South = 'South' ;
244  const SouthWest = 'SouthWest' ;
245  const West = 'West' ;
246 
247  private function __construct(){}
248 }
249 
250 
252  public static $fontSize ='12';
253  public static $font = false;
254 
255  public static $color = '#000';
256  public static $background = false;
257 
258  public static $gravity = phMagickGravity::Center; //ignored in fromString()
259  public $Text = '';
260 
261  private function __construct(){}
262 }
263 
264 
266  protected $fontSize;
267  protected $font;
268 
269  protected $color;
270  protected $background;
271 
272  protected $pGravity; //ignored in fromString()
273  protected $pText = '';
274 
275  public function __construct(){
280  $this->pGravity = phMagickTextObjectDefaults::$gravity;
281  }
282 
283  function defaultFontSize($value){
285  }
286 
287  function defaultFont($value){
289  }
290 
291  function defaultColor($value){
293  }
294 
295  function defaultBackground($value){
297  }
298 
299  function defaultGravity($value){
301  }
302 
303 
304 
305  function fontSize($i){
306  $this->fontSize = $i ;
307  return $this;
308  }
309 
310  function font($i){
311  $this->font = $i ;
312  return $this;
313  }
314 
315  function color($i){
316  $this->color = $i ;
317  return $this;
318  }
319 
320  function background($i){
321  $this->background = $i ;
322  return $this;
323  }
324 
325  function __get($var){
326  return $this->$var ;
327  }
328 
329  function gravity( $gravity){
330  $this->pGravity = $gravity;
331  return $this ;
332  }
333 
334  function text( $text){
335  $this->pText = $text;
336  return $this ;
337  }
338 }
339 
340 ?>