Source for file ImageMagick.php
Documentation is available at ImageMagick.php
6: * @copyright Copyright (c) 2004, 2010 David Grudl
7: * @license http://nettephp.com/license Nette license
8: * @link http://nettephp.com
16: * Manipulation with large images using ImageMagick.
19: * $image = Image::fromFile('bigphoto.jpg');
20: * $image->resize(150, 100);
25: * @copyright Copyright (c) 2004, 2010 David Grudl
30: /** @var string path to ImageMagick library */
31: public static $path =
'';
34: public static $tempDir;
40: private $isTemporary =
FALSE;
52: * @param string detected image format
58: throw new InvalidArgumentException("File '$file' not found.");
61: if ($format ===
'JPEG') $format =
self::JPEG;
62: elseif ($format ===
'PNG') $format =
self::PNG;
63: elseif ($format ===
'GIF') $format =
self::GIF;
69: * Returns image width.
74: return $this->file ===
NULL ?
parent::getWidth() :
$this->width;
80: * Returns image height.
85: return $this->file ===
NULL ?
parent::getHeight() :
$this->height;
91: * Returns image GD resource.
96: if ($this->file !==
NULL) {
97: if (!$this->isTemporary) {
98: $this->execute("convert -strip %input %output", self::PNG);
101: if ($this->isTemporary) {
114: * @param mixed width in pixels or percent
115: * @param mixed height in pixels or percent
117: * @return ImageMagick provides a fluent interface
119: public function resize($width, $height, $flags =
self::FIT)
121: if ($this->file ===
NULL) {
122: return parent::resize($newWidth, $newHeight, $flags);
126: if ($width <
0) $mirror .=
' -flop';
127: if ($height <
0) $mirror .=
' -flip';
128: list($newWidth, $newHeight) =
self::calculateSize($this->getWidth(), $this->getHeight(), $width, $height, $flags);
129: $this->execute("convert -resize {$newWidth}x{$newHeight}! {$mirror} -strip %input %output", self::PNG);
137: * @param int x-coordinate
138: * @param int y-coordinate
141: * @return ImageMagick provides a fluent interface
143: public function crop($left, $top, $width, $height)
145: if ($this->file ===
NULL) {
146: return parent::crop($left, $top, $width, $height);
153: $this->execute("convert -crop {$width}x{$height}+{$left}+{$top} -strip %input %output", self::PNG);
160: * Saves image to the file.
161: * @param string filename
162: * @param int quality 0..100 (for JPEG and PNG)
163: * @param int optional image type
164: * @return bool TRUE on success or FALSE on failure.
166: public function save($file =
NULL, $quality =
NULL, $type =
NULL)
168: if ($this->file ===
NULL) {
169: return parent::save($file, $quality, $type);
172: $quality =
$quality ===
NULL ?
'' :
'-quality ' .
max(0, min(100, (int)
$quality));
173: if ($file ===
NULL) {
174: $this->execute("convert $quality -strip %input %output", $type ===
NULL ?
self::PNG :
$type);
178: $this->execute("convert $quality -strip %input %output", (string)
$file);
186: * Change and identify image file.
187: * @param string filename
188: * @return string detected image format
190: private function setFile($file)
192: $this->file =
$file;
195: throw new Exception("Unknown image type in file '$file' or ImageMagick not available.");
197: list($this->width, $this->height, $format) =
explode(',', $res, 3);
205: * @param string command
206: * @param string|bool process output?
209: private function execute($command, $output =
NULL)
220: exec(self::$path .
$command, $lines, $status); // $status: 0 - ok, 1 - error, 127 - command not found?
224: throw new Exception("Unknown error while calling ImageMagick.");
226: if ($this->isTemporary) {
229: $this->setFile($newFile);
233: return $lines ?
$lines[0] :
FALSE;
239: * Delete temporary files.
244: if ($this->file !==
NULL &&
$this->isTemporary) {