Source for file ImageMagick.php
Documentation is available at ImageMagick.php
6: * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
8: * This source file is subject to the "Nette license" that is bundled
9: * with this package in the file license.txt.
11: * For more information please see http://nettephp.com
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license http://nettephp.com/license Nette license
15: * @link http://nettephp.com
22: require_once dirname(__FILE__) .
'/Object.php';
27: * Manipulation with large images using ImageMagick.
30: * $image = Image::fromFile('bigphoto.jpg');
31: * $image->resize(150, 100);
36: * @author David Grudl
37: * @copyright Copyright (c) 2004, 2009 David Grudl
42: /** @var string path to ImageMagick library */
43: public static $path =
'';
46: public static $tempDir;
52: private $isTemporary =
FALSE;
64: * @param string detected image format
70: throw new InvalidArgumentException('File not found.');
73: if ($format ===
'JPEG') $format =
self::JPEG;
74: elseif ($format ===
'PNG') $format =
self::PNG;
75: elseif ($format ===
'GIF') $format =
self::GIF;
81: * Returns image width.
86: return $this->file ===
NULL ?
parent::getWidth() :
$this->width;
92: * Returns image height.
97: return $this->file ===
NULL ?
parent::getHeight() :
$this->height;
103: * Returns image GD resource.
108: if ($this->file !==
NULL) {
109: if (!$this->isTemporary) {
110: $this->execute("convert -strip %input %output", self::PNG);
113: if ($this->isTemporary) {
126: * @param mixed width in pixels or percent
127: * @param mixed height in pixels or percent
129: * @return ImageMagick provides a fluent interface
131: public function resize($newWidth, $newHeight, $flags =
0)
133: if ($this->file ===
NULL) {
134: return parent::resize($newWidth, $newHeight, $flags);
137: list($newWidth, $newHeight) =
$this->calculateSize($newWidth, $newHeight, $flags);
138: $this->execute("convert -resize {$newWidth}x{$newHeight}! -strip %input %output", self::PNG);
146: * @param int x-coordinate
147: * @param int y-coordinate
150: * @return ImageMagick provides a fluent interface
152: public function crop($left, $top, $width, $height)
154: if ($this->file ===
NULL) {
155: return parent::crop($left, $top, $width, $height);
162: $this->execute("convert -crop {$width}x{$height}+{$left}+{$top} -strip %input %output", self::PNG);
169: * Saves image to the file.
170: * @param string filename
171: * @param int quality 0..100 (for JPEG and PNG)
172: * @param int optional image type
173: * @return bool TRUE on success or FALSE on failure.
175: public function save($file =
NULL, $quality =
NULL, $type =
NULL)
177: if ($this->file ===
NULL) {
178: return parent::save($file, $quality, $type);
181: $quality =
$quality ===
NULL ?
'' :
'-quality ' .
max(0, min(100, (int)
$quality));
182: if ($file ===
NULL) {
183: $this->execute("convert $quality -strip %input %output", $type ===
NULL ?
self::PNG :
$type);
187: $this->execute("convert $quality -strip %input %output", (string)
$file);
195: * Change and identify image file.
196: * @param string filename
197: * @return string detected image format
199: private function setFile($file)
201: $this->file =
$file;
204: throw new Exception("Unknown image type in file '$file' or ImageMagick not available.");
206: list($this->width, $this->height, $format) =
explode(',', $res, 3);
214: * @param string command
215: * @param string|bool process output?
218: private function execute($command, $output =
NULL)
229: exec(self::$path .
$command, $lines, $status); // $status: 0 - ok, 1 - error, 127 - command not found?
233: throw new Exception("Unknown error while calling ImageMagick.");
235: if ($this->isTemporary) {
238: $this->setFile($newFile);
242: return $lines ?
$lines[0] :
FALSE;
248: * Delete temporary files.
253: if ($this->file !==
NULL &&
$this->isTemporary) {