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