1 <?php
2
3 /*
4 * This file is part of the Icybee package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Icybee\Modules\Images;
13
14 /**
15 * Representation of a managed image.
16 *
17 * @property-read int $surface The surface of the image, computed from the {@link $width} and
18 * {@link $height} properties.
19 *
20 * @property-read \Icybee\Modules\Nodes\Node $consumer The node to which the image is associated.
21 *
22 * @method \ICanBoogie\Modules\Thumbnailer\Thumbnail thumbnail()
23 */
24 class Image extends \Icybee\Modules\Files\File
25 {
26 const WIDTH = 'width';
27 const HEIGHT = 'height';
28 const ALT = 'alt';
29
30 /**
31 * Width of the image in pixels.
32 *
33 * @var int
34 */
35 public $width;
36
37 /**
38 * Height of the image in pixels.
39 *
40 * @var int
41 */
42 public $height;
43
44 /**
45 * Alternative text, used when the image cannot be displayed.
46 *
47 * @var string
48 */
49 public $alt = '';
50
51 /**
52 * Defaults the model to "images".
53 *
54 * @param string $model
55 */
56 public function __construct($model='images')
57 {
58 parent::__construct($model);
59 }
60
61 /**
62 * Returns an `IMG` element.
63 *
64 * @return string
65 */
66 public function __toString()
67 {
68 $path = \ICanBoogie\escape($this->path);
69 $alt = \ICanBoogie\escape($this->alt);
70
71 return <<<EOT
72 <img src="$path" alt="$alt" width="{$this->width}" height="{$this->height}" data-nid="{$this->nid}" />
73 EOT;
74 }
75
76 public function save()
77 {
78 if (isset($this->{ self::HTTP_FILE }))
79 {
80 /* @var $file \ICanBoogie\HTTP\File */
81
82 $file = $this->{ self::HTTP_FILE };
83
84 list($w, $h) = getimagesize($file->pathname);
85
86 $this->width = $w;
87 $this->height = $h;
88 }
89
90 return parent::save();
91 }
92
93 /**
94 * Returns the surface of the image, computed from the {@link $width} and {@link $height}
95 * properties.
96 *
97 * @return int
98 */
99 protected function get_surface()
100 {
101 return $this->width * $this->height;
102 }
103 }