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 = Image::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 ImageMagick($file, $format);
104: switch ($format =
$info[2]) {
115: if (self::$useImageMagick) {
116: return new ImageMagick($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.
137: * Creates blank image.
143: public static function fromBlank($width, $height, $color =
NULL)
146: throw new Exception("PHP extension GD is not loaded.");
149: $width = (int)
$width;
150: $height = (int)
$height;
151: if ($width <
1 ||
$height <
1) {
152: throw new InvalidArgumentException('Image width and height must be greater than zero.');
160: return new self($image);
177: * Returns image width.
188: * Returns image height.
199: * Sets image resource.
206: throw new InvalidArgumentException('Image is not valid.');
208: $this->image =
$image;
214: * Returns image GD resource.
219: return $this->image;
226: * @param mixed width in pixels or percent
227: * @param mixed height in pixels or percent
229: * @return Image provides a fluent interface
231: public function resize($newWidth, $newHeight, $flags =
0)
233: list($newWidth, $newHeight) =
$this->calculateSize($newWidth, $newHeight, $flags);
236: $this->image =
$newImage;
243: * Calculates dimensions of resized image.
244: * @param mixed width in pixels or percent
245: * @param mixed height in pixels or percent
255: $newWidth =
round($width /
100 *
$newWidth);
256: $flags |=
self::ENLARGE;
259: $newWidth = (int)
$newWidth;
263: $newHeight =
round($height /
100 *
$newHeight);
264: $flags |=
empty($percents) ?
self::ENLARGE :
self::STRETCH;
266: $newHeight = (int)
$newHeight;
269: if ($flags & self::STRETCH) { // non-proportional
270: if ($newWidth <
1 ||
$newHeight <
1) {
271: throw new InvalidArgumentException('For stretching must be both width and height specified.');
274: if (($flags & self::ENLARGE) ===
0) {
275: $newWidth =
round($width *
min(1, $newWidth /
$width));
276: $newHeight =
round($height *
min(1, $newHeight /
$height));
279: } else { // proportional
280: if ($newWidth <
1 &&
$newHeight <
1) {
281: throw new InvalidArgumentException('At least width or height must be specified.');
285: if ($newWidth >
0) { // fit width
286: $scale[] =
$newWidth /
$width;
289: if ($newHeight >
0) { // fit height
290: $scale[] =
$newHeight /
$height;
293: if (($flags & self::ENLARGE) ===
0) {
302: return array($newWidth, $newHeight);
309: * @param int x-coordinate
310: * @param int y-coordinate
313: * @return Image provides a fluent interface
315: public function crop($left, $top, $width, $height)
324: $this->image =
$newImage;
332: * @return Image provides a fluent interface
337: array( -
1, -
1, -
1 ),
338: array( -
1, 24, -
1 ),
339: array( -
1, -
1, -
1 ),
347: * Puts another image into this image.
349: * @param mixed x-coordinate in pixels or percent
350: * @param mixed y-coordinate in pixels or percent
351: * @param int opacity 0..100
352: * @return Image provides a fluent interface
354: public function place(Image $image, $left =
0, $top =
0, $opacity =
100)
366: if ($opacity ===
100) {
369: } elseif ($opacity <>
0) {
378: * Saves image to the file.
379: * @param string filename
380: * @param int quality 0..100 (for JPEG and PNG)
381: * @param int optional image type
382: * @return bool TRUE on success or FALSE on failure.
384: public function save($file =
NULL, $quality =
NULL, $type =
NULL)
386: if ($type ===
NULL) {
402: $quality =
$quality ===
NULL ?
85 :
max(0, min(100, (int)
$quality));
406: $quality =
$quality ===
NULL ?
9 :
max(0, min(9, (int)
$quality));
413: throw new Exception("Unsupported image type.");
420: * Outputs image to string.
421: * @param int image type
422: * @param int quality 0..100 (for JPEG and PNG)
425: public function toString($type =
self::JPEG, $quality =
NULL)
428: $this->save(NULL, $quality, $type);
435: * Outputs image to string.
443: } catch (Exception $e) {
452: * Outputs image to browser.
453: * @param int image type
454: * @param int quality 0..100 (for JPEG and PNG)
455: * @return bool TRUE on success or FALSE on failure.
457: public function send($type =
self::JPEG, $quality =
NULL)
459: if ($type !==
self::GIF &&
$type !==
self::PNG &&
$type !==
self::JPEG) {
460: throw new Exception("Unsupported image type.");
463: return $this->save(NULL, $quality, $type);
469: * Call to undefined method.
471: * @param string method name
472: * @param array arguments
474: * @throws MemberAccessException
478: $function =
'image' .
$name;
480: foreach ($args as $key =>
$value) {
481: if ($value instanceof
self) {
482: $args[$key] =
$value->getImageResource();
484: } elseif (is_array($value) &&
isset($value['red'])) { // rgb