sergmoro1 / yii2-byone-uploader
Widget for file upload. Using Blueimp and JCrop jQuery plugins. Easy setup, quick start.
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 182
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 1
Open Issues: 0
Type:yii2-extension
Requires
- php: >=5.4.0
- bower-asset/jcrop: *
- bower-asset/jquery-file-upload: *
- yiisoft/yii2: *
- yiisoft/yii2-jui: ~2.0.0
This package is auto-updated.
Last update: 2019-12-22 13:17:10 UTC
README
No more maintained, please use next version sergmoro1\yii2-uploader
Demo
Photos by categories. (when editing)Photo gallery "before" & "after" (similarly).
Advantages
If the model needs files, you don't need:
- define attribute
file
in a model, - to process the result of filling the form,
- to come up with where save files,
You can:
- add a description to the file,
- delete no needed files,
- define sizes to compress images,
- crop images,
- swap files rows by mouse.
How it's made?
Information about all uploaded files are stored in one table - onefile.
There is no need to define a field of type file
in the model, which need files.
The files are uploaded and stored in the directory frontend/web/files
.
For each model subdirectories are possible: frontend/web/files/user
or frontend/web/files/post
.
In the subdirectory the files are arranged by users (or posts) and sizes:
frontend/web/files/user/2
frontend/web/files/user/2/thumb
frontend/web/files/user/2/original
Rows with uploaded files can be sorted dragging the rows with the mouse.
Example
User must can upload photos. Need to be defined in a modelcommon/models/User
:
... use sergmoro1\uploader\FilePath; use sergmoro1\uploader\models\OneFile; ... class User extends ActiveRecord implements IdentityInterface { ... // Images sizes public $sizes = [ // Catalog original should be define for cropping 'original' => ['width' => 1600, 'height' => 900, 'catalog' => 'original'], 'main' => ['width' => 400, 'height' => 400, 'catalog' => ''], 'thumb' => ['width' => 90, 'height' => 90, 'catalog' => 'thumb'], ]; // Get ref to the file, make dir and so public function behaviors() { return [ 'FilePath' => [ 'class' => FilePath::className(), 'file_path' => '/files/user/', ] ]; } // All files for User model public function getFiles() { return OneFile::find() ->where('parent_id=:parent_id AND model=:model', [ ':parent_id' => $this->id, ':model' => 'common\models\User', ]) // only if rows should be draggable & sortable ->orderBy('created_at') ->all(); } ...
Now User can uploading and files will be in:
frontend/web/files/user/user_id frontend/web/files/user/user_id/original frontend/web/files/user/user_id/thumb
Thus, the sizes will be modified as specified.
To do uploading you need to place widget in a form or any other view.
For ex. backend/views/user/_form.php
:
use sergmoro1\uploader\widgets\Byone; ... <?= Byone::widget([ 'model' => $model, 'cropAllowed' => true, ]) ?>
If image should be cropped (cropAllowed = true), subdirectory original
need to be define.
The aspect ratio for cropping is set on main
subdirectory.
By default, only main
image will be cropped, but you can specify any other directory (except original
)
by defining the parameter crop
.
public $sizes = [ 'original' => ['width' => 3600, 'height' => 2400, 'catalog' => 'original'], 'main' => ['width' => 400, 'height' => 400, 'catalog' => ''], 'thumb' => ['width' => 120, 'height' => 80, 'catalog' => 'thumb', 'crop' => true], ];
May be uploaded any amount of files but files amount can be limited by maxFiles
.
Installation
In app directory:
$ composer require sergmoro1/yii2-byone-uploader "dev-master"
Run migration:
$ php yii migrate --migrationPath=@vendor/sergmoro1/yii2-byone-uploader/migrations
To register the module in an app - common/config/main.php
:
'modules' => [ 'uploader' => [ 'class' => 'sergmoro1\uploader\Module', ],
If advanced
template is used then before_web
param should be defined.
For example backend\config\params.php
<?php return [ 'before_web' => 'backend', ... ];
For frontend
or basic
template no needed.
Description of uploaded files
You can leave comments to the files. To do this in the form backend/views/user/_form.php
,
in the already mentioned widget, you need to add the parameter appendeixView
:
... <?= Byone::widget([ 'model' => $model, 'appendixView' => '/user/appendix', 'cropAllowed' => true, ]) ?>
And add view, for ex. backend/views/user/appendix.php
the following content:
<span id='description'> <?php echo isset($file->vars->description) ? $file->vars->description : ''; ?> </span>
Field description
defined by default, but fields not limited.
Options
cropAllowed
(false)
If image should be cropped, 'original' size must be defined in $sizes
array of the model.
draggable
(false)
If uploaded files should be swapped then in a getter getFiles()
rows must be sorted by created_at
.
public function getFiles() { return OneFile::find() ->where('parent_id=:parent_id AND model=:model', [ ':parent_id' => $this->id, ':model' => 'common\models\YourModel', ]) ->orderBy('created_at') ->all(); }
acceptFileTypes
('image\/[jpeg|jpg|png|gif]')
minFileSize
(0.1Mb)
maxFileSize
(2Mb)
maxFiles
(0 - any amount)
secure
(true)
Ordinary extension require user authorization, but verification may be switched off.