Packages

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • NArrayTools
  • NCallback
  • NComponent
  • NComponentContainer
  • NConfigurator
  • NDateTime53
  • NDebug
  • NEnvironment
  • NFramework
  • NFreezableObject
  • NGenericRecursiveIterator
  • NImage
  • NImageMagick
  • NInstanceFilterIterator
  • NObject
  • NObjectMixin
  • NPaginator
  • NRecursiveComponentIterator
  • NServiceLocator
  • NSmartCachingIterator
  • NString
  • NTools

Interfaces

  • IComponent
  • IComponentContainer
  • IDebuggable
  • IServiceLocator
  • ITranslator

Exceptions

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