zenstruck/image
Composer 安装命令:
composer require zenstruck/image
包简介
Image file wrapper with generic transformation support.
README 文档
README
Image file wrapper to provide image-specific metadata, generic transformations, and ThumbHash generator.
Installation
composer require zenstruck/image
Usage
Note
Zenstruck\ImageFileInfo extends \SplFileInfo.
use Zenstruck\ImageFileInfo; $image = ImageFileInfo::wrap('some/local.jpg'); // create from local file $image = ImageFileInfo::from($resource); // create from resource/stream (in a temp file) // dimensional information $image->dimensions()->height(); // int $image->dimensions()->width(); // int $image->dimensions()->aspectRatio(); // float $image->dimensions()->pixels(); // int $image->dimensions()->isSquare(); // bool $image->dimensions()->isLandscape(); // bool $image->dimensions()->isPortrait(); // bool // other metadata $image->mimeType(); // string (ie "image/jpeg") $image->guessExtension(); // string - the extension if available or guess from mime-type $image->iptc(); // array - IPTC data (if the image supports) $image->exif(); // array - EXIF data (if the image supports) // utility $image->refresh(); // self - clear any cached metadata $image->delete(); // void - delete the image file // access any \SplFileInfo methods $image->getMTime();
Note
Images created with ImageFileInfo::from() are created in unique temporary files
and deleted at the end of the script.
Transformations
The following transformers are available:
To use the desired transformer, type-hint the first parameter of the callable
passed to Zenstruck\ImageFileInfo::transform() with the desired transformer's
image object:
- GD:
\GdImage - Imagick:
\Imagick - intervention\image:
Intervention\Image\Image - imagine\imagine:
Imagine\Image\ImageInterface - spatie\image:
Spatie\Image\Image
Note
The return value of the callable must be the same as the passed parameter.
The following example uses \GdImage but any of the above type-hints can be used.
/** @var Zenstruck\ImageFileInfo $image */ $resized = $image->transform(function(\GdImage $image): \GdImage { // perform desired manipulations... return $image; }); // a new temporary Zenstruck\ImageFileInfo instance (deleted at the end of the script) // configure the format $resized = $image->transform( function(\GdImage $image): \GdImage { // perform desired manipulations... return $image; }, ['format' => 'png'] ); // configure the path for the created file $resized = $image->transform( function(\GdImage $image): \GdImage { // perform desired manipulations... return $image; }, ['output' => 'path/to/file.jpg'] );
Transform "In Place"
/** @var Zenstruck\ImageFileInfo $image */ $resized = $image->transformInPlace(function(\GdImage $image): \GdImage { // perform desired manipulations... return $image; }); // overwrites the original image file
Filter Objects
Both Imagine and Intervention have the concept of filters. These are objects
that can be passed directly to transform() and transformInPlace():
/** @var Imagine\Filter\FilterInterface $imagineFilter */ /** @var Intervention\Image\Filters\FilterInterface|Intervention\Image\Interfaces\ModifierInterface $interventionFilter */ /** @var Zenstruck\ImageFileInfo $image */ $transformed = $image->transform($imagineFilter); $transformed = $image->transform($interventionFilter); $image->transformInPlace($imagineFilter); $image->transformInPlace($interventionFilter);
Custom Filter Objects
Because transform() and transformInPlace() accept any callable, you can wrap complex
transformations into invokable filter objects:
class GreyscaleThumbnail { public function __construct(private int $width, private int $height) { } public function __invoke(\GdImage $image): \GdImage { // greyscale and resize to $this->width/$this->height return $image; } }
To use, pass a new instance to transform() or transformInPlace():
/** @var Zenstruck\ImageFileInfo $image */ $thumbnail = $image->transform(new GreyscaleThumbnail(200, 200)); $image->transformInPlace(new GreyscaleThumbnail(200, 200));
Transformation Object
Zenstruck\ImageFileInfo::as() returns a new instance of the desired
transformation library's image object:
use Imagine\Image\ImageInterface; /** @var Zenstruck\ImageFileInfo $image */ $image->as(ImageInterface::class); // ImageInterface object for this image $image->as(\Imagick::class); // \Imagick object for this image
ThumbHash
A very compact representation of an image placeholder. Store it inline with your data and show it while the real image is loading for a smoother loading experience.
Note
srwiez/thumbhash is required for this feature
(install with composer require srwiez/thumbhash).
Note
Imagick is required for this feature.
Generate from Image
use Zenstruck\Image\Hash\ThumbHash; /** @var Zenstruck\ImageFileInfo $image */ $thumbHash = $image->thumbHash(); // ThumbHash $thumbHash->dataUri(); // string - the ThumbHash as a data-uri $thumbHash->approximateAspectRatio(); // float - the approximate aspect ratio $thumbHash->key(); // string - small string representation that can be cached/stored in a database
Caution
Generating from an image can be slow depending on the size of the source image. It is recommended to cache the data-uri and/or key for subsequent requests of the same ThumbHash image.
Generate from Key
When generating from an image, the ThumbHash::key() method returns a small string that
can be stored for later use. This key can be used to generate the ThumbHash without
needing to re-process the image.
use Zenstruck\Image\Hash\ThumbHash; /** @var string $key */ $thumbHash = ThumbHash::fromKey($key); // ThumbHash $thumbHash->dataUri(); // string - the ThumbHash as a data-uri $thumbHash->approximateAspectRatio(); // float - the approximate aspect ratio
zenstruck/image 适用场景与选型建议
zenstruck/image 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 34.68k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2023 年 01 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「image」 「imagine」 「gd」 「imagick」 「manipulation」 「transformation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zenstruck/image 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zenstruck/image 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zenstruck/image 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides integration between LiipImagineBundle and JMSSerializerBundle.
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
Stand-alone zoomify tile generator (format Zoomify) for use with OpenSeadragon, OpenLayers and various viewers.
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
Image manipulation using Imagine and Twig Filters.
Image Imagine generator for Nette Framework
统计信息
- 总下载量: 34.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 31
- 点击次数: 20
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-21