| 1: | <?php |
| 2: | |
| 3: | namespace Zippy\Html; |
| 4: | |
| 5: | |
| 6: | |
| 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: | |
| 22: | |
| 23: | |
| 24: | public function __construct($id, $src = "") { |
| 25: | parent::__construct($id); |
| 26: | $this->src = $src; |
| 27: | } |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | public function setType($type) { |
| 40: | $this->type = $type; |
| 41: | } |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 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: | |
| 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: | |
| 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: | |
| 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: | |
| 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: | |
| 122: | |
| 123: | |
| 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: | |
| 138: | |
| 139: | public function RequestHandle() { |
| 140: | $this->binaryOutput(); |
| 141: | } |
| 142: | |
| 143: | } |
| 144: | |