|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @link https://github.com/SDKiller/zyx-widget-imagex |
| 4 | + * @copyright Copyright (c) 2014 Serge Postrash |
| 5 | + * @license BSD 3-Clause, see LICENSE.md |
| 6 | + */ |
| 7 | + |
| 8 | +namespace zyx\widgets; |
| 9 | + |
| 10 | +use Yii; |
| 11 | +use yii\base\Widget; |
| 12 | +use yii\helpers\Url; |
| 13 | +use yii\helpers\Html; |
| 14 | +use yii\base\InvalidParamException; |
| 15 | + |
| 16 | + |
| 17 | +/** |
| 18 | + * Class ImageX |
| 19 | + * |
| 20 | + * Widget for simple generating OpenGraph meta tags and Schema.org markdown for images ('X' stands for 'eXtended') |
| 21 | + * |
| 22 | + * @package zyx\widgets |
| 23 | + */ |
| 24 | +class ImageX extends Widget |
| 25 | +{ |
| 26 | + |
| 27 | + /** |
| 28 | + * @var null $src Image src attribute (the same you could used for [[Html::img()]] |
| 29 | + */ |
| 30 | + public $src = null; |
| 31 | + /** |
| 32 | + * @var array $options Usual image options (the same you could used for [[Html::img()]] |
| 33 | + */ |
| 34 | + public $options = []; |
| 35 | + /** |
| 36 | + * @var array $og OpenGraph options (not mandatory) - if not set, widget will try to get them from image options |
| 37 | + * You can disable rendering OpenGraph meta tags by setting 'enable' => false |
| 38 | + */ |
| 39 | + public $og = ['enable' => true]; |
| 40 | + /** |
| 41 | + * @var array $md Schema.org options (not mandatory) - if not set, widget will try to get them from image options |
| 42 | + * You can disable rendering Schema.org markdown by setting 'enable' => false |
| 43 | + */ |
| 44 | + public $md = ['enable' => true]; |
| 45 | + |
| 46 | + |
| 47 | + public function __construct($config = []) |
| 48 | + { |
| 49 | + //by default both opengraph and microdata are enabled, disable if ['enable' => false] was explicitly passed to widget |
| 50 | + $config['og'] = array_merge($this->og, (isset($config['og'])) ? $config['og'] : []); |
| 51 | + $config['md'] = array_merge($this->md, (isset($config['md'])) ? $config['md'] : []); |
| 52 | + |
| 53 | + parent::__construct($config); |
| 54 | + } |
| 55 | + |
| 56 | + public function init() |
| 57 | + { |
| 58 | + if (empty($this->src)) { |
| 59 | + throw new InvalidParamException('Image path should be defined!'); |
| 60 | + } |
| 61 | + |
| 62 | + parent::init(); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + public function run() |
| 67 | + { |
| 68 | + $html = ''; |
| 69 | + $html_end = ''; |
| 70 | + if (isset($this->og['enable']) && $this->og['enable'] !== false) { |
| 71 | + $this->renderHead(); |
| 72 | + } |
| 73 | + if (isset($this->md['enable']) && $this->md['enable'] !== false) { |
| 74 | + $html .= '<div itemscope itemtype="http://schema.org/ImageObject" '.(empty($this->md['div_class']) ? '' : 'class="'.$this->md['container_class'].'"').'>'; |
| 75 | + $html_end .= $this->renderMarkdown(); |
| 76 | + } |
| 77 | + |
| 78 | + $html .= Html::img($this->src, $this->options); |
| 79 | + $html .= $html_end; |
| 80 | + |
| 81 | + echo $html; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * This function renderes meta tags for image according to OpenGraph protocol. |
| 86 | + * @see http://ogp.me/ |
| 87 | + * Properties set explicitly in 'og' configuration array, passed to widget, have priority. |
| 88 | + * If no 'og' options were set, widget attempts to extract properties from image 'options' array. |
| 89 | + */ |
| 90 | + private function renderHead() |
| 91 | + { |
| 92 | + $this->view->registerMetaTag(['property' => 'og:image', 'content' => Url::to($this->src)]); |
| 93 | + |
| 94 | + if (!empty($this->og['secure_url'])) { |
| 95 | + $this->view->registerMetaTag(['property' => 'og:image:secure_url', 'content' => $this->og['secure_url']]); |
| 96 | + } |
| 97 | + |
| 98 | + $type = empty($this->og['type']) ? $this->guessMime($this->src) : $this->og['type']; |
| 99 | + if (!empty($type)) { |
| 100 | + $this->view->registerMetaTag(['property' => 'og:image:type', 'content' => $type]); |
| 101 | + } |
| 102 | + |
| 103 | + if (!empty($this->options['width']) || !empty($this->og['width'])) { |
| 104 | + $this->view->registerMetaTag(['property' => 'og:image:width', 'content' => (empty($this->og['width']) ? $this->options['width'] : $this->og['width'])]); |
| 105 | + } |
| 106 | + if (!empty($this->options['height']) || !empty($this->og['height'])) { |
| 107 | + $this->view->registerMetaTag(['property' => 'og:image:height', 'content' => (empty($this->og['height']) ? $this->options['height'] : $this->og['height'])]); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + private function guessMime($src) |
| 113 | + { |
| 114 | + $mime = ''; |
| 115 | + |
| 116 | + $mimes = [ |
| 117 | + 'bmp' => 'image/bmp', |
| 118 | + 'gif' => 'image/gif', |
| 119 | + 'jpeg' => 'image/jpeg', |
| 120 | + 'jpe' => 'image/jpeg', |
| 121 | + 'jpg' => 'image/jpeg', |
| 122 | + 'png' => 'image/png', |
| 123 | + 'tiff' => 'image/tiff', |
| 124 | + 'tif' => 'image/tiff', |
| 125 | + 'svg' => 'image/svg+xml' |
| 126 | + ]; |
| 127 | + |
| 128 | + $dot = strrpos($src, '.') + 1; |
| 129 | + $ext = substr($src, $dot); |
| 130 | + |
| 131 | + if (array_key_exists($ext, $mimes)) { |
| 132 | + $mime = $mimes[$ext]; |
| 133 | + } |
| 134 | + |
| 135 | + return $mime; |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + /** |
| 140 | + * Properties set explicitly in 'md' configuration array, passed to widget, have priority. |
| 141 | + * Schema.org gives no exaples for 'width' and 'height' properties, we take appropriate example |
| 142 | + * @see http://help.yandex.ru/webmaster/video/schema-org-markup.xml |
| 143 | + * @return string |
| 144 | + */ |
| 145 | + private function renderMarkdown() |
| 146 | + { |
| 147 | + $this->options['itemprop'] = 'contentUrl'; |
| 148 | + |
| 149 | + $html = ''; |
| 150 | + if (!empty($this->options['width']) || !empty($this->md['width'])) { |
| 151 | + $html .= '<meta itemprop="width" content="'.(empty($this->md['width']) ? $this->options['width'] : $this->md['width']).'" />'; |
| 152 | + } |
| 153 | + if (!empty($this->options['height']) || !empty($this->og['height'])) { |
| 154 | + $html .= '<meta itemprop="height" content="'.(empty($this->md['height']) ? $this->options['height'] : $this->md['height']).'" />'; |
| 155 | + } |
| 156 | + $html .= '</div>'; |
| 157 | + |
| 158 | + return $html; |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments