fiberphp-ext / image
🖼️ FiberPHP 图片处理组件 —— GD/Imagick 双驱动,支持缩放、裁剪、水印、格式转换,链式操作。
dev-master
2026-08-23 06:58 UTC
Requires
- php: >=8.3
- ext-gd: *
- fiberphp/framework: dev-master
Requires (Dev)
- phpunit/phpunit: ^11.0
This package is auto-updated.
Last update: 2026-08-23 06:58:13 UTC
README
GD/Imagick 双驱动,支持缩放、裁剪、水印、格式转换,链式操作。
目录结构
src/
├── Image.php # 统一入口
├── Install.php # 安装器(发布配置)
├── helpers.php # 助手函数 image()
├── Contract/
│ └── DriverInterface.php # 驱动契约
└── Driver/
├── GdDriver.php # GD 驱动(默认,PHP 内置)
└── ImagickDriver.php # Imagick 驱动(更高质量)
config/
└── image.php # 配置文件
tests/
├── ImageTest.php # 8 个测试用例
├── bootstrap.php
└── fixtures/ # 测试图片
安装
composer require fiberphp-ext/image
配置
config/image.php:
return [
'driver' => extension_loaded('imagick') ? 'imagick' : 'gd',
'quality' => 85,
'formats' => ['jpg', 'png', 'webp', 'gif'],
'watermark' => [
'position' => 'bottom-right',
'opacity' => 80,
'padding' => 10,
],
];
快速开始
链式操作
// 打开图片并链式处理
image()->open('/path/to/photo.jpg')
->resize(300, 200)
->crop(200, 200)
->watermark('/path/to/logo.png', 'bottom-right')
->save('/path/to/output.jpg', quality: 80);
缩放
image()->open($path)->resize(800, 600)->save($output);
裁剪
image()->open($path)->crop(200, 200, x: 50, y: 50)->save($output);
水印
image()->open($path)
->watermark('/path/to/logo.png', 'bottom-right', opacity: 60)
->save($output);
位置可选:top-left、top-right、bottom-left、bottom-right、center
格式转换
image()->open($path)->convert('webp')->save('/path/to/output.webp');
Base64 输出
$base64 = image()->open($path)->resize(100, 100)->toBase64();
// data:image/png;base64,iVBORw0KG...
获取尺寸
$img = image()->open($path);
echo $img->width(); // 1920
echo $img->height(); // 1080
API
DriverInterface
| 方法 | 说明 |
|---|---|
open($path) | 打开图片 |
resize($w, $h) | 缩放 |
crop($w, $h, $x, $y) | 裁剪 |
watermark($path, $pos, $opacity) | 水印 |
convert($format) | 格式转换 |
save($path, $quality) | 保存 |
toBase64() | 输出 Base64 |
width() | 宽度 |
height() | 高度 |
destroy() | 释放资源 |
测试
composer install
./vendor/bin/phpunit
License
MIT