2017 © Pedro Peláez
 

library laravel-image-cache

Laravel image cache on Imagine

image

astroanu/laravel-image-cache

Laravel image cache on Imagine

  • Monday, August 3, 2015
  • by astroanu
  • Repository
  • 2 Watchers
  • 3 Stars
  • 89 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

Laravel ImageCache

Install

Add this to your composer and run composer update, (*1)

"astroanu/laravel-image-cache": "dev-master"

or run, (*2)

composer require astroanu/laravel-image-cache

Registering the Laravel Provider

Add this to config/app.php, (*3)

'providers' => [
         ...
        'Astroanu\ImageCache\ImageCacheProvider'
    ];

The config file

Run artisan vendor:publish to copy the config file., (*4)

php artisan vendor:publish

Sample config file:, (*5)

return  array(
    'paths' => array(
        'input' => '../storage/assets', //this is where we will upload images. Also supports Laravel 5 file system.. see below
        'output' => '../storage/imagecache' // this is where we will save cached images. You are free to clean this folder when needed manually. Also supports Laravel 5 file system.. see below
    ),
    'usestorage' => true, // wheather to use Laravel filesystem or not.. see below.
    'imagedriver' => 'imagick', // imagick or gd
    'imagepath' => 'images', // default route for images. http://www.doamin.com/route/xxxx.jpg
    'defaults' => array(
        'thumbwidth' => 80, // default thumb size
        'thumbheight' => 80, // default thumb size
        'jpgquality' => 80, // default jpg quality
    )
);

Using Laravel FileSystem

The image cache can be used with Laravel 5 file system enabling you to save your images right in to the cloud or the local file system based on your /config/filesystem.php configuration., (*6)

To set up Laravel file system turn 'usestorage' => true on the config file. Define the input and output disks on the config/filesystem.php, (*7)

'disks' => [

    'img_cache_input' => [
        'driver' => 'local',
        'root'   => storage_path().'/app/imagecache/input',
    ],
    'img_cache_output' => [
        'driver' => 'local',
        'root'   => storage_path().'/app/imagecache/output',
    ],

    'local' => [
        'driver' => 'local',
        'root'   => storage_path().'/app',
    ],

Then define the same disk ids in the config/astroanu/imagecache.php, (*8)

return  array(
    'paths' => array(
        'input' => 'img_cache_input',
        'output' => 'img_cache_output',
    ),
    'usestorage' => true,
    'imagedriver' => 'imagick',
    'imagepath' => 'images',
    'defaults' => array(
        'thumbwidth' => 80,
        'thumbheight' => 80,
        'jpgquality' => 80
    ),
);

Image routes

The following image urls/routes are available:, (*9)

/{route}/{folder}/{image id}.{extention} // full image
/{route}/{folder}/{image id}-{squareSize}.{extention} // full image
/{route}/{folder}/{image id}-{width}-{height}.{extention} // resized and cropped

examples:
http://domain.com/route/folder/imageid.jpg
http://domain.com/route/folder/imageid-100-200.jpg

Uploading images

To upload images use Astroanu\ImageCache\Uploader class;, (*10)

use Astroanu\ImageCache\Uploader;

class Users extends Model {

    // this is required. this is we save the uploaded files.
    protected $imagesdir = 'avatars';  

    public function uploadAndSetAvatar($file)
    {
        if (!is_null($file)) {      
            // here we are saving the returned image id to the model's image attribute
            $this->image = Uploader::upload($file, $this->imagesdir); 
        }
    }
}

And in the controller simply:, (*11)

$user = new User();
$user->uploadAndSetAvatar(Input::file('image'));
$user->save();

OR

if you're uploading from the controller do this:, (*12)

$fileId = Uploader::upload(Input::file('avatar'), 'avatars'); // store the $fileId on the database

Using Traits to retrive images

You may use the Trait \Astroanu\ImageCache\Traits\GetImage inside your model to retrive the image. Simply put:, (*13)

class Users extends Model {
    // this is required. this is where we look for the image file. see uploading.
    protected $imagesdir = 'avatars';  
    use \Astroanu\ImageCache\Traits\GetImage;
}

Then in your view or controller:, (*14)

echo User::find(1)->getImage(); // returns the cached image url

getImage() supports the followign parameters:, (*15)

getImage() //  returns image url with the default thumb size: as defined in the config
getImage(50) // returns a cropped and resized square image url
getImage(50, 800) // returns a cropped and resized rectangular image url

Inserting images in to html

Since you know the image id and the folder you can insert the image easily in to html, (*16)

<img src="/images/<?php echo $folder; ?>/<?php echo $imageid; ?>.jpg">
<img src="/images/<?php echo $folder; ?>/<?php echo $imageid; ?>-100.jpg">
<img src="/images/<?php echo $folder; ?>/<?php echo $imageid; ?>-30-40.jpg">

Loading image object

You can load an image as an instace and read it as blob, (*17)

$image = new Astroanu\ImageCache\Image('forlder', 'imageid');
$blob = $image->resize(200); // resized
$blob = $image->resize(50, 500); // resized
$blob = $image->resize(null); // null will return the original image blob

The Versions

03/08 2015

dev-master

9999999-dev

Laravel image cache on Imagine

  Sources   Download

MIT

The Requires

 

laravel cache image imagine

03/08 2015

1.0.6

1.0.6.0

Laravel image cache on Imagine

  Sources   Download

MIT

The Requires

 

laravel cache image imagine

15/05 2015

1.1.5

1.1.5.0

Laravel image cache on Imagine

  Sources   Download

MIT

The Requires

 

laravel cache image imagine

15/05 2015

1.1.3

1.1.3.0

Laravel image cache on Imagine

  Sources   Download

MIT

The Requires

 

laravel cache image imagine

15/05 2015

1.1.4

1.1.4.0

Laravel image cache on Imagine

  Sources   Download

MIT

The Requires

 

laravel cache image imagine

15/05 2015

1.1.2

1.1.2.0

Laravel image cache on Imagine

  Sources   Download

MIT

The Requires

 

laravel cache image imagine

15/05 2015

1.1.1

1.1.1.0

Laravel image cache on Imagine

  Sources   Download

MIT

The Requires

 

laravel cache image imagine

15/05 2015

1.1.0

1.1.0.0

Laravel image cache on Imagine

  Sources   Download

MIT

The Requires

 

laravel cache image imagine

11/03 2015

1.0.0

1.0.0.0

Laravel image cache on Imagine

  Sources   Download

MIT

The Requires

 

laravel cache image imagine