dodecastudio/craft-blurhash
Composer 安装命令:
composer require dodecastudio/craft-blurhash
包简介
Render a BlurHash from a given image.
README 文档
README
Render a BlurHash from a given asset in Craft CMS.
A BlurHash is a compact representation of a placeholder for an image. A blurred version of an image, useful for displaying whilst the full resolution image is loading.
This plugin uses kornrunner's PHP implementation of BlurHash, php-blurhash.
Requirements
- Craft CMS 3.X, 4.X or 5.X
- PHP 7.4+
- GD / ImageMagick (as required by Craft CMS)
Installation
Install the plugin as follows:
-
Open your terminal and go to your Craft project:
cd /path/to/project -
Then tell Composer to load the plugin:
composer require dodecastudio/craft-blurhash -
In the Control Panel, go to Settings → Plugins and click the “Install” button for BlurHash.
BlurHash Overview
The BlurHash plugin will generate a BlurHash image from a craft asset. Here's an example of what you can expect:
Input:
Result:
Using BlurHash
Common use
To use the plugin, first grab an asset from Craft, perhaps something like this:
{% set testAsset = craft.assets().id(101).one() %}
You can then pass your asset to the plugin's blurhash function, which will return a blurhash image as a data-url, perfect for using in an img element like so:
<img src="{{ blurhash(testAsset) }}" width="{{testAsset.width}}" height="{{testAsset.height}}" style="aspect-ratio: {{testAsset.width}} / {{testAsset.height}};" />
Note: Due to the compact dimensions of the blurhash image, its aspect ratio may differ fractionally from the source material. Ensure any image markup correctly renders the aspect ratio based on the source markup if you are trying to prevent CLS. One way to achieve this is to use the CSS aspect-ratio property, as used in the example above.
Other features
Generating a blurhash string
If you just want to generate just a blurhash string from a given asset, you can use the blurhash filter like this:
{{ testAsset|blurhash }}
Which will return something like this:
f+IrKQogj]j[ayj[_4ofj[j[ayj[%NazayfQj[j[t6ayWBayofj[Rjj[j@fQj[ay
You might want to store this in some way, for use later.
Generating a blurhash image from a blurhash string
If you already have a blurhash string and want to generate an image from it, you can use the blurhashToUri function like this:
<img src="{{ blurhashToUri('f+IrKQogj]j[ayj[_4ofj[j[ayj[%NazayfQj[j[t6ayWBayofj[Rjj[j@fQj[ay') }}" width="256" height="256" alt="Blurhash image" />
Note that blurhash strings do not have any data encoded about the size or scale of the image. You may need additional information in order to display the correct aspect ratio.
Returning average color for an image
BlurHash strings contain the average color for the image. You can decode this value from a BlurHash string and return it as a Craft ColorData object.
{{ averageColor('f+IrKQogj]j[ayj[_4ofj[j[ayj[%NazayfQj[j[t6ayWBayofj[Rjj[j@fQj[ay') }}
{{ averageColor('f+IrKQogj]j[ayj[_4ofj[j[ayj[%NazayfQj[j[t6ayWBayofj[Rjj[j@fQj[ay').getRgb() }}
...or directly from an asset:
{{ testAsset|averageColor }}
{{ averageColor(testAsset) }}
{{ averageColor(testAsset).getRgb() }}
Returning memory info
Images need to be loaded in to memory in order to be read, and a blurhash generated. This helper function will tell you approximately how much memory (in bytes) will be needed for a given image.
{{ testAsset|memoryInfo }}
GraphQL Support
As of v1.1.0 it's possible to use the plugin via Graph QL. The same functionality that's available with the Twig functions, is now available through Graph QL directives.
Returning a blurhash data URI from an asset field
An asset field can be returned as a data uri to a blurhash image using the @assetToBlurHash directive. It can also be used to return a blurhash string by setting the asUri argument to false.
{
entries(section: "news") {
title
... on news_article_Entry {
asset {
blurhashString: url @assetToBlurHash(asUri: false)
blurhashUri: url @assetToBlurHash
}
}
}
}
This will return some JSON which looks a bit something like this:
{
"data": {
"entries": [
{
"title": "An important news item",
"asset": [
{
"blurhashString": "f+IrKQogj]j[ayj[_4ofj[j[ayj[%NazayfQj[j[t6ayWBayofj[Rjj[j@fQj[ay",
"blurhashUri": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAAB..."
}
]
}
]
}
}
Rendering a blurhash image from a plaintext blurhash string
If you have a blurhash string saved in a Craft, you can render it to a data URI using the @blurhashToUri directive. In this example, there is a plaintext field called blurhashStringField stored in our news article entry.
{
entries(section: "news") {
title
blurhashStringField
blurhashUri: blurhashStringField @blurhashToUri
}
}
And that will then give us some JSON that looks a bit like this:
{
"data": {
"entries": [
{
"title": "An important news item",
"blurhashStringField": "f+IrKQogj]j[ayj[_4ofj[j[ayj[%NazayfQj[j[t6ayWBayofj[Rjj[j@fQj[ay",
"blurhashUri": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAAB..."
}
]
}
}
Getting an average color for an image
If you want to return the average color for an image saved in Craft, you can use the @averageColor directive. This returns a hex color value as a string.
{
entries(section: "news") {
title
... on news_article_Entry {
blurhashStringField @averageColor
asset {
averageColor: url @averageColor
}
}
}
}
Which will return you some JSON like this:
{
"data": {
"entries": [
{
"title": "An important news item",
"blurhashStringField": "#a3a696",
"asset": [
{
"averageColor": "#817c82"
}
]
}
]
}
}
Settings
Default settings can be overridden. Please see the blurhash-config.php file for details. Copy to /config/blur-hash.php to use.
Licence 🌳
This package is Treeware. If you use it in production, then we ask that you buy the world a tree.
And why not? By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
Thanks! 🙌
Shout-out to @olsp for buying 100 trees and @mokopan for buying 25 trees.
If you've purchased trees through Ecologi, as part of the Treeware license, please let us know for a shout-out.
dodecastudio/craft-blurhash 适用场景与选型建议
dodecastudio/craft-blurhash 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.28k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2021 年 08 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cms」 「Craft」 「craftcms」 「craft-plugin」 「blurhash」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dodecastudio/craft-blurhash 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dodecastudio/craft-blurhash 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dodecastudio/craft-blurhash 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Expand, collapse, change the status of, or delete multiple blocks in a Matrix field simultaneously.
GraphQL authentication for your headless Craft CMS applications.
Set Links with a specific language parameter
Supercharged text field validation.
Integrate with Snipcart.
Restrict user registration by domain in Craft CMS, allows you to limit who can register for your site based on their email address.
统计信息
- 总下载量: 15.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 10
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2021-08-05

