1 | <?php |
||
2 | /** |
||
3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
4 | * you may not use this file except in compliance with the License. |
||
5 | * You may obtain a copy of the License at |
||
6 | * |
||
7 | * @license http://www.apache.org/licenses/LICENSE-2.0 |
||
8 | * |
||
9 | * Unless required by applicable law or agreed to in writing, software |
||
10 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
12 | * See the License for the specific language governing permissions and |
||
13 | * limitations under the License. |
||
14 | * |
||
15 | * @author Alexander Zakharov <[email protected]> |
||
16 | * @link http://rootlocal.ru |
||
17 | * @copyright Copyright © 2018 rootlocal.ru |
||
18 | */ |
||
19 | |||
20 | namespace image\components; |
||
21 | |||
22 | use Yii; |
||
23 | use yii\base\BaseObject; |
||
24 | use yii\base\ErrorException; |
||
25 | |||
26 | /** |
||
27 | * Class Thumb |
||
28 | * @package image\components |
||
29 | * @property ThumbConfig $size |
||
30 | */ |
||
31 | class Thumb extends BaseObject |
||
32 | { |
||
33 | |||
34 | /** |
||
35 | * @var ThumbConfig |
||
36 | * @example |
||
37 | * 'config' => [ |
||
38 | * 'width' => 100, |
||
39 | * 'height' => 100, |
||
40 | * 'crop' => false, |
||
41 | * 'watermark' => true, |
||
42 | * 'watermarkOpacity' => 25, |
||
43 | * 'quality' => 100, |
||
44 | * ] |
||
45 | */ |
||
46 | public $config = []; |
||
47 | |||
48 | /** |
||
49 | * The File path to the Watermark File |
||
50 | * @var string |
||
51 | */ |
||
52 | public $watermarkFile = null; |
||
53 | |||
54 | /** |
||
55 | * The File path to the Image |
||
56 | * @var string |
||
57 | */ |
||
58 | public $file = null; |
||
59 | |||
60 | /** |
||
61 | * Out File path to the Image |
||
62 | * @var string |
||
63 | */ |
||
64 | public $outFile = null; |
||
65 | |||
66 | /** |
||
67 | * default driver: GD, Imagick |
||
68 | * @var string |
||
69 | */ |
||
70 | public $driver; |
||
71 | |||
72 | /** |
||
73 | * @var Thumb |
||
74 | */ |
||
75 | private static $_instance; |
||
76 | |||
77 | /** |
||
78 | * @param array $config |
||
79 | * @return Thumb |
||
80 | */ |
||
81 | public static function getInstance(array $config = []) |
||
82 | { |
||
83 | self::$_instance = new self($config); |
||
84 | return self::$_instance; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @inheritdoc |
||
89 | * @throws ErrorException |
||
90 | */ |
||
91 | public function init() |
||
92 | { |
||
93 | parent::init(); |
||
94 | |||
95 | if (empty($this->file)) |
||
96 | throw new ErrorException(Yii::t('image', 'No path to file specified "file"')); |
||
97 | if (empty($this->outFile)) |
||
98 | throw new ErrorException('No Out File path to file specified "outFile"'); |
||
99 | |||
100 | if (empty($this->watermarkFile)) |
||
101 | $this->config['watermark'] = false; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @return ThumbConfig |
||
106 | */ |
||
107 | private function getThumbConfig() |
||
108 | { |
||
109 | return new ThumbConfig($this->config); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Creating thumb |
||
114 | * @return string PATH to tmp file |
||
115 | * @throws ErrorException |
||
116 | */ |
||
117 | public function create() |
||
118 | { |
||
119 | $ImageDriver = new Image(['driver' => $this->driver]); |
||
120 | $ImageDriver = $ImageDriver->load($this->file); |
||
121 | |||
122 | $config = $this->getThumbConfig(); |
||
123 | $thisImageSize = getimagesize($this->file); |
||
124 | |||
125 | if ($thisImageSize[0] < $config->width) { |
||
126 | $ImageDriver->sharpen($config->width - $thisImageSize[0]); |
||
127 | } |
||
128 | |||
129 | $ImageDriver->resize(null, $config->height); |
||
130 | |||
131 | if ($config->crop) |
||
132 | $ImageDriver->crop($config->width, $config->height); |
||
133 | |||
134 | if ($config->watermark) { |
||
135 | $watermark = new Image(['driver' => $this->driver]); |
||
136 | $watermark = $watermark->load($this->watermarkFile); |
||
137 | $watermark->resize($ImageDriver->width, null); |
||
138 | $ImageDriver->watermark( |
||
139 | $watermark, null, null, |
||
140 | $config->watermarkOpacity |
||
141 | ); |
||
142 | |||
143 | unset($watermark); |
||
144 | } |
||
145 | |||
146 | $ImageDriver->resize($config->width, null); |
||
147 | |||
148 | if ($config->crop && $ImageDriver->height > $config->height) { |
||
149 | $ImageDriver->crop($config->width, $config->height); |
||
150 | } |
||
151 | |||
152 | if ($ImageDriver->save($this->outFile, $config->quality)) { |
||
153 | unset($ImageDriver); |
||
154 | return $this->outFile; |
||
155 | } |
||
156 | throw new ErrorException("error saving file: {$this->outFile}"); |
||
157 | } |
||
158 | |||
159 | } |