clapp/imagerepository

1.0.0-alpha1 2017-09-07 15:37 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:04:04 UTC


README

使用示例(在 Laravel 中)

use Clapp\ImageRepository\ImageRepository;
use Clapp\ImageRepository\ImageMissingOrInvalidException;

class User {

    protected $profilePictureRepository = null;

    public function __construct(/* ... */){

        $this->profilePictureRepository = new ImageRepository('profile-pictures/');

        /* ... */
    }

    public function getProfilePicture($width=500, $height=500)
    {
        try {
            return $this->profilePictureRepository->get(array_get($this->attributes, 'profile_picture'), $width, $height);
        }catch(ImageMissingOrInvalidException $e){
            return $this->profilePictureRepository->get(resource_path('assets/images/placeholder.png'), $width, $height);
        }
    }

    public function setProfilePictureAttribute($pictureContents){
        $value = $pictureContents;
        if (!empty($value)){
            $value = $this->profilePictureRepository->put($value);
        }
        $this->attributes['profile_picture'] = $value;
    }
}

API 参考

ImageRepository::__construct($storagePrefix = "", $storageDisk = null, $cacheDisk = null, ImageManager $imageManager = null)

创建一个新的 ImageRepository 实例。

参数

  • $storagePrefix: string 一个前缀,允许在相同的 $storageDisk 和 $cacheDisk 上有多个集合 - 例如 "user-profile-pictures"
  • $storageDisk: Illuminate\Contracts\Filesystem\Filesystem 一个磁盘,用于存储原始图像
  • $cacheDisk: Illuminate\Contracts\Filesystem\Filesystem 一个磁盘,用于存储生成的图像缩略图
  • $imageManager: Intervention\Image\ImageManager 用于图像处理的 ImageManager

ImageRepository::put($imageContents)

将图像存储到 ImageRepository 实例中。

参数

  • $imageContents: mixed Intervention\Image\ImageManager::make() 可以解析的任何图像格式

返回

  • string $key,可以用于从 get() 中检索图像

ImageRepository::get($key, $width = 500, $height = 500)

参数

  • $key: stringput() 中来的 key OR 你本地磁盘上的图像文件的一个绝对路径(用于占位符)
  • $width: int 将图像适配到这个宽度(默认:500)
  • $height: int 将图像适配到这个高度(默认:500)

返回

  • string 从 $cacheDisk 基础生成的图像的路径 - 可以立即用于 laravel 的 asset() 函数

ImageRepository::get($key, Closure $transform, Closure $transformId)

参数

  • $key: stringput() 中来的 key OR 你本地磁盘上的图像文件的一个绝对路径(用于占位符)
  • $transform: Closure 使用这个函数来对图像应用自定义转换
  • $transformId: Closure 使用这个函数来为自定义转换生成一个唯一的字符串 - 相同的转换应该有相同的唯一字符串

示例

$image = $repo->get($key, function($image){
    $image->resize(123, null, function($constraint){
        $constraint->aspectRatio();
    });
    return $image;
}, function(){
    return "123_auto";
});

返回

  • string 从 $cacheDisk 基础生成的图像的路径 - 可以立即用于 laravel 的 asset() 函数