PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/vendors/php_thumb/phmagick/phmagick.php

http://bedita.googlecode.com/
PHP | 340 lines | 244 code | 63 blank | 33 comment | 20 complexity | 2d1ae373b0334306917f77752a6f5e20 MD5 | raw file
Possible License(s): AGPL-1.0, AGPL-3.0, LGPL-3.0, LGPL-2.1, GPL-3.0
  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. /**
  19. * phMagick - Image manipulation with Image Magick
  20. *
  21. * @version 0.4.1
  22. * @author Nuno Costa - sven@francodacosta.com
  23. * @copyright Copyright (c) 2007
  24. * @license LGPL
  25. * @link http://www.francodacosta.com/phmagick
  26. * @since 2008-03-13
  27. */
  28. class phmagick{
  29. private $availableMethods = array();
  30. private $loadedPlugins = array();
  31. private $escapeChars = null ;
  32. private $history = array();
  33. private $originalFile = '';
  34. private $source = '';
  35. private $destination = '';
  36. private $imageMagickPath = '';
  37. private $imageQuality = 80 ;
  38. public $debug = false;
  39. private $log = array();
  40. function __construct($sourceFile='', $destinationFile=''){
  41. $this->originalFile = $sourceFile;
  42. $this->source = $sourceFile ;
  43. $this->destination = $destinationFile;
  44. if(is_null($this->escapeChars) ){
  45. $this->escapeChars = !( strtolower ( substr( php_uname('s'), 0, 3)) == "win" ) ;
  46. }
  47. $this->loadPlugins();
  48. }
  49. public function getLog(){
  50. return $this->log;
  51. }
  52. public function getBinary($binName){
  53. return $this->getImageMagickPath() . $binName ;
  54. }
  55. //-----------------
  56. function setSource ($path){
  57. $this->source = str_replace(' ','\ ',$path) ;
  58. return $this ;
  59. }
  60. function getSource (){
  61. return $this->source ;
  62. }
  63. //-----------------
  64. function setDestination ($path){
  65. $path = str_replace(' ','\ ',$path) ;
  66. $this->destination = $path ;
  67. return $this;
  68. }
  69. function getDestination (){
  70. if( ($this->destination == '')){
  71. $source = $this->getSource() ;
  72. $ext = end (explode('.', $source)) ;
  73. $this->destinationFile = dirname($source) . '/' . md5(microtime()) . '.' . $ext;
  74. }
  75. return $this->destination ;
  76. }
  77. //-----------------
  78. function setImageMagickPath ($path){
  79. if($path != '')
  80. if ( strpos($path, '/') < strlen($path))
  81. $path .= '/';
  82. $this->imageMagickPath = str_replace(' ','\ ',$path) ;
  83. }
  84. function getImageMagickPath (){
  85. return $this->imageMagickPath;
  86. }
  87. //-----------------
  88. function setImageQuality($value){
  89. $this->imageQuality = intval($value);
  90. return $this;
  91. }
  92. function getImageQuality(){
  93. return $this->imageQuality;
  94. }
  95. //-----------------
  96. function getHistory( $type = Null ){
  97. switch ($type){
  98. case phMagickHistory::returnCsv:
  99. return explode(',', array_unique($this->history));
  100. break;
  101. default:
  102. case phMagickHistory::returnArray :
  103. return array_unique($this->history) ;
  104. break;
  105. }
  106. }
  107. public function setHistory($path){
  108. $this->history[] = $path ;
  109. return $this;
  110. }
  111. public function clearHistory(){
  112. unset ($this->history);
  113. $this->history = array();
  114. }
  115. public function requirePlugin($name, $version=null){
  116. if(key_exists($name, $this->loadedPlugins)) {
  117. if(! is_null($version)) {
  118. if( property_exists($this->loadedPlugins[$name], 'version') ){
  119. if($this->loadedPlugins[$name]->version > $version)
  120. return true;
  121. if($this->debug) throw new phMagickException ('Plugin "'.$name.'" version ='.$this->loadedPlugins[$name]->version . ' required >= ' . $version);
  122. }
  123. }
  124. return true ;
  125. }
  126. if($this->debug) throw new phMagickException ('Plugin "'.$name.'" not found!');
  127. return false;
  128. }
  129. //-----------------
  130. private function loadPlugins(){
  131. $base = dirname(__FILE__) . '/plugins';
  132. $plugins = glob($base . '/*.php');
  133. foreach($plugins as $plugin){
  134. include_once $plugin ;
  135. $name = basename($plugin, '.php');
  136. $className = 'phMagick_'.$name ;
  137. $obj = new $className();
  138. $this->loadedPlugins[$name] = $obj ;
  139. foreach (get_class_methods($obj) as $method )
  140. $this->availableMethods[$method] = $name ;
  141. }
  142. }
  143. public function execute($cmd){
  144. $ret = null ;
  145. $out = array();
  146. if($this->escapeChars) {
  147. $cmd= str_replace ('(','\(',$cmd);
  148. $cmd= str_replace (')','\)',$cmd);
  149. }
  150. exec( $cmd .' 2>&1', $out, $ret);
  151. if($ret != 0)
  152. if($this->debug) trigger_error (new phMagickException ('Error executing "'. $cmd.'" <br>return code: '. $ret .' <br>command output :"'. implode("<br>", $out).'"' ), E_USER_NOTICE );
  153. $this->log[] = array(
  154. 'cmd' => $cmd
  155. ,'return' => $ret
  156. ,'output' => $out
  157. );
  158. return $ret ;
  159. }
  160. public function __call($method, $args){
  161. if(! key_exists($method, $this->availableMethods))
  162. throw new Exception ('Call to undefined method : ' . $method);
  163. array_unshift($args, $this);
  164. $ret = call_user_func_array(array($this->loadedPlugins[$this->availableMethods[$method]], $method), $args);
  165. if($ret === false)
  166. throw new Exception ('Error executing method "' . $method ."'");
  167. return $ret ;
  168. }
  169. }
  170. class phMagickHistory{
  171. const returnArray = 0 ;
  172. const returnCsv = 1 ;
  173. private function __construct(){}
  174. }
  175. class phMagickException extends Exception {
  176. function __construct($message, $code=1){
  177. //parent::__construct('', $code);
  178. $this->message($message);
  179. }
  180. function message($message){
  181. echo '<br><b>phMagick</b>: ' . $message ;
  182. }
  183. }
  184. class phMagickGravity{
  185. const None = 'None' ;
  186. const Center = 'Center' ;
  187. const East = 'East' ;
  188. const Forget = 'Forget' ;
  189. const NorthEast = 'NorthEast' ;
  190. const North = 'North' ;
  191. const NorthWest = 'NorthWest' ;
  192. const SouthEast = 'SouthEast' ;
  193. const South = 'South' ;
  194. const SouthWest = 'SouthWest' ;
  195. const West = 'West' ;
  196. private function __construct(){}
  197. }
  198. class phMagickTextObjectDefaults{
  199. public static $fontSize ='12';
  200. public static $font = false;
  201. public static $color = '#000';
  202. public static $background = false;
  203. public static $gravity = phMagickGravity::Center; //ignored in fromString()
  204. public $Text = '';
  205. private function __construct(){}
  206. }
  207. class phMagickTextObject {
  208. protected $fontSize;
  209. protected $font;
  210. protected $color;
  211. protected $background;
  212. protected $pGravity; //ignored in fromString()
  213. protected $pText = '';
  214. public function __construct(){
  215. $this->fontSize = phMagickTextObjectDefaults::$fontSize;
  216. $this->font = phMagickTextObjectDefaults::$font;
  217. $this->color = phMagickTextObjectDefaults::$color ;
  218. $this->background = phMagickTextObjectDefaults::$background;
  219. $this->pGravity = phMagickTextObjectDefaults::$gravity;
  220. }
  221. function defaultFontSize($value){
  222. phMagickTextObjectDefaults::$fontSize = $value;
  223. }
  224. function defaultFont($value){
  225. phMagickTextObjectDefaults::$font = $value;
  226. }
  227. function defaultColor($value){
  228. phMagickTextObjectDefaults::$color = $value;
  229. }
  230. function defaultBackground($value){
  231. phMagickTextObjectDefaults::$background = $value;
  232. }
  233. function defaultGravity($value){
  234. phMagickTextObjectDefaults::$gravity = $value;
  235. }
  236. function fontSize($i){
  237. $this->fontSize = $i ;
  238. return $this;
  239. }
  240. function font($i){
  241. $this->font = $i ;
  242. return $this;
  243. }
  244. function color($i){
  245. $this->color = $i ;
  246. return $this;
  247. }
  248. function background($i){
  249. $this->background = $i ;
  250. return $this;
  251. }
  252. function __get($var){
  253. return $this->$var ;
  254. }
  255. function gravity( $gravity){
  256. $this->pGravity = $gravity;
  257. return $this ;
  258. }
  259. function text( $text){
  260. $this->pText = $text;
  261. return $this ;
  262. }
  263. }
  264. ?>