zyx /widget-imagex
为图像添加opengraph元标签和microdata markdown
Requires
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-14 13:58:32 UTC
README
Widget用于简单生成图像的OpenGraph元标签和Schema.org markdown('X'代表'扩展')。
你可以什么都不做 - OpenGraph元标签和Schema.org markdown将从图像属性(你仍然可以像在Html::img()
中定义那样定义)动态生成。
需求
通常应遵循Yii 2需求。最低要求是您的Web服务器支持PHP 5.4.0。
安装
通过Composer安装
如果您没有Composer,您可以通过遵循getcomposer.org上的说明来安装它。
运行以下命令之一:
php composer.phar require --prefer-dist zyx/widget-imagex "*"
或者
"zyx/widget-imagex": "*"
将其添加到您的composer.json文件的require部分。
用法
只需将以下字符串添加到您的布局中
use zyx\widgets\ImageX;
并在图像所在的位置调用widget,而不是通常的Html::img()
echo ImageX::widget([
'src' => 'http://static.yiiframework.com/css/img/logo.png',
'options' => ['width' => 280, 'height' => 60],
'og' => [],
'md' => ['div_class' => 'image_wrap']
]);
注意:只有'src'参数是必需的。'options'数组与您在Html::img()
中使用的图像选项相同。如果'og'(OpenGraph)和'md'(schema.org)配置数组都为空,则可以完全不声明它们。您可以禁用其中一个(默认情况下两者都启用) - 例如 'md' => ['enable' => false]
。
因此,上述示例的结果可能如下所示
在HEAD中
...
<meta property="og:image" content="http://static.yiiframework.com/css/img/logo.png">
<meta property="og:image:type" content="image/png">
<meta property="og:image:width" content="280">
<meta property="og:image:height" content="60">
...
在BODY中('img'被包裹在'div'标签中)
...
<div itemscope itemtype="http://schema.org/ImageObject" class="image_wrap">
<img src="http://static.yiiframework.com/css/img/logo.png" width="280" height="60" alt="" itemprop="contentUrl" />
<meta itemprop="width" content="280" />
<meta itemprop="height" content="60" />
</div>
...
在'og'和'md'配置数组中明确设置的属性(例如'width'和'height'),并将它们传递给widget时具有优先级。如果没有设置此类选项,则widget会尝试从图像'option'数组中提取属性。
因此,您只需像调用Html::img()
而没有附加选项一样调用
echo ImageX::widget([
'src' => 'http://static.yiiframework.com/css/img/logo.png',
'options' => ['width' => 280, 'height' => 60]
]);
即可。