Skip to content

Latest commit

History

58 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

LaravelLaravelLicense

Imagecache

Laravel 5 package that allows you to create image thumbnails according to predefined presets, and store them in your Laravel public folder to serve them up without generating them on each page load.

Original repo: https://github.com/DevFactoryCH/imagecache + added watermarks

Installation

Using Composer, edit your composer.json file to require rik43/imagecache.

Laravel 5
"require": {
"rik43/imagecache": "3.0.*"
}

Then from the terminal run

composer update

Then in your app/config/app.php file register the following service providers:

'Intervention\Image\ImageServiceProvider',
'Devfactory\Imagecache\ImagecacheServiceProvider',

And the Facade:

'Imagecache' => 'Devfactory\Imagecache\Facades\ImagecacheFacade',

Publish the config:

Laravel 5
php artisan vendor:publish

Usage

Define some presets in:

Laravel 5

config/imagecache.presets.php

<?phpreturnarray(
'teaser' => array(
'width' => 150,
'height' => 100,
'method' => 'crop',
),
'465x320' => array(
'width' => 465,
'height' => 320,
'method' => 'resize',
'background_color' => '#FFFFFF',
),
);

get($filename, $preset, $args = NULL)

ParameterDescription
$filenameThe FILENAME passed to the get() method is relative to the files_directory that you set in the config file (which itself is relative to the public_path config). So if you pass images/sunset.jpg, it will actually look for PUBLIC_PATH/uplaods/images/sunset.jpg with the default config. I haven't tried, but if you set public_path and files_directory to '', you should be able to pass aa absolute path to the file.
$presetOne of the presets defined in the presets.php config file
$args(optional)An array of additional properties.

$args properties

PropertyDescription
base_dirIf you don't want to use the base folder you have setup in the config file because you are referencing a file that might be stored in your assets, you can pass an absolute path here to override the defaults
classA string containing any classes you want the <img> tag to contain
altThe alt text for the image
titleThe title text for the image

Return Value:

PropertyDescription
srcThe URL to the image to be used inside the <img src=""> attribute
imgThe full <img> tag to display the image
img_nosizeThe full <img> tag without width and height attributes for use with responsive themes
pathThe full path to the image on storage

Example usages:

$image = Imagecache::get('images/sunset.jpg', 'teaser');
echo$image->img;
echo'<img src="'. $image->src .'">// Directly in a blade template{{ Imagecache::get('uploads/images/sunset.jpg', 'teaser')->img }}

You can also directly access one of the properties as such without needing to if gate the call to get(). If using Laravel 5 you'll need to use the new raw notation instead of the double curly braces {{ ... }}.

{!! Imagecache::get('uploads/images/sunset.jpg', 'teaser')->img !!}

If you set the use_placeholders variable to TRUE in the imagecache.config.php file, and your image doesn't exist or doesn't generate a cached version. Where you would normally receive an array with empty values, you can get a placeholder image matching the preset that you requested. This is very useful during developement when you might not have images for all your content.

get_original($filename, $args = NULL)

If you don't want to apply any preset to the image, but still want to use the call to generate the <img> tag, accepts same parameters and works the same way as get(), just without $preset

delete($filename)

Deletes all the cached images for each preset for the given filename.

Presets

When defining your presets, these are the options you can set:

PropertyDescription
widthrequiredThe width of the generated image in pixels.
heightrequiredThe height of the generated image in pixels.
methodrequiredDefines the way the image will be transformed. See the table below for accepted methods
background_coloroptionalThe color of the canvas which will be used as a background when using the method resize. e.g. '#FFFFFF'.

The method property accepts the following types of transformations:

MethodDescription
cropWill smart crop an image to make it fit the desired dimensions. It will cut content of the image off the top/bottom and sides if required to preserve the aspect ratio.
resizeWill create a canvas of the desired dimensions and will then resize the image to fit within the bounds without cropping. Images will not be upscaled if they are smaller then the dimensions. The optional property background_colorcan be used here to define the color of the canvas which the image will be placed on.

About

Laravel 4/5 Package that crops and creates thumbnails of images

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages