inteve / simple-image-storage
Nette 的简单图像存储
v2.0.0
2018-01-08 16:13 UTC
Requires
- php: >=5.6.0
- nette/http: ^2.4
- nette/utils: ^2.4
README
Nette 的图像存储。
安装
composer require inteve/simple-image-storage
库需要 PHP 5.6.0 或更高版本。
用法
use Inteve\SimpleImageStorage\ImageStorage;
在配置中注册
parameters: imageStorage: directory: %wwwDir% publicDirectory: @httpRequest::getUrl()::getBaseUrl() storageName: images # optional services: - Inteve\SimpleImageStorage\ImageStorage(%imageStorage.directory%, %imageStorage.publicDirectory%, %imageStorage.storageName%)
存储图像
<?php $image = $imageStorage->upload($fileUpload); // $image = 'image-name.jpg' $avatar = $imageStorage->upload($fileUpload, 'upload/avatars'); // $avatar = 'upload/avatars/image-name.jpg';
删除图像
<?php $imageStorage->delete('upload/avatar/image-name.jpg');
获取原始路径
<?php $path = $imageStorage->getRealPath('upload/avatar/image-name.jpg');
获取原始公开路径
<?php $path = $imageStorage->getPublicPath('upload/avatar/image-name.jpg');
缩略图
<?php $path = $imageStorage->thumbnail($file, $width, $height, $flags = NULL, $quality = NULL); $path = $imageStorage->thumbnail('upload/avatar/image-name.jpg', 512, 256);
默认使用 Nette\Utils\Image
,但您可以在构造函数中提供自定义缩略图生成器
$imageStorage = new ImageStorage(..., ..., ..., function ($sourcePath, $outputPath, array $thumbnailData) { $im = new Imagick; $im->readImage($sourcePath); $im->crop(...); $im->writeImage($outputPath); });
string $sourcePath
- 原始图像路径string $outputPath
- 缩略图路径array $thumbnailData
int|NULL width
- 缩略图宽度或 NULLint|NULL height
- 缩略图高度或 NULLint|NULL quality
- 输出图像质量或 NULLint flags
- 查看常量 ImageStorage::SHRINK_ONLY, STRETCH, FIT, FILL & EXACT
在模板中
class BasePresenter extends Nette\Application\UI\Presenter { /** @var Inteve\SimpleImageStorage\ImageStorage @inject */ public $imageStorage; protected function beforeRender() { parent::beforeRender(); $this->template->img = $this->imageStorage; } }
<img src="{$img->thumbnail($avatar, 512, 256)}"> <img src="{$img->thumbnail($avatar, 512, 256, $img::SHRINK_ONLY)}"> <img src="{$img->thumbnail($avatar, 512, 256, $img::STRETCH)}"> <img src="{$img->thumbnail($avatar, 512, 256, $img::FILL)}"> <img src="{$img->thumbnail($avatar, 512, 256, $img::EXACT)}">
许可证: 新BSD许可证
作者: Jan Pecha, https://www.janpecha.cz/