Source for file ImageMagick.php

Documentation is available at ImageMagick.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/Object.php';
  24. 24:  
  25. 25:  
  26. 26:  
  27. 27: /**
  28. 28:  * Manipulation with large images using ImageMagick.
  29. 29:  *
  30. 30:  * <code>
  31. 31:  * $image = Image::fromFile('bigphoto.jpg');
  32. 32:  * $image->resize(150, 100);
  33. 33:  * $image->sharpen();
  34. 34:  * $image->send();
  35. 35:  * </code>
  36. 36:  *
  37. 37:  * @author     David Grudl
  38. 38:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  39. 39:  * @package    Nette
  40. 40:  */
  41. 41: class ImageMagick extends Image
  42. 42: {
  43. 43:     /** @var string  path to ImageMagick library */
  44. 44:     static public $path = '';
  45. 45:  
  46. 46:     /** @var string */
  47. 47:     static public $tempDir;
  48. 48:  
  49. 49:     /** @var string */
  50. 50:     private $file;
  51. 51:  
  52. 52:     /** @var bool */
  53. 53:     private $isTemporary FALSE;
  54. 54:  
  55. 55:     /** @var int */
  56. 56:     private $width;
  57. 57:  
  58. 58:     /** @var int */
  59. 59:     private $height;
  60. 60:  
  61. 61:  
  62. 62:  
  63. 63:     /**
  64. 64:      * Wraps image file.
  65. 65:      * @param  string  detected image format
  66. 66:      * @param  string 
  67. 67:      */
  68. 68:     public function __construct($file$format NULL)
  69. 69:     {
  70. 70:         if (!is_file($file)) {
  71. 71:             throw new InvalidArgumentException('File not exists.');
  72. 72:         }
  73. 73:         $format $this->setFile(realpath($file));
  74. 74:         if ($format === 'JPEG'$format self::JPEG;
  75. 75:         elseif ($format === 'PNG'$format self::PNG;
  76. 76:         elseif ($format === 'GIF'$format self::GIF;
  77. 77:     }
  78. 78:  
  79. 79:  
  80. 80:  
  81. 81:     /**
  82. 82:      * Returns image width.
  83. 83:      * @return int 
  84. 84:      */
  85. 85:     public function getWidth()
  86. 86:     {
  87. 87:         return $this->file === NULL parent::getWidth($this->width;
  88. 88:     }
  89. 89:  
  90. 90:  
  91. 91:  
  92. 92:     /**
  93. 93:      * Returns image height.
  94. 94:      * @return int 
  95. 95:      */
  96. 96:     public function getHeight()
  97. 97:     {
  98. 98:         return $this->file === NULL parent::getHeight($this->height;
  99. 99:     }
  100. 100:  
  101. 101:  
  102. 102:  
  103. 103:     /**
  104. 104:      * Returns image GD resource.
  105. 105:      * @return resource 
  106. 106:      */
  107. 107:     public function getImageResource()
  108. 108:     {
  109. 109:         if ($this->file !== NULL{
  110. 110:             if (!$this->isTemporary{
  111. 111:                 $this->execute("convert -strip %input %output"self::PNG);
  112. 112:             }
  113. 113:             $this->setImageResource(imagecreatefrompng($this->file));
  114. 114:             if ($this->isTemporary{
  115. 115:                 unlink($this->file);
  116. 116:             }
  117. 117:             $this->file NULL;
  118. 118:         }
  119. 119:  
  120. 120:         return parent::getImageResource();
  121. 121:     }
  122. 122:  
  123. 123:  
  124. 124:  
  125. 125:     /**
  126. 126:      * Resizes image.
  127. 127:      * @param  mixed  width in pixels or percent
  128. 128:      * @param  mixed  height in pixels or percent
  129. 129:      * @param  int    flags
  130. 130:      * @return ImageMagick  provides a fluent interface
  131. 131:      */
  132. 132:     public function resize($newWidth$newHeight$flags 0)
  133. 133:     {
  134. 134:         if ($this->file === NULL{
  135. 135:             return parent::resize($newWidth$newHeight$flags);
  136. 136:         }
  137. 137:  
  138. 138:         list($newWidth$newHeight$this->calculateSize($newWidth$newHeight$flags);
  139. 139:         $this->execute("convert -resize {$newWidth}x{$newHeight}! -strip %input %output"self::PNG);
  140. 140:         return $this;
  141. 141:     }
  142. 142:  
  143. 143:  
  144. 144:  
  145. 145:     /**
  146. 146:      * Crops image.
  147. 147:      * @param  int  x-coordinate
  148. 148:      * @param  int  y-coordinate
  149. 149:      * @param  int  width
  150. 150:      * @param  int  height
  151. 151:      * @return ImageMagick  provides a fluent interface
  152. 152:      */
  153. 153:     public function crop($left$top$width$height)
  154. 154:     {
  155. 155:         if ($this->file === NULL{
  156. 156:             return parent::crop($left$top$width$height);
  157. 157:         }
  158. 158:  
  159. 159:         $left max(0(int) $left);
  160. 160:         $top max(0(int) $top);
  161. 161:         $width min((int) $width$this->getWidth($left);
  162. 162:         $height min((int) $height$this->getHeight($top);
  163. 163:         $this->execute("convert -crop \"{$width}x{$height}+{$left}+{$top}\" -strip %input %output"self::PNG);
  164. 164:         return $this;
  165. 165:     }
  166. 166:  
  167. 167:  
  168. 168:  
  169. 169:     /**
  170. 170:      * Saves image to the file.
  171. 171:      * @param  string  filename
  172. 172:      * @param  int  quality 0..100 (for JPEG and PNG)
  173. 173:      * @param  int  optional image type
  174. 174:      * @return bool TRUE on success or FALSE on failure.
  175. 175:      */
  176. 176:     public function save($file NULL$quality NULL$type NULL)
  177. 177:     {
  178. 178:         if ($this->file === NULL{
  179. 179:             return parent::save($file$quality$type);
  180. 180:         }
  181. 181:  
  182. 182:         $quality $quality === NULL '' '-quality ' max(0min(100(int) $quality));
  183. 183:         if ($file === NULL{
  184. 184:             $this->execute("convert $quality -strip %input %output"$type === NULL self::PNG $type);
  185. 185:             readfile($this->file);
  186. 186:  
  187. 187:         else {
  188. 188:             $this->execute("convert $quality -strip %input %output"(string) $file);
  189. 189:         }
  190. 190:         return TRUE;
  191. 191:     }
  192. 192:  
  193. 193:  
  194. 194:  
  195. 195:     /**
  196. 196:      * Change and identify image file.
  197. 197:      * @param  string  filename
  198. 198:      * @return string  detected image format
  199. 199:      */
  200. 200:     private function setFile($file)
  201. 201:     {
  202. 202:         $this->file $file;
  203. 203:         $res $this->execute('identify -format "%w,%h,%m" ' addcslashes($this->file' '));
  204. 204:         if (!$res{
  205. 205:             throw new Exception("Unknown image type in file '$file' or ImageMagick not available.");
  206. 206:         }
  207. 207:         list($this->width$this->height$formatexplode(','$res3);
  208. 208:         return $format;
  209. 209:     }
  210. 210:  
  211. 211:  
  212. 212:  
  213. 213:     /**
  214. 214:      * Executes command.
  215. 215:      * @param  string  command
  216. 216:      * @param  string|bool process output?
  217. 217:      * @return string 
  218. 218:      */
  219. 219:     private function execute($command$output NULL)
  220. 220:     {
  221. 221:         $command str_replace('%input'addcslashes($this->file' ')$command);
  222. 222:         if ($output{
  223. 223:             $newFile is_string($output)
  224. 224:                 ? $output
  225. 225:                 : (self::$tempDir self::$tempDir dirname($this->file)) '/_tempimage' uniqid(image_type_to_extension($output);
  226. 226:             $command str_replace('%output'addcslashes($newFile' ')$command);
  227. 227:         }
  228. 228:  
  229. 229:         $lines array();
  230. 230:         exec(self::$path $command$lines$status)// $status: 0 - ok, 1 - error, 127 - command not found?
  231. 231:  
  232. 232:         if ($output{
  233. 233:             if ($status != 0{
  234. 234:                 throw new Exception("Unknown error while calling ImageMagick.");
  235. 235:             }
  236. 236:             if ($this->isTemporary{
  237. 237:                 unlink($this->file);
  238. 238:             }
  239. 239:             $this->setFile($newFile);
  240. 240:             $this->isTemporary !is_string($output);
  241. 241:         }
  242. 242:  
  243. 243:         return $lines $lines[0FALSE;
  244. 244:     }
  245. 245:  
  246. 246:  
  247. 247:  
  248. 248:     /**
  249. 249:      * Delete temporary files.
  250. 250:      * @return void 
  251. 251:      */
  252. 252:     public function __destruct()
  253. 253:     {
  254. 254:         if ($this->file !== NULL && $this->isTemporary{
  255. 255:             unlink($this->file);
  256. 256:         }
  257. 257:     }
  258. 258: