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 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: * Basic manipulation with images.
31: * $image = Image::fromFile('nette.jpg');
32: * $image->resize(150, 100);
37: * @author David Grudl
38: * @copyright Copyright (c) 2004, 2009 David Grudl
43: /**#@+ resizing flags {@link resize()} */
48: /**#@+ @int image types {@link send()} */
50: const PNG =
IMAGETYPE_PNG;
51: const GIF =
IMAGETYPE_GIF;
54: 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;";
57: public static $useImageMagick =
FALSE;
66: * @param int red 0..255
67: * @param int green 0..255
68: * @param int blue 0..255
69: * @param int transparency 0..127
72: public static function rgb($red, $green, $blue, $transparency =
0)
76: 'green' =>
max(0, min(255, (int)
$green)),
78: 'alpha' =>
max(0, min(127, (int)
$transparency)),
85: * Opens image from file.
87: * @param mixed detected image format
90: public static function fromFile($file, & $format =
NULL)
93: throw new Exception("PHP extension GD is not loaded.");
97: if (self::$useImageMagick &&
(empty($info) ||
$info[0] *
$info[1] >
2e6)) {
98: return new ImageMagick($file, $format);
101: switch ($format =
$info[2]) {
112: if (self::$useImageMagick) {
113: return new ImageMagick($file, $format);
115: throw new Exception("Unknown image type in file '$file'.");
122: * Create a new image from the image stream in the string.
134: * Creates blank image.
140: public static function fromBlank($width, $height, $color =
NULL)
143: throw new Exception("PHP extension GD is not loaded.");
146: $width = (int)
$width;
147: $height = (int)
$height;
148: if ($width <
1 ||
$height <
1) {
149: throw new InvalidArgumentException('Image width and height must be greater than zero.');
157: return new self($image);
174: * Returns image width.
185: * Returns image height.
196: * Sets image resource.
203: throw new InvalidArgumentException('Image is not valid.');
205: $this->image =
$image;
211: * Returns image GD resource.
216: return $this->image;
223: * @param mixed width in pixels or percent
224: * @param mixed height in pixels or percent
226: * @return Image provides a fluent interface
228: public function resize($newWidth, $newHeight, $flags =
0)
230: list($newWidth, $newHeight) =
$this->calculateSize($newWidth, $newHeight, $flags);
233: $this->image =
$newImage;
240: * Calculates dimensions of resized image.
241: * @param mixed width in pixels or percent
242: * @param mixed height in pixels or percent
252: $newWidth =
round($width /
100 *
$newWidth);
253: $flags |=
self::ENLARGE;
256: $newWidth = (int)
$newWidth;
260: $newHeight =
round($height /
100 *
$newHeight);
261: $flags |=
empty($percents) ?
self::ENLARGE :
self::STRETCH;
263: $newHeight = (int)
$newHeight;
266: if ($flags & self::STRETCH) { // non-proportional
267: if ($newWidth <
1 ||
$newHeight <
1) {
268: throw new InvalidArgumentException('For stretching must be both width and height specified.');
271: if (($flags & self::ENLARGE) ===
0) {
272: $newWidth =
round($width *
min(1, $newWidth /
$width));
273: $newHeight =
round($height *
min(1, $newHeight /
$height));
276: } else { // proportional
277: if ($newWidth <
1 &&
$newHeight <
1) {
278: throw new InvalidArgumentException('At least width or height must be specified.');
282: if ($newWidth >
0) { // fit width
283: $scale[] =
$newWidth /
$width;
286: if ($newHeight >
0) { // fit height
287: $scale[] =
$newHeight /
$height;
290: if (($flags & self::ENLARGE) ===
0) {
299: return array($newWidth, $newHeight);
306: * @param int x-coordinate
307: * @param int y-coordinate
310: * @return Image provides a fluent interface
312: public function crop($left, $top, $width, $height)
321: $this->image =
$newImage;
329: * @return Image provides a fluent interface
334: array( -
1, -
1, -
1 ),
335: array( -
1, 24, -
1 ),
336: array( -
1, -
1, -
1 ),
344: * Puts another image into this image.
346: * @param mixed x-coordinate in pixels or percent
347: * @param mixed y-coordinate in pixels or percent
348: * @param int opacity 0..100
349: * @return Image provides a fluent interface
351: public function place(Image $image, $left =
0, $top =
0, $opacity =
100)
363: if ($opacity ===
100) {
366: } elseif ($opacity <>
0) {
375: * Saves image to the file.
376: * @param string filename
377: * @param int quality 0..100 (for JPEG and PNG)
378: * @param int optional image type
379: * @return bool TRUE on success or FALSE on failure.
381: public function save($file =
NULL, $quality =
NULL, $type =
NULL)
383: if ($type ===
NULL) {
399: $quality =
$quality ===
NULL ?
85 :
max(0, min(100, (int)
$quality));
403: $quality =
$quality ===
NULL ?
9 :
max(0, min(9, (int)
$quality));
410: throw new Exception("Unsupported image type.");
417: * Outputs image to string.
418: * @param int image type
419: * @param int quality 0..100 (for JPEG and PNG)
422: public function toString($type =
self::JPEG, $quality =
NULL)
425: $this->save(NULL, $quality, $type);
432: * Outputs image to string.
440: } catch (Exception $e) {
449: * Outputs image to browser.
450: * @param int image type
451: * @param int quality 0..100 (for JPEG and PNG)
452: * @return bool TRUE on success or FALSE on failure.
454: public function send($type =
self::JPEG, $quality =
NULL)
456: if ($type !==
self::GIF &&
$type !==
self::PNG &&
$type !==
self::JPEG) {
457: throw new Exception("Unsupported image type.");
460: return $this->save(NULL, $quality, $type);
466: * Call to undefined method.
468: * @param string method name
469: * @param array arguments
471: * @throws MemberAccessException
475: $function =
'image' .
$name;
477: foreach ($args as $key =>
$value) {
478: if ($value instanceof
self) {
479: $args[$key] =
$value->getImageResource();
481: } elseif (is_array($value) &&
isset($value['red'])) { // rgb