PHPackages                             allanmcarvalho/upload - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. allanmcarvalho/upload

ActiveCakephp-plugin[File &amp; Storage](/categories/file-storage)

allanmcarvalho/upload
=====================

Upload plugin for CakePHP

1.2.0(7y ago)199[2 issues](https://github.com/allanmcarvalho/Upload/issues)MITPHPPHP &gt;=5.5.9

Since Feb 24Pushed 7y ago1 watchersCompare

[ Source](https://github.com/allanmcarvalho/Upload)[ Packagist](https://packagist.org/packages/allanmcarvalho/upload)[ RSS](/packages/allanmcarvalho-upload/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (3)Versions (17)Used By (0)

**Upload plugin for CakePHP 3.x**
=================================

[](#upload-plugin-for-cakephp-3x)

By [Allan Carvalho](https://www.facebook.com/Allan.Mariucci.Carvalho)
---------------------------------------------------------------------

[](#by-allan-carvalho)

**Installation**
================

[](#installation)

### 1. Installing dependency

[](#1-installing-dependency)

You can install this plugin into your CakePHP application using [`composer`](http://getcomposer.org). The recommended way to install composer packages is:

```
composer require allanmcarvalho/upload

```

### 2. Loading plugin

[](#2-loading-plugin)

In `App\config\bootstrap.php`

```
Plugin::load('Upload', ['bootstrap' => true]);
```

**Basic usage**
===============

[](#basic-usage)

#### **1** - You should open the table and then add the `behavior` of the plugin **Upload**.

[](#1---you-should-open-the-table-and-then-add-the-behavior-of-the-plugin-upload)

```
// in App\Model\Table\ExamplesTable.php
class ExamplesTable extends Table
{
...
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->addBehavior('Upload.Upload', [
			'file1' => [],
			'file2' => []
		]);
    }
...
```

> **Note:**Just adding the `Upload.Upload` **Behavior** does not mean it's ready, in fact, if you just add, nothing will happen. You must define which table columns are going to be responsible for storing the file name as shown above (`file1` and `file2`).

### Available **Behavior** settings

[](#available-behavior-settings)

> **Note:** The configuration name must be the same as the table column name. In this example is `file1`.

```
// in App\Model\Table\ExamplesTable.php

	$this->addBehavior('Upload.Upload', [
		'file1' => [
			'path' => WWW_ROOT . 'img' . DS,
			'prefix' => 'example_'
		]
	]);
```

> **Options**
>
> - **path:** The path where that file will be saved.

> > **Note:**When `path` is not provided, the default is `WWW_ROOT . 'files' . DS . $this->table->getAlias() . DS` or `WWW_ROOT . 'img' . DS . $this->table->getAlias() . DS` when the `image` setting is set.
>
> - **prefix:** A prefix to be added to the image file name. Default: **Does not have**;

---

- **image:** Set of settings for image uploads;

```
// in App\Model\Table\ExamplesTable.php

	$this->addBehavior('Upload.Upload', [
		'file1'  => [
			'image'  => [
				'crop' => [
					'width' => 600 // Height will be the same
				],
				'format' => 'png',
				'quality' => 75,
				'resize' => [
					'height' => 750, // width will be automatically calculated
				],
				'thumbnails' => [
					[
						'label' => 'thumb1',
						'resize' => [
							'height' => 750,
						],
						'watermark' => false // Disables watermark for this item
					],
					[
						'label' => 'thumb2',
						'resize' => [
							'min_size' => 750,
						],
						'watermark' => [
							'opacity' => 60, // 60% of opacity
							'position' => 'top', // center top position
							'path' => WWW_ROOT . 'img' . DS . 'watermark2.png',
						],
						'crop' => [
							'width' => 600 // Height will be the same
						]
					]
				],
				'watermark' => [
					'ignore_default' => true, //do not insert watermark on default
					'opacity' => 10, // 10% of opacity
					'path' => WWW_ROOT . 'img' . DS . 'watermark.png',
					'position' => 'bottom-right'
				]
			]
		]
	]);
```

> **Image options:**
>
> > **Note:** Compatibility formats to use these settings: `jpg`, `png` and `gif`.
>
> - **crop:** (optional) Crop the image. **Obs.:** If resize is also configured, it will be done before crop. Default: **Does not have**; - **width:** (at least one) The crop image width. Default: **If height is set is the same**; - **height:** (at least one) The crop image height. Default: **If width is set is the same**; - **x:** (optional) The crop image x position based from left. Default: **Center**; - **y:** (optional) The crop image y position based from top. Default: **Center**;

- **format:** Image format. It can be (`jpg`, `png`, `gif`, `same` (same as the original)). Default: `jpg`;
- **quality:** Image quality from 1 to 100. Default: `100`;
- **preserve\_animation:** When the image is gif, its animation is not frozen. Format must be `same`. No changes (thubnails, crop ...) will be made in this image.. Default: `false`;
- **resize:** (optional) Changes the image size. Default: **Does not have**;
    - **width:** (at least one) New image width. Default: **If height is set is automatic**;
    - **height:** (at least one) New image height. Default: **If width is set is automatic**;
    - **min\_size:** (at least one) Resize an image from the smaller side. Default: `false`;
- **thumbnails:** (optional) Setting to set thumbnails to be created. Default: **Does not have**;
    - **label:** (required) Label for the folder where the thumbnail will be saved. . Default: **none**;
    - **resize:** (optional) Changes the image size. Default: **Does not have**;
        - **width:** (at least one) New image width. Default: **If height is set is automatic**;
        - **height:** (at least one) New image height. Default: **If width is set is automatic**;
        - **min\_size:** (at least one) Resize an image from the smaller side. Default: `false`;
    - **watermark:** (optional) If `true` follows the default image settings (if exists). If `false` does not insert the watermark. If any setting is passed in an **array**, overwrites the default image settings. Default: `true`;
        - **opacity:** (optional) Watermak opacity from 1 to 100 where the smaller is more transparent. Default: **Same as original**.
        - **path:** (optional) Path to watermark image for this thumbnail. Default: **Same as original**;
        - **position:** (optional) Watermak orientation. Default: `bottom-right`. It can be the same positions quotes below;
    - **crop:** (optional) Crop the new thumbnail image. **Obs.:** If resize is also configured, it will be done before crop. Default: **Does not have**;
        - **width:** (required) New image crop width. Default:**If height is set is the same**;
        - **height:** (required) New image crop height. Default: **If width is set is the same**;
        - **x:** (required) The crop image x position. Default: **Center**;
        - **y:** (required) The crop image y position. Default: **Center**;
- **watermark:** Insert watermark on image. Default: **Does not have**;
    - **ignore\_default:** (optional) If `true` ignores the watermark in the default image. Default: `false`;
    - **opacity:** (optional) Watermak opacity from 1 to 100 where the smaller is more transparent. Default: `100`.
    - **path:** (required) Path to watermark image. Default:**Does not have**;
    - **position:** (optional) Watermak orientation. Default: `bottom-right`. It can be:
        - **top-left**
        - **top**
        - **top-right**
        - **left**
        - **center**
        - **right**
        - **bottom-left**
        - **bottom**
        - **bottom-right**

\####**2** - Now should open the view from the form and configure the `form` and `input` to be of the file type.

```
// in App\Template\Controller\add.ctp
...
