dev-master
9999999-dev https://github.com/karolak/cakephp-imageImage plugin for CakePHP 3.
MIT
The Requires
by Krzysztof Karolak
cakephp image uploader
Wallogit.com
2017 © Pedro Peláez
Image plugin for CakePHP 3.
Plugin for fast and easy handling image uploads in CakePHP., (*1)
Image presets are generated using WideImage. See the sourceforge documentation page., (*2)
You can install this plugin into your CakePHP application using composer., (*3)
The recommended way to install composer packages is:, (*4)
$ composer require karolak/cakephp-image
or manually add this line to your "require" key in composer.json file:, (*5)
"require": {
...
"karolak/cakephp-image": "dev-master"
}
and run in console:, (*6)
$ composer update
Next in your bootstrap.php file add line (to enable plugin in your app):, (*7)
Plugin::load('Image', ['bootstrap' => false, 'routes' => false]);
To create table "images" in your database for storing informations about uploaded images, run this command:, (*8)
$ bin/cake migrations migrate -p Image
Before you add a file upload field to a form, you must first make sure that the form enctype is set to “multipart/form-data”:, (*9)
echo $this->Form->create($document, ['enctype' => 'multipart/form-data']); // OR echo $this->Form->create($document, ['type' => 'file']);
Next add one or more file inputs:, (*10)
echo $this->Form->input('photos', ['type' => 'file']);
Now you have to configure behavior in your table class. For example, add this to your initialize method:, (*11)
public function initialize(array $config)
{
$this->addBehavior('Image.Image', [
'path' => Configure::read('App.wwwRoot').Configure::read('App.imageBaseUrl').'uploads',
'presets' => [
'small' => [
'resize' => [200, 200, 'outside', 'any'],
'crop' => ['center', 'center', 200, 200]
],
'big' => [
'resize' => [1000, 1000, 'inside', 'any']
]
],
'fields' => [
'photos' => 'many'
],
'quality' => 80
]);
}
To check uploaded images use Cake's standard validation methods (in 3.0 version they add some new file validation rules)., (*12)
$document = $this->Documents->get($id, ['contain' => ['Images_DocumentsPhotos']]);
This example assume that your table is called Documents and form field name used to upload images was photos., (*13)
You can use helper to show images from presets. Just add this code to AppView initialize method:, (*14)
public function initialize()
{
if(Plugin::loaded('Image')) {
$this->loadHelper('Image.Image');
}
}
Now to render img tag with image from preset use:, (*15)
echo $this->Image->render($image); // original file echo $this->Image->render($image, ['preset' => 'small']); // image file from "small" preset echo $this->Image->render($image, ['preset' => 'big', 'alt' => 'Cool image']); // image file from "big" preset + img attributes
Or you can just get image url:, (*16)
echo $this->Image->url($image);
Image plugin for CakePHP 3.
MIT
cakephp image uploader