This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ImageFileBehavior.php
80 lines (76 loc) · 2.89 KB
/
ImageFileBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* @link https://github.com/yii2tech
* @copyright Copyright (c) 2015 Yii2tech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace yii2tech\ar\file;
use yii\base\InvalidConfigException;
use yii\imagine\Image;
/**
* ImageFileBehavior is an enhanced version of [[TransformFileBehavior]] developed for the managing image files.
* Behavior allows to set up several different transformations for image, so actually several files will be related to the one record in the database table.
* You can set up the [[transformCallback]] in order to specify transformation method(s).
* By default behavior resizes images, using [[Image::thumbnail()]].
*
* In order to specify image resizing, you should set [[fileTransformations]] field.
* For example:
*
* ```php
* [
* 'full' => [800, 600],
* 'thumbnail' => [200, 150]
* ];
* ```
*
* In order save original file without any transformations, set string value with native key.
* For example:
*
* ```php
* [
* 'origin',
* 'main' => [800, 600],
* 'thumbnail' => [200, 150]
* ];
* ```
*
* Note: you can always use [[saveFile()]] method to attach any file (not just uploaded one) to the model.
*
* Attention: this extension requires the extension "yiisoft/yii2-imagine" to be attached to the application!
*
* @see TransformFileBehavior
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 1.0
*/
class ImageFileBehavior extends TransformFileBehavior
{
/**
* @inheritdoc
*/
protected function transformFile($sourceFileName, $destinationFileName, $transformationSettings)
{
if ($this->transformCallback === null) {
return $this->transformImageFileResize($sourceFileName, $destinationFileName, $transformationSettings);
}
return parent::transformFile($sourceFileName, $destinationFileName, $transformationSettings);
}
/**
* Resizes source file to destination file according to the transformation settings, using [[Image::thumbnail()]].
* @param string $sourceFileName is the full source file system name.
* @param string $destinationFileName is the full destination file system name.
* @param array $transformSettings is the transform settings data, it should be the pair: 'imageWidth' and 'imageHeight',
* For example: `[800, 600]`
* @throws InvalidConfigException on invalid transform settings.
* @return bool success.
*/
protected function transformImageFileResize($sourceFileName, $destinationFileName, $transformSettings)
{
if (!is_array($transformSettings)) {
throw new InvalidConfigException('Wrong transform settings are passed to "' . get_class($this) . '::' . __FUNCTION__ . '"');
}
list($width, $height) = array_values($transformSettings);
Image::thumbnail($sourceFileName, $width, $height)->save($destinationFileName);
return true;
}
}