1: <?php
2:
3: namespace Zippy\Html;
4:
5: /**
6: * Компонент для тэга &lt;IMG&gt;
7: *
8: */
9: class Image extends HtmlComponent implements \Zippy\Interfaces\Requestable, \Zippy\Interfaces\AjaxRender
10: {
11: public static $DEFAULT_TYPE = 0;
12: public static $URLDATA_TYPE = 1;
13: public static $DYNAMIC_TYPE = 2;
14: public $src;
15: public $title;
16: private $type = 0;
17: private $event = null;
18:
19: /**
20: * Конструктор
21: * @param string ID компонента
22: * @param string адрес изображения
23: */
24: public function __construct($id, $src = "") {
25: parent::__construct($id);
26: $this->src = $src;
27: }
28:
29: /**
30: * Тип подгрузки изображения
31: *
32: * @param mixed $type
33: * Возможные вариантыЖ
34: * Image::$DEFAULT_TYPE - обычный путь к файлу в атрибуте src - по умоляанию
35: * Image::$URLDATA_TYPE - URL Data
36: * Image::$DYNAMIC_TYPE - динамическое формирование изображение в методе binaryOutput
37: *
38: */
39: public function setType($type) {
40: $this->type = $type;
41: }
42:
43: /**
44: * адрес изображения
45: *
46: * @param mixed $src
47: */
48: public function setUrl($src) {
49: $this->src = $src;
50:
51: if(\Zippy\WebApplication::$app->getRequest()->isAjaxRequest()) {
52: $_src = $this->getAttribute('src');
53: $js = "$('#{$this->id}').attr('src','{$_src}')";
54:
55:
56: \Zippy\WebApplication::$app->getResponse()->addAjaxResponse($js) ;
57: }
58:
59: }
60:
61: /**
62: * Записывает значение src в зависимости от типа
63: * *
64: */
65: private function setSource() {
66: if ($this->type == self::$DEFAULT_TYPE) {
67: if (strlen($this->src) > 0) {
68: $this->setAttribute("src", $this->src);
69: }
70: }
71:
72: if ($this->type == self::$URLDATA_TYPE) {
73: $this->setAttribute("src", $this->getURLData());
74: }
75:
76: if ($this->type == self::$DYNAMIC_TYPE) {
77: $this->setAttribute("src", $this->owner->getURLNode() . "::" . $this->id . ':' . time() . '&binary=true');
78: }
79:
80:
81:
82:
83: }
84:
85: /**
86: * Кодирование URL Data
87: */
88: protected function getURLData() {
89: $data = base64_encode(file_get_contents($this->src));
90: $im = getimagesize($this->src);
91: $data = "data:" . $im['mime'] . ";base64," . $data;
92:
93: return $data;
94: }
95:
96: /**
97: * рендеринг
98: * @see Zippy\Html\HtmlComponent
99: */
100: protected function RenderImpl() {
101:
102: if (strlen($this->title) > 0) {
103: $this->setAttribute("title", $this->title);
104: }
105:
106: $this->setSource();
107: }
108:
109: /**
110: * Асинхронное обновление изображения
111: * @see \Zippy\Interfaces\AjaxRender
112: */
113: public function AjaxAnswer() {
114: $this->setSource();
115: $_src = $this->getAttribute('src');
116: return "$('#{$this->id}').attr('src','{$_src}')";
117: }
118:
119: /**
120: * Формирует бинарный выходной поток
121: * по умолчанию считывает имя файла с src
122: * переопределяется в классе наследннике для реализации собственного вывода
123: * Должна заканчиваться die
124: */
125: protected function binaryOutput() {
126: $data = file_get_contents($this->src);
127: $im = getimagesize($this->src);
128: header('Content-Length: ' . strlen($data));
129: header("Content-type: " . $im['mime']);
130: echo $data;
131: flush();
132: die;
133: }
134:
135: /**
136: * Обработчик запроса для бинарного вывода
137: * @see \Zippy\Interfaces\Requestable
138: */
139: public function RequestHandle() {
140: $this->binaryOutput();
141: }
142:
143: }
144: