Skip to content

Commit 46e37fa

Browse files
committed
First commit
0 parents  commit 46e37fa

File tree

7 files changed

+660
-0
lines changed

7 files changed

+660
-0
lines changed

LICENSE.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2013-2020, developer Sergio Coderius.
4+
5+
>Permission is hereby granted, free of charge, to any person obtaining a copy
6+
>of this software and associated documentation files (the "Software"), to deal
7+
>in the Software without restriction, including without limitation the rights
8+
>to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
>copies of the Software, and to permit persons to whom the Software is
10+
>furnished to do so, subject to the following conditions:
11+
>
12+
>The above copyright notice and this permission notice shall be included in all
13+
>copies or substantial portions of the Software.
14+
>
15+
>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
>IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
>FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
>AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
>LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
>OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
>SOFTWARE.
22+

README.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Yii2 upload file behavior #
2+
3+
## About
4+
Yii2 upload file behavior - simple wey to upload images and files to server.
5+
No need anymore wrote tonn of code in controller and else testing it by houers. As a result - saving time and labor costs for uploading files to the site.
6+
Only needed upload extention from github and past some less code to model class (\yii\db\ActiveRecord) where needed hendler uploading files.
7+
More on this below.
8+
9+
## Installation
10+
11+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
12+
13+
First download extention. Run the command in the terminal:
14+
```
15+
composer require "coderius/yii2-upload-file-behavior"
16+
```
17+
18+
or add in composer.json
19+
```
20+
"coderius/yii2-upload-file-behavior": "^1.0"
21+
```
22+
and run `composer update`
23+
24+
## Usage
25+
This extention created for usage in \yii\db\ActiveRecord model classes.
26+
27+
Configyration behavior.
28+
29+
* __$nameOfAttributeFile__ = (string) default name 'file'. Virtual attribute for uploading file instance from file systrem.
30+
* __$nameOfAttributeStorage__ = (string) default name 'face_img'. Attribute for saving path to uploaded file in db.
31+
* __$newFileName__ = (string) name which is assigned to uploaded file
32+
* __$directories__ = (array) configs to upload folder and upload hendlers.Сonsists of separate arrays.
33+
Each array contains settings for the path to the target folder and a handler for uploading files to this folder like 'path' and 'handler'
34+
__-'path'__ - contains path to target folder
35+
__-'hendler'__ - Processes the downloaded file and saves to the specified in param 'path' location.
36+
37+
This extention created for usage in \yii\db\ActiveRecord model classes.
38+
39+
- So, first in model class put namespace to yii2-upload-file-behavior.
40+
- Create public variable $file for loading file from filesystem.
41+
- The database must have an attribute to store the file path. In example below it is 'img_src' attribute (marked like save in public function rules())
42+
- Then past needed configs behaviors() method like in example.
43+
44+
__!Note.__ _Don't forget to include the dependency namespaces._
45+
46+
**Example**
47+
48+
```
49+
namespase your/models;
50+
51+
use coderius\yii2UploadFileBehavior\UploadFileBehavior;
52+
use yii\imagine\Image;
53+
use Imagine\Image\Point;
54+
use Imagine\Image\Box;
55+
56+
class YourModel extends \yii\db\ActiveRecord
57+
{
58+
public $file;
59+
60+
//'img_src' - attribute to save path to file in db
61+
public function rules()
62+
{
63+
return [
64+
[['img_src'], 'safe'],
65+
}
66+
67+
...
68+
69+
public function behaviors()
70+
{
71+
return [
72+
//Another behaviors
73+
//...
74+
75+
'uploadFileBehavior' => [
76+
'class' => UploadFileBehavior::className(),
77+
'nameOfAttributeStorage' => 'img_src',
78+
'directories' => [
79+
80+
[
81+
'path' => function($attributes){
82+
return \Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/big/');
83+
},
84+
'hendler' => function($fileTempName, $newFilePath){
85+
Image::thumbnail($fileTempName, 900, 900*2/3)
86+
->copy()
87+
->crop(new Point(0, 0), new Box(900, 900*2/3))
88+
->save($newFilePath, ['quality' => 80]);
89+
sleep(1);
90+
}
91+
],
92+
[
93+
'path' => function($attributes){
94+
return \Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/middle/');
95+
},
96+
'hendler' => function($fileTempName, $newFilePath){
97+
Image::thumbnail($fileTempName, 400, 400*2/3)
98+
->save($newFilePath, ['quality' => 80]);
99+
sleep(1);
100+
}
101+
],
102+
[
103+
'path' => function($attributes){
104+
return \Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/small/');
105+
},
106+
'hendler' => function($fileTempName, $newFilePath){
107+
Image::thumbnail($fileTempName, 150, 150*2/3)
108+
->save($newFilePath, ['quality' => 80]);
109+
sleep(1);
110+
}
111+
],
112+
]
113+
],
114+
115+
];
116+
}
117+
118+
...
119+
}
120+
121+
```
122+
123+
124+
Additional actions:
125+
-------------------------------------
126+
1. Create aliases to target folders for saving uploaded files.
127+
2. Create target folders in 'frontend/web' dirrectory like example.
128+
3. Don't forget create vertual attribute. If it named like '$file', then no need set config to 'nameOfAttributeFile'(default = (string)'file').
129+
130+
## Testing
131+
132+
```bash
133+
$ ./vendor/bin/phpunit
134+
```
135+
136+
## Credits
137+
138+
- [Sergio Coderius](https://github.com/coderius)
139+
140+
## License
141+
142+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"name": "coderius/yii2-upload-file-behavior",
3+
"description": "yii2 hit counter",
4+
"keywords": [
5+
"yii2",
6+
"yii2 modules",
7+
"yii2 extension",
8+
"yii2 upload file",
9+
"image",
10+
"file",
11+
"upload",
12+
"behavior"
13+
],
14+
"type": "yii2-extension",
15+
"license": "MIT",
16+
"authors": [
17+
{
18+
"name": "Serg Coderius",
19+
"email": "sunrise4fun@gmail.com",
20+
"role": "Developer"
21+
}
22+
],
23+
24+
"require": {
25+
"php": ">=5.6.0",
26+
"yiisoft/yii2": "^2.0.0",
27+
"yiisoft/yii2-imagine": "^2.2",
28+
"mockery/mockery": "^1.2"
29+
},
30+
"require-dev": {
31+
"phpunit/phpunit": "^5"
32+
},
33+
"autoload": {
34+
"psr-4": {
35+
"coderius\\yii2UploadFileBehavior\\": "src"
36+
}
37+
},
38+
39+
"config": {
40+
"fxp-asset": {
41+
"installer-paths": {
42+
"npm-asset-library": "vendor/npm",
43+
"bower-asset-library": "vendor/bower"
44+
}
45+
},
46+
"process-timeout": 1800
47+
},
48+
"extra": {
49+
"asset-installer-paths": {
50+
"npm-asset-library": "vendor/npm",
51+
"bower-asset-library": "vendor/bower"
52+
},
53+
"asset-vcs-driver-options": {
54+
"github-no-api": true
55+
},
56+
"asset-pattern-skip-version": "(-patch)"
57+
}
58+
59+
}

phpunit.xml.dist

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit bootstrap="./tests/bootstrap.php"
3+
colors="true"
4+
verbose="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
stopOnFailure="false">
9+
<testsuites>
10+
<testsuite name="Integration">
11+
<directory>./tests/integration</directory>
12+
</testsuite>
13+
<testsuite name="Unit">
14+
<directory>./tests/unit</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">./src</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="tap" target="build/report.tap"/>
24+
<log type="junit" target="build/report.junit.xml"/>
25+
<log type="coverage-html" target="build/coverage.html"/>
26+
<log type="coverage-text" target="build/coverage.txt"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
29+
</phpunit>

0 commit comments

Comments
 (0)