huttchdev / craft-webpme
如果访问者的浏览器和服务器都支持WebP,则获取WebP图像的URL
1.1.2
2024-02-20 20:58 UTC
Requires
- php: ^8.0
- craftcms/cms: ^4.0|^5.0
README
这是一个简单的函数,如果访问者的浏览器和服务器支持WebP,则返回WebP格式的图像URL,否则将回退到标准图像URL。
- 如果服务器和访问者的浏览器都支持WebP,则传入的图像将被转换为WebP格式,除非传入的图像是SVG。
- 如果访问者的浏览器不支持WebP,则图像将不会转换,并保持其当前格式。
- 如果传入的图像已经是WebP图像,但访问者的浏览器不支持WebP,则图像将被转换为PNG(如果是透明图像)。
用法
WebP Twig函数
Example:
<img src="{{ WebP(entry.image.one(), {'mode': 'crop', 'height': '600', 'width': '800'}) }}"/>
WebP(image asset, craft image transform params)
使用预设图像转换
Example:
<img src="{{ WebP(entry.image.one(), {transform: 'headerImage'}) }}
图像转换参数
任何Craft支持图像参数都可以使用 查看文档
可能参数
{
transform: 'headerImage', // (optional) pass to use preset image transform created in the CMS admin panel, optionally add other params in combination with this to override preset settings
height: 300, // set height
width: 800, // set width
mode: 'crop', // set transform mode (crop, fit, letterbox, stretch)
quality: 80, // ovveride quality settings
position: 'top-center' // override default center position when no focal point (supported when using 'crop' or 'letterbox')
}
可选函数名变体(根据您的喜好)
WebP()
webp()
WebPMe()
webpMe()
webpme()
IsWebPSupported Twig函数
对缓存键很有用,用于在 {% cache %} 时生成支持WebP的缓存版本和不支持的版本
例如:{% cache using key 'header-' ~ IsWebPSupported %} 查看文档
IsWebPSupported
根据服务器和访问者浏览器的支持返回true或false