Source for file Image.php
Documentation is available at Image.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: * Basic manipulation with images.
30: * $image = NImage::fromFile('nette.jpg');
31: * $image->resize(150, 100);
36: * @author David Grudl
37: * @copyright Copyright (c) 2004, 2009 David Grudl
40: * @property-read int $width
41: * @property-read int $height
42: * @property-read resource $imageResource
46: /**#@+ resizing flags {@link resize()} */
51: /**#@+ @int image types {@link send()} */
53: const PNG =
IMAGETYPE_PNG;
54: const GIF =
IMAGETYPE_GIF;
57: const EMPTY_GIF =
"GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;";
60: public static $useImageMagick =
FALSE;
69: * @param int red 0..255
70: * @param int green 0..255
71: * @param int blue 0..255
72: * @param int transparency 0..127
75: public static function rgb($red, $green, $blue, $transparency =
0)
79: 'green' =>
max(0, min(255, (int)
$green)),
81: 'alpha' =>
max(0, min(127, (int)
$transparency)),
88: * Opens image from file.
90: * @param mixed detected image format
93: public static function fromFile($file, & $format =
NULL)
96: throw new Exception("PHP extension GD is not loaded.");
100: if (self::$useImageMagick &&
(empty($info) ||
$info[0] *
$info[1] >
2e6)) {
101: return new NImageMagick($file, $format);
104: switch ($format =
$info[2]) {
115: if (self::$useImageMagick) {
116: return new NImageMagick($file, $format);
118: throw new Exception("Unknown image type or file '$file' not found.");
125: * Create a new image from the image stream in the string.
127: * @param mixed detected image format
133: $format =
self::JPEG;
136: $format =
self::PNG;
139: $format =
self::GIF;
150: * Creates blank image.
156: public static function fromBlank($width, $height, $color =
NULL)
159: throw new Exception("PHP extension GD is not loaded.");
162: $width = (int)
$width;
163: $height = (int)
$height;
164: if ($width <
1 ||
$height <
1) {
165: throw new InvalidArgumentException('NImage width and height must be greater than zero.');
170: $color +=
array('alpha' =>
0);
176: return new self($image);
193: * Returns image width.
204: * Returns image height.
215: * Sets image resource.
217: * @return NImage provides a fluent interface
222: throw new InvalidArgumentException('NImage is not valid.');
224: $this->image =
$image;
231: * Returns image GD resource.
236: return $this->image;
243: * @param mixed width in pixels or percent
244: * @param mixed height in pixels or percent
246: * @return NImage provides a fluent interface
248: public function resize($newWidth, $newHeight, $flags =
0)
250: list($newWidth, $newHeight) =
$this->calculateSize($newWidth, $newHeight, $flags);
251: $newImage =
self::fromBlank($newWidth, $newHeight, self::RGB(0, 0, 0, 127))->getImageResource();
253: $this->image =
$newImage;
260: * Calculates dimensions of resized image.
261: * @param mixed width in pixels or percent
262: * @param mixed height in pixels or percent
272: $newWidth =
round($width /
100 *
$newWidth);
273: $flags |=
self::ENLARGE;
276: $newWidth = (int)
$newWidth;
280: $newHeight =
round($height /
100 *
$newHeight);
281: $flags |=
empty($percents) ?
self::ENLARGE :
self::STRETCH;
283: $newHeight = (int)
$newHeight;
286: if ($flags & self::STRETCH) { // non-proportional
287: if ($newWidth <
1 ||
$newHeight <
1) {
288: throw new InvalidArgumentException('For stretching must be both width and height specified.');
291: if (($flags & self::ENLARGE) ===
0) {
292: $newWidth =
round($width *
min(1, $newWidth /
$width));
293: $newHeight =
round($height *
min(1, $newHeight /
$height));
296: } else { // proportional
297: if ($newWidth <
1 &&
$newHeight <
1) {
298: throw new InvalidArgumentException('At least width or height must be specified.');
302: if ($newWidth >
0) { // fit width
303: $scale[] =
$newWidth /
$width;
306: if ($newHeight >
0) { // fit height
307: $scale[] =
$newHeight /
$height;
310: if (($flags & self::ENLARGE) ===
0) {
319: return array($newWidth, $newHeight);
326: * @param int x-coordinate
327: * @param int y-coordinate
330: * @return NImage provides a fluent interface
332: public function crop($left, $top, $width, $height)
339: $newImage =
self::fromBlank($width, $height, self::RGB(0, 0, 0, 127))->getImageResource();
341: $this->image =
$newImage;
349: * @return NImage provides a fluent interface
354: array( -
1, -
1, -
1 ),
355: array( -
1, 24, -
1 ),
356: array( -
1, -
1, -
1 ),
364: * Puts another image into this image.
366: * @param mixed x-coordinate in pixels or percent
367: * @param mixed y-coordinate in pixels or percent
368: * @param int opacity 0..100
369: * @return NImage provides a fluent interface
371: public function place(NImage $image, $left =
0, $top =
0, $opacity =
100)
383: if ($opacity ===
100) {
386: } elseif ($opacity <>
0) {
395: * Saves image to the file.
396: * @param string filename
397: * @param int quality 0..100 (for JPEG and PNG)
398: * @param int optional image type
399: * @return bool TRUE on success or FALSE on failure.
401: public function save($file =
NULL, $quality =
NULL, $type =
NULL)
403: if ($type ===
NULL) {
419: $quality =
$quality ===
NULL ?
85 :
max(0, min(100, (int)
$quality));
423: $quality =
$quality ===
NULL ?
9 :
max(0, min(9, (int)
$quality));
430: throw new Exception("Unsupported image type.");
437: * Outputs image to string.
438: * @param int image type
439: * @param int quality 0..100 (for JPEG and PNG)
442: public function toString($type =
self::JPEG, $quality =
NULL)
445: $this->save(NULL, $quality, $type);
452: * Outputs image to string.
460: } catch (Exception $e) {
469: * Outputs image to browser.
470: * @param int image type
471: * @param int quality 0..100 (for JPEG and PNG)
472: * @return bool TRUE on success or FALSE on failure.
474: public function send($type =
self::JPEG, $quality =
NULL)
476: if ($type !==
self::GIF &&
$type !==
self::PNG &&
$type !==
self::JPEG) {
477: throw new Exception("Unsupported image type.");
480: return $this->save(NULL, $quality, $type);
486: * Call to undefined method.
488: * @param string method name
489: * @param array arguments
491: * @throws MemberAccessException
495: $function =
'image' .
$name;
497: foreach ($args as $key =>
$value) {
498: if ($value instanceof
self) {
499: $args[$key] =
$value->getImageResource();
501: } elseif (is_array($value) &&
isset($value['red'])) { // rgb