clapp/imagerepository

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

This package is not auto-updated.

Last update: 2024-04-13 18:02:41 UTC


README

Usage example (in 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 reference

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

Create a new ImageRepository instance.

Params:

  • $storagePrefix: string a prefix to allow multiple collections on the same $storageDisk and $cacheDisk - e.g. "user-profile-pictures"
  • $storageDisk: Illuminate\Contracts\Filesystem\Filesystem a disk to store the original images
  • $cacheDisk: Illuminate\Contracts\Filesystem\Filesystem a disk to store the generated image thumbnails
  • $imageManager: Intervention\Image\ImageManager ImageManager to use for image manipulation

ImageRepository::put($imageContents)

Store an image into the ImageRepository instance.

Params:

  • $imageContents: mixed any image format that Intervention\Image\ImageManager::make() can parse

Returns:

  • string $key that can be used to retrieve the image from get()

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

Params:

  • $key: string key from put() OR an absolute path to an image file on your local disk (for placeholders)
  • $width: int fit the image into this width (default: 500)
  • $height: int fit the image into this height (default: 500)

Returns:

  • string path to the generated image from the base of the $cacheDisk - can be put immediately into laravel's asset() function

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

Params:

  • $key: string key from put() OR an absolute path to an image file on your local disk (for placeholders)
  • $transform: Closure use this function to apply custom transformations to the image
  • $transformId: Closure use this function to generate a unique string for the custom transformation - the same transformation should have the same unique string

Example:

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

Returns:

  • string path to the generated image from the base of the $cacheDisk - can be put immediately into laravel's asset() function