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 https://nette.org
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license https://nette.org/license Nette license
15: * @link https://nette.org
23: require_once dirname(__FILE__) .
'/Object.php';
28: * Manipulation with large images using ImageMagick.
31: * $image = Image::fromFile('bigphoto.jpg');
32: * $image->resize(150, 100);
37: * @author David Grudl
38: * @copyright Copyright (c) 2004, 2009 David Grudl
43: /** @var string path to ImageMagick library */
53: private $isTemporary =
FALSE;
65: * @param string detected image format
71: throw new InvalidArgumentException('File not exists.');
74: if ($format ===
'JPEG') $format =
self::JPEG;
75: elseif ($format ===
'PNG') $format =
self::PNG;
76: elseif ($format ===
'GIF') $format =
self::GIF;
82: * Returns image width.
87: return $this->file ===
NULL ?
parent::getWidth() :
$this->width;
93: * Returns image height.
98: return $this->file ===
NULL ?
parent::getHeight() :
$this->height;
104: * Returns image GD resource.
109: if ($this->file !==
NULL) {
110: if (!$this->isTemporary) {
111: $this->execute("convert -strip %input %output", self::PNG);
114: if ($this->isTemporary) {
127: * @param mixed width in pixels or percent
128: * @param mixed height in pixels or percent
130: * @return ImageMagick provides a fluent interface
132: public function resize($newWidth, $newHeight, $flags =
0)
134: if ($this->file ===
NULL) {
135: return parent::resize($newWidth, $newHeight, $flags);
138: list($newWidth, $newHeight) =
$this->calculateSize($newWidth, $newHeight, $flags);
139: $this->execute("convert -resize {$newWidth}x{$newHeight}! -strip %input %output", self::PNG);
147: * @param int x-coordinate
148: * @param int y-coordinate
151: * @return ImageMagick provides a fluent interface
153: public function crop($left, $top, $width, $height)
155: if ($this->file ===
NULL) {
156: return parent::crop($left, $top, $width, $height);
163: $this->execute("convert -crop \"{$width}x{$height}+{$left}+{$top}\" -strip %input %output", self::PNG);
170: * Saves image to the file.
171: * @param string filename
172: * @param int quality 0..100 (for JPEG and PNG)
173: * @param int optional image type
174: * @return bool TRUE on success or FALSE on failure.
176: public function save($file =
NULL, $quality =
NULL, $type =
NULL)
178: if ($this->file ===
NULL) {
179: return parent::save($file, $quality, $type);
182: $quality =
$quality ===
NULL ?
'' :
'-quality ' .
max(0, min(100, (int)
$quality));
183: if ($file ===
NULL) {
184: $this->execute("convert $quality -strip %input %output", $type ===
NULL ?
self::PNG :
$type);
188: $this->execute("convert $quality -strip %input %output", (string)
$file);
196: * Change and identify image file.
197: * @param string filename
198: * @return string detected image format
200: private function setFile($file)
202: $this->file =
$file;
203: $res =
$this->execute('identify -format "%w,%h,%m" ' .
addcslashes($this->file, ' '));
205: throw new Exception("Unknown image type in file '$file' or ImageMagick not available.");
207: list($this->width, $this->height, $format) =
explode(',', $res, 3);
215: * @param string command
216: * @param string|bool process output?
219: private function execute($command, $output =
NULL)
230: exec(self::$path .
$command, $lines, $status); // $status: 0 - ok, 1 - error, 127 - command not found?
234: throw new Exception("Unknown error while calling ImageMagick.");
236: if ($this->isTemporary) {
239: $this->setFile($newFile);
243: return $lines ?
$lines[0] :
FALSE;
249: * Delete temporary files.
254: if ($this->file !==
NULL &&
$this->isTemporary) {