Commit 98e8e9e57d7e7b7c954dbbad7c6271da0dd1701b
1 parent
1f90c5f4
ArtboxImage uploader
Showing
17 changed files
with
172 additions
and
1960 deletions
Show diff stats
backend/config/main.php
backend/controllers/BrandController.php
... | ... | @@ -105,8 +105,8 @@ class BrandController extends Controller |
105 | 105 | public function actionUpdate($id) |
106 | 106 | { |
107 | 107 | $model = $this->findModel($id); |
108 | - | |
109 | 108 | if ($model->load(Yii::$app->request->post())) { |
109 | + var_dump($_POST, $_FILES);exit; | |
110 | 110 | if ( ($image = UploadedFile::getInstance($model, 'imageUpload')) ) { |
111 | 111 | $model->image = $image->name; |
112 | 112 | } | ... | ... |
backend/views/brand/_form.php
... | ... | @@ -18,18 +18,7 @@ use yii\widgets\ActiveForm; |
18 | 18 | |
19 | 19 | <?= $form->field($model, 'alias')->textInput(['maxlength' => true]) ?> |
20 | 20 | |
21 | - <?= $form->field($model, 'imageUpload')->widget(\kartik\file\FileInput::classname(), [ | |
22 | - 'options' => [ | |
23 | - 'accept' => 'image/*', | |
24 | - ], | |
25 | - 'pluginOptions' => [ | |
26 | - 'allowedFileExtensions' => ['jpg','gif','png'], | |
27 | - 'initialPreview' => $model->imageUrl ? Html::img($model->imageUrl) : '', | |
28 | - 'overwriteInitial' => true, | |
29 | - 'showRemove' => true, | |
30 | - 'showUpload' => false, | |
31 | - ], | |
32 | - ]); ?> | |
21 | + <?= \common\components\artboximage\ArtboxImageHelper::fileinputWidget($model, 'image');?> | |
33 | 22 | |
34 | 23 | <?= $form->field($model, 'meta_title')->textInput(['maxlength' => true]) ?> |
35 | 24 | ... | ... |
common/components/artboximage/ArtboxImage.php
... | ... | @@ -2,6 +2,7 @@ |
2 | 2 | |
3 | 3 | namespace common\components\artboximage; |
4 | 4 | |
5 | +use kartik\file\FileInput; | |
5 | 6 | use Yii; |
6 | 7 | use yii\base\Component; |
7 | 8 | use yii\base\ErrorException; |
... | ... | @@ -26,10 +27,38 @@ class ArtboxImage extends Component { |
26 | 27 | 'bmp' => 'bmp', |
27 | 28 | ]; |
28 | 29 | |
30 | + public $uploadUrl = '/admin/artboxfile/action/upload'; | |
31 | + | |
29 | 32 | public function load($file = null, $driver = null) { |
30 | 33 | if(empty($file) || !realpath($file)) { |
31 | 34 | throw new ErrorException('File name can not be empty and exists'); |
32 | 35 | } |
33 | 36 | return Image::factory($file, $driver ? $driver : $this->driver); |
34 | 37 | } |
38 | + | |
39 | + public function fileinputWidget($model, $modelField, $formField = 'fileUpload', $multiple = false, $imageOnly = true) { | |
40 | + $options = [ | |
41 | + 'multiple' => $multiple, | |
42 | + ]; | |
43 | + if ($imageOnly) { | |
44 | + $options['accept'] = 'image/*'; | |
45 | + } | |
46 | + return FileInput::widget([ | |
47 | + 'name' => $formField, | |
48 | + 'options' => $options, | |
49 | + 'pluginOptions' => [ | |
50 | + 'allowedFileExtensions' => array_keys($this->extensions), | |
51 | + // @todo set for multiple | |
52 | + 'initialPreview' => $model->{$modelField} ? Html::img($modelField) : '', | |
53 | + 'overwriteInitial' => !$multiple, | |
54 | + 'showRemove' => true, | |
55 | + 'showUpload' => false, | |
56 | + 'uploadUrl' => $this->uploadUrl, | |
57 | + 'uploadExtraData' => [ | |
58 | + 'fileField' => $modelField, | |
59 | + 'multiple' => intval($multiple), | |
60 | + ], | |
61 | + ], | |
62 | + ]); | |
63 | + } | |
35 | 64 | } |
36 | 65 | \ No newline at end of file | ... | ... |
common/components/artboximage/ArtboxImageHelper.php
... | ... | @@ -46,6 +46,10 @@ class ArtboxImageHelper extends Object { |
46 | 46 | return self::getPresetUrl($filePath, $preset, $preset_alias); |
47 | 47 | } |
48 | 48 | |
49 | + public static function fileinputWidget($model, $modelField, $formField = 'fileUpload', $multiple = false) { | |
50 | + return Yii::$app->artboximage->fileinputWidget($model, $modelField, $formField, $multiple); | |
51 | + } | |
52 | + | |
49 | 53 | private static function getPathFromUrl($url) { |
50 | 54 | return substr_replace($url, self::getDriver()->rootPath, 0, strlen(self::getDriver()->rootUrl)); |
51 | 55 | } | ... | ... |
common/components/artboximage/drivers/Image.php deleted
common/components/artboximage/drivers/Image/GD.php deleted
1 | -<?php | |
2 | - | |
3 | -namespace common\components\artboximage\drivers\Image; | |
4 | - | |
5 | -use common\components\artboximage\drivers\Kohana\Image\Kohana_Image_GD; | |
6 | - | |
7 | -/** | |
8 | - * Support for image manipulation using [Imagick](http://php.net/Imagick). | |
9 | - * | |
10 | - * @package Kohana/Image | |
11 | - * @category Drivers | |
12 | - * @author Tamas Mihalik tamas.mihalik@gmail.com | |
13 | - * @copyright (c) 2009-2012 Kohana Team | |
14 | - * @license http://kohanaphp.com/license.html | |
15 | - */ | |
16 | -class Image_GD extends Kohana_Image_GD {} | |
17 | - | |
18 | -?> | |
19 | 0 | \ No newline at end of file |
common/components/artboximage/drivers/Image/Imagick.php deleted
1 | -<?php | |
2 | - | |
3 | -namespace common\components\artboximage\drivers\Image; | |
4 | -use common\components\artboximage\drivers\Kohana\Image\Kohana_Image_Imagick; | |
5 | - | |
6 | -/** | |
7 | - * Support for image manipulation using [Imagick](http://php.net/Imagick). | |
8 | - * | |
9 | - * @package Kohana/Image | |
10 | - * @category Drivers | |
11 | - * @author Tamas Mihalik tamas.mihalik@gmail.com | |
12 | - * @copyright (c) 2009-2012 Kohana Team | |
13 | - * @license http://kohanaphp.com/license.html | |
14 | - */ | |
15 | -class Image_Imagick extends Kohana_Image_Imagick {} | |
16 | 0 | \ No newline at end of file |
common/components/artboximage/drivers/Kohana/Image/GD.php deleted
1 | -<?php | |
2 | -namespace common\components\artboximage\drivers\Kohana\Image; | |
3 | - | |
4 | -use common\components\artboximage\drivers\Kohana\Kohana_Image; | |
5 | -use yii\base\ErrorException; | |
6 | -/** | |
7 | - * Support for image manipulation using [GD](http://php.net/GD). | |
8 | - * | |
9 | - * @package Kohana/Image | |
10 | - * @category Drivers | |
11 | - * @author Kohana Team | |
12 | - * @copyright (c) 2008-2009 Kohana Team | |
13 | - * @license http://kohanaphp.com/license.html | |
14 | - */ | |
15 | -class Kohana_Image_GD extends Kohana_Image { | |
16 | - | |
17 | - // Which GD functions are available? | |
18 | - const IMAGEROTATE = 'imagerotate'; | |
19 | - const IMAGECONVOLUTION = 'imageconvolution'; | |
20 | - const IMAGEFILTER = 'imagefilter'; | |
21 | - const IMAGELAYEREFFECT = 'imagelayereffect'; | |
22 | - protected static $_available_functions = array(); | |
23 | - | |
24 | - /** | |
25 | - * Checks if GD is enabled and verify that key methods exist, some of which require GD to | |
26 | - * be bundled with PHP. Exceptions will be thrown from those methods when GD is not | |
27 | - * bundled. | |
28 | - * | |
29 | - * @return boolean | |
30 | - */ | |
31 | - public static function check() | |
32 | - { | |
33 | - if ( ! function_exists('gd_info')) | |
34 | - { | |
35 | - throw new ErrorException('GD is either not installed or not enabled, check your configuration'); | |
36 | - } | |
37 | - $functions = array( | |
38 | - Image_GD::IMAGEROTATE, | |
39 | - Image_GD::IMAGECONVOLUTION, | |
40 | - Image_GD::IMAGEFILTER, | |
41 | - Image_GD::IMAGELAYEREFFECT | |
42 | - ); | |
43 | - foreach ($functions as $function) | |
44 | - { | |
45 | - Image_GD::$_available_functions[$function] = function_exists($function); | |
46 | - } | |
47 | - | |
48 | - if (defined('GD_VERSION')) | |
49 | - { | |
50 | - // Get the version via a constant, available in PHP 5.2.4+ | |
51 | - $version = GD_VERSION; | |
52 | - } | |
53 | - else | |
54 | - { | |
55 | - // Get the version information | |
56 | - $info = gd_info(); | |
57 | - | |
58 | - // Extract the version number | |
59 | - preg_match('/\d+\.\d+(?:\.\d+)?/', $info['GD Version'], $matches); | |
60 | - | |
61 | - // Get the major version | |
62 | - $version = $matches[0]; | |
63 | - } | |
64 | - | |
65 | - if ( ! version_compare($version, '2.0.1', '>=')) | |
66 | - { | |
67 | - throw new ErrorException(sprintf('Image_GD requires GD version 2.0.1 or greater, you have %s',$version)); | |
68 | - } | |
69 | - | |
70 | - return Image_GD::$_checked = TRUE; | |
71 | - } | |
72 | - | |
73 | - /* @var resource Temporary image resource */ | |
74 | - protected $_image; | |
75 | - | |
76 | - /* @var string Function name to open Image */ | |
77 | - protected $_create_function; | |
78 | - | |
79 | - /** | |
80 | - * Runs [Image_GD::check] and loads the image. | |
81 | - * | |
82 | - * @param string $file image file path | |
83 | - * @return void | |
84 | - * @throws ErrorException | |
85 | - */ | |
86 | - public function __construct($file) | |
87 | - { | |
88 | - if ( ! Image_GD::$_checked) | |
89 | - { | |
90 | - // Run the install check | |
91 | - Image_GD::check(); | |
92 | - } | |
93 | - | |
94 | - parent::__construct($file); | |
95 | - | |
96 | - // Set the image creation function name | |
97 | - switch ($this->type) | |
98 | - { | |
99 | - case IMAGETYPE_JPEG: | |
100 | - $create = 'imagecreatefromjpeg'; | |
101 | - break; | |
102 | - case IMAGETYPE_GIF: | |
103 | - $create = 'imagecreatefromgif'; | |
104 | - break; | |
105 | - case IMAGETYPE_PNG: | |
106 | - $create = 'imagecreatefrompng'; | |
107 | - break; | |
108 | - } | |
109 | - | |
110 | - if ( ! isset($create) OR ! function_exists($create)) | |
111 | - { | |
112 | - throw new ErrorException(sprintf('Installed GD does not support %s images',image_type_to_extension($this->type, FALSE))); | |
113 | - } | |
114 | - | |
115 | - // Save function for future use | |
116 | - $this->_create_function = $create; | |
117 | - | |
118 | - // Save filename for lazy loading | |
119 | - $this->_image = $this->file; | |
120 | - } | |
121 | - | |
122 | - /** | |
123 | - * Destroys the loaded image to free up resources. | |
124 | - * | |
125 | - * @return void | |
126 | - */ | |
127 | - public function __destruct() | |
128 | - { | |
129 | - if (is_resource($this->_image)) | |
130 | - { | |
131 | - // Free all resources | |
132 | - imagedestroy($this->_image); | |
133 | - } | |
134 | - } | |
135 | - | |
136 | - /** | |
137 | - * Loads an image into GD. | |
138 | - * | |
139 | - * @return void | |
140 | - */ | |
141 | - protected function _load_image() | |
142 | - { | |
143 | - if ( ! is_resource($this->_image)) | |
144 | - { | |
145 | - // Gets create function | |
146 | - $create = $this->_create_function; | |
147 | - | |
148 | - // Open the temporary image | |
149 | - $this->_image = $create($this->file); | |
150 | - | |
151 | - // Preserve transparency when saving | |
152 | - imagesavealpha($this->_image, TRUE); | |
153 | - } | |
154 | - } | |
155 | - | |
156 | - /** | |
157 | - * Execute a resize. | |
158 | - * | |
159 | - * @param integer $width new width | |
160 | - * @param integer $height new height | |
161 | - * @return void | |
162 | - */ | |
163 | - protected function _do_resize($width, $height) | |
164 | - { | |
165 | - // Presize width and height | |
166 | - $pre_width = $this->width; | |
167 | - $pre_height = $this->height; | |
168 | - | |
169 | - // Loads image if not yet loaded | |
170 | - $this->_load_image(); | |
171 | - | |
172 | - // Test if we can do a resize without resampling to speed up the final resize | |
173 | - if ($width > ($this->width / 2) AND $height > ($this->height / 2)) | |
174 | - { | |
175 | - // The maximum reduction is 10% greater than the final size | |
176 | - $reduction_width = round($width * 1.1); | |
177 | - $reduction_height = round($height * 1.1); | |
178 | - | |
179 | - while ($pre_width / 2 > $reduction_width AND $pre_height / 2 > $reduction_height) | |
180 | - { | |
181 | - // Reduce the size using an O(2n) algorithm, until it reaches the maximum reduction | |
182 | - $pre_width /= 2; | |
183 | - $pre_height /= 2; | |
184 | - } | |
185 | - | |
186 | - // Create the temporary image to copy to | |
187 | - $image = $this->_create($pre_width, $pre_height); | |
188 | - | |
189 | - if (imagecopyresized($image, $this->_image, 0, 0, 0, 0, $pre_width, $pre_height, $this->width, $this->height)) | |
190 | - { | |
191 | - // Swap the new image for the old one | |
192 | - imagedestroy($this->_image); | |
193 | - $this->_image = $image; | |
194 | - } | |
195 | - } | |
196 | - | |
197 | - // Create the temporary image to copy to | |
198 | - $image = $this->_create($width, $height); | |
199 | - | |
200 | - // Execute the resize | |
201 | - if (imagecopyresampled($image, $this->_image, 0, 0, 0, 0, $width, $height, $pre_width, $pre_height)) | |
202 | - { | |
203 | - // Swap the new image for the old one | |
204 | - imagedestroy($this->_image); | |
205 | - $this->_image = $image; | |
206 | - | |
207 | - // Reset the width and height | |
208 | - $this->width = imagesx($image); | |
209 | - $this->height = imagesy($image); | |
210 | - } | |
211 | - } | |
212 | - | |
213 | - /** | |
214 | - * Adaptation the image. | |
215 | - * | |
216 | - * @param integer $width image width | |
217 | - * @param integer $height image height | |
218 | - * @param integer $bg_width background width | |
219 | - * @param integer $bg_height background height | |
220 | - * @param integer $offset_x offset from the left | |
221 | - * @param integer $offset_y offset from the top | |
222 | - */ | |
223 | - protected function _do_adapt($width, $height, $bg_width, $bg_height, $offset_x, $offset_y) | |
224 | - { | |
225 | - $this->_load_image(); | |
226 | - $image = $this->_image; | |
227 | - $this->_image = $this->_create($bg_width, $bg_height); | |
228 | - $this->width = $bg_width; | |
229 | - $this->height = $bg_height; | |
230 | - imagealphablending($this->_image, false); | |
231 | - $col = imagecolorallocatealpha($this->_image, 0, 255, 0, 127); | |
232 | - imagefilledrectangle($this->_image, 0, 0, $bg_width, $bg_height, $col); | |
233 | - imagealphablending($this->_image, true); | |
234 | - imagecopy($this->_image, $image, $offset_x, $offset_y, 0, 0, $width, $height); | |
235 | - imagealphablending($this->_image, false); | |
236 | - imagesavealpha($this->_image, true); | |
237 | - imagedestroy($image); | |
238 | - } | |
239 | - | |
240 | - /** | |
241 | - * Execute a crop. | |
242 | - * | |
243 | - * @param integer $width new width | |
244 | - * @param integer $height new height | |
245 | - * @param integer $offset_x offset from the left | |
246 | - * @param integer $offset_y offset from the top | |
247 | - * @return void | |
248 | - */ | |
249 | - protected function _do_crop($width, $height, $offset_x, $offset_y) | |
250 | - { | |
251 | - // Create the temporary image to copy to | |
252 | - $image = $this->_create($width, $height); | |
253 | - | |
254 | - // Loads image if not yet loaded | |
255 | - $this->_load_image(); | |
256 | - | |
257 | - // Execute the crop | |
258 | - if (imagecopyresampled($image, $this->_image, 0, 0, $offset_x, $offset_y, $width, $height, $width, $height)) | |
259 | - { | |
260 | - // Swap the new image for the old one | |
261 | - imagedestroy($this->_image); | |
262 | - $this->_image = $image; | |
263 | - | |
264 | - // Reset the width and height | |
265 | - $this->width = imagesx($image); | |
266 | - $this->height = imagesy($image); | |
267 | - } | |
268 | - } | |
269 | - | |
270 | - /** | |
271 | - * Execute a rotation. | |
272 | - * | |
273 | - * @param integer $degrees degrees to rotate | |
274 | - * @return void | |
275 | - */ | |
276 | - protected function _do_rotate($degrees) | |
277 | - { | |
278 | - if (empty(Image_GD::$_available_functions[Image_GD::IMAGEROTATE])) | |
279 | - { | |
280 | - throw new ErrorException('This method requires imagerotate, which is only available in the bundled version of GD'); | |
281 | - } | |
282 | - | |
283 | - // Loads image if not yet loaded | |
284 | - $this->_load_image(); | |
285 | - | |
286 | - // Transparent black will be used as the background for the uncovered region | |
287 | - $transparent = imagecolorallocatealpha($this->_image, 0, 0, 0, 127); | |
288 | - | |
289 | - // Rotate, setting the transparent color | |
290 | - $image = imagerotate($this->_image, 360 - $degrees, $transparent, 1); | |
291 | - | |
292 | - // Save the alpha of the rotated image | |
293 | - imagesavealpha($image, TRUE); | |
294 | - | |
295 | - // Get the width and height of the rotated image | |
296 | - $width = imagesx($image); | |
297 | - $height = imagesy($image); | |
298 | - | |
299 | - if (imagecopymerge($this->_image, $image, 0, 0, 0, 0, $width, $height, 100)) | |
300 | - { | |
301 | - // Swap the new image for the old one | |
302 | - imagedestroy($this->_image); | |
303 | - $this->_image = $image; | |
304 | - | |
305 | - // Reset the width and height | |
306 | - $this->width = $width; | |
307 | - $this->height = $height; | |
308 | - } | |
309 | - } | |
310 | - | |
311 | - /** | |
312 | - * Execute a flip. | |
313 | - * | |
314 | - * @param integer $direction direction to flip | |
315 | - * @return void | |
316 | - */ | |
317 | - protected function _do_flip($direction) | |
318 | - { | |
319 | - // Create the flipped image | |
320 | - $flipped = $this->_create($this->width, $this->height); | |
321 | - | |
322 | - // Loads image if not yet loaded | |
323 | - $this->_load_image(); | |
324 | - | |
325 | - if ($direction === Image::HORIZONTAL) | |
326 | - { | |
327 | - for ($x = 0; $x < $this->width; $x++) | |
328 | - { | |
329 | - // Flip each row from top to bottom | |
330 | - imagecopy($flipped, $this->_image, $x, 0, $this->width - $x - 1, 0, 1, $this->height); | |
331 | - } | |
332 | - } | |
333 | - else | |
334 | - { | |
335 | - for ($y = 0; $y < $this->height; $y++) | |
336 | - { | |
337 | - // Flip each column from left to right | |
338 | - imagecopy($flipped, $this->_image, 0, $y, 0, $this->height - $y - 1, $this->width, 1); | |
339 | - } | |
340 | - } | |
341 | - | |
342 | - // Swap the new image for the old one | |
343 | - imagedestroy($this->_image); | |
344 | - $this->_image = $flipped; | |
345 | - | |
346 | - // Reset the width and height | |
347 | - $this->width = imagesx($flipped); | |
348 | - $this->height = imagesy($flipped); | |
349 | - } | |
350 | - | |
351 | - /** | |
352 | - * Execute a sharpen. | |
353 | - * | |
354 | - * @param integer $amount amount to sharpen | |
355 | - * @return void | |
356 | - */ | |
357 | - protected function _do_sharpen($amount) | |
358 | - { | |
359 | - if (empty(Image_GD::$_available_functions[Image_GD::IMAGECONVOLUTION])) | |
360 | - { | |
361 | - throw new ErrorException('This method requires imageconvolution, which is only available in the bundled version of GD'); | |
362 | - } | |
363 | - | |
364 | - // Loads image if not yet loaded | |
365 | - $this->_load_image(); | |
366 | - | |
367 | - // Amount should be in the range of 18-10 | |
368 | - $amount = round(abs(-18 + ($amount * 0.08)), 2); | |
369 | - | |
370 | - // Gaussian blur matrix | |
371 | - $matrix = array | |
372 | - ( | |
373 | - array(-1, -1, -1), | |
374 | - array(-1, $amount, -1), | |
375 | - array(-1, -1, -1), | |
376 | - ); | |
377 | - | |
378 | - // Perform the sharpen | |
379 | - if (imageconvolution($this->_image, $matrix, $amount - 8, 0)) | |
380 | - { | |
381 | - // Reset the width and height | |
382 | - $this->width = imagesx($this->_image); | |
383 | - $this->height = imagesy($this->_image); | |
384 | - } | |
385 | - } | |
386 | - | |
387 | - /** | |
388 | - * Execute a reflection. | |
389 | - * | |
390 | - * @param integer $height reflection height | |
391 | - * @param integer $opacity reflection opacity | |
392 | - * @param boolean $fade_in TRUE to fade out, FALSE to fade in | |
393 | - * @return void | |
394 | - */ | |
395 | - protected function _do_reflection($height, $opacity, $fade_in) | |
396 | - { | |
397 | - if (empty(Image_GD::$_available_functions[Image_GD::IMAGEFILTER])) | |
398 | - { | |
399 | - throw new ErrorException('This method requires imagefilter, which is only available in the bundled version of GD'); | |
400 | - } | |
401 | - | |
402 | - // Loads image if not yet loaded | |
403 | - $this->_load_image(); | |
404 | - | |
405 | - // Convert an opacity range of 0-100 to 127-0 | |
406 | - $opacity = round(abs(($opacity * 127 / 100) - 127)); | |
407 | - | |
408 | - if ($opacity < 127) | |
409 | - { | |
410 | - // Calculate the opacity stepping | |
411 | - $stepping = (127 - $opacity) / $height; | |
412 | - } | |
413 | - else | |
414 | - { | |
415 | - // Avoid a "divide by zero" error | |
416 | - $stepping = 127 / $height; | |
417 | - } | |
418 | - | |
419 | - // Create the reflection image | |
420 | - $reflection = $this->_create($this->width, $this->height + $height); | |
421 | - | |
422 | - // Copy the image to the reflection | |
423 | - imagecopy($reflection, $this->_image, 0, 0, 0, 0, $this->width, $this->height); | |
424 | - | |
425 | - for ($offset = 0; $height >= $offset; $offset++) | |
426 | - { | |
427 | - // Read the next line down | |
428 | - $src_y = $this->height - $offset - 1; | |
429 | - | |
430 | - // Place the line at the bottom of the reflection | |
431 | - $dst_y = $this->height + $offset; | |
432 | - | |
433 | - if ($fade_in === TRUE) | |
434 | - { | |
435 | - // Start with the most transparent line first | |
436 | - $dst_opacity = round($opacity + ($stepping * ($height - $offset))); | |
437 | - } | |
438 | - else | |
439 | - { | |
440 | - // Start with the most opaque line first | |
441 | - $dst_opacity = round($opacity + ($stepping * $offset)); | |
442 | - } | |
443 | - | |
444 | - // Create a single line of the image | |
445 | - $line = $this->_create($this->width, 1); | |
446 | - | |
447 | - // Copy a single line from the current image into the line | |
448 | - imagecopy($line, $this->_image, 0, 0, 0, $src_y, $this->width, 1); | |
449 | - | |
450 | - // Colorize the line to add the correct alpha level | |
451 | - imagefilter($line, IMG_FILTER_COLORIZE, 0, 0, 0, $dst_opacity); | |
452 | - | |
453 | - // Copy a the line into the reflection | |
454 | - imagecopy($reflection, $line, 0, $dst_y, 0, 0, $this->width, 1); | |
455 | - } | |
456 | - | |
457 | - // Swap the new image for the old one | |
458 | - imagedestroy($this->_image); | |
459 | - $this->_image = $reflection; | |
460 | - | |
461 | - // Reset the width and height | |
462 | - $this->width = imagesx($reflection); | |
463 | - $this->height = imagesy($reflection); | |
464 | - } | |
465 | - | |
466 | - /** | |
467 | - * Execute a watermarking. | |
468 | - * | |
469 | - * @param Kohana_Image $image watermarking Kohana_Image | |
470 | - * @param integer $offset_x offset from the left | |
471 | - * @param integer $offset_y offset from the top | |
472 | - * @param integer $opacity opacity of watermark | |
473 | - * @return void | |
474 | - */ | |
475 | - protected function _do_watermark(Kohana_Image $watermark, $offset_x, $offset_y, $opacity) | |
476 | - { | |
477 | - if (empty(Image_GD::$_available_functions[Image_GD::IMAGELAYEREFFECT])) | |
478 | - { | |
479 | - throw new ErrorException('This method requires imagelayereffect, which is only available in the bundled version of GD'); | |
480 | - } | |
481 | - | |
482 | - // Loads image if not yet loaded | |
483 | - $this->_load_image(); | |
484 | - | |
485 | - // Create the watermark image resource | |
486 | - $overlay = imagecreatefromstring($watermark->render()); | |
487 | - | |
488 | - imagesavealpha($overlay, TRUE); | |
489 | - | |
490 | - // Get the width and height of the watermark | |
491 | - $width = imagesx($overlay); | |
492 | - $height = imagesy($overlay); | |
493 | - | |
494 | - if ($opacity < 100) | |
495 | - { | |
496 | - // Convert an opacity range of 0-100 to 127-0 | |
497 | - $opacity = round(abs(($opacity * 127 / 100) - 127)); | |
498 | - | |
499 | - // Allocate transparent gray | |
500 | - $color = imagecolorallocatealpha($overlay, 127, 127, 127, $opacity); | |
501 | - | |
502 | - // The transparent image will overlay the watermark | |
503 | - imagelayereffect($overlay, IMG_EFFECT_OVERLAY); | |
504 | - | |
505 | - // Fill the background with the transparent color | |
506 | - imagefilledrectangle($overlay, 0, 0, $width, $height, $color); | |
507 | - } | |
508 | - | |
509 | - // Alpha blending must be enabled on the background! | |
510 | - imagealphablending($this->_image, TRUE); | |
511 | - | |
512 | - if (imagecopy($this->_image, $overlay, $offset_x, $offset_y, 0, 0, $width, $height)) | |
513 | - { | |
514 | - // Destroy the overlay image | |
515 | - imagedestroy($overlay); | |
516 | - } | |
517 | - } | |
518 | - | |
519 | - /** | |
520 | - * Execute a background. | |
521 | - * | |
522 | - * @param integer $r red | |
523 | - * @param integer $g green | |
524 | - * @param integer $b blue | |
525 | - * @param integer $opacity opacity | |
526 | - * @return void | |
527 | - */ | |
528 | - protected function _do_background($r, $g, $b, $opacity) | |
529 | - { | |
530 | - // Loads image if not yet loaded | |
531 | - $this->_load_image(); | |
532 | - | |
533 | - // Convert an opacity range of 0-100 to 127-0 | |
534 | - $opacity = round(abs(($opacity * 127 / 100) - 127)); | |
535 | - | |
536 | - // Create a new background | |
537 | - $background = $this->_create($this->width, $this->height); | |
538 | - | |
539 | - // Allocate the color | |
540 | - $color = imagecolorallocatealpha($background, $r, $g, $b, $opacity); | |
541 | - | |
542 | - // Fill the image with white | |
543 | - imagefilledrectangle($background, 0, 0, $this->width, $this->height, $color); | |
544 | - | |
545 | - // Alpha blending must be enabled on the background! | |
546 | - imagealphablending($background, TRUE); | |
547 | - | |
548 | - // Copy the image onto a white background to remove all transparency | |
549 | - if (imagecopy($background, $this->_image, 0, 0, 0, 0, $this->width, $this->height)) | |
550 | - { | |
551 | - // Swap the new image for the old one | |
552 | - imagedestroy($this->_image); | |
553 | - $this->_image = $background; | |
554 | - } | |
555 | - } | |
556 | - | |
557 | - /** | |
558 | - * Execute a save. | |
559 | - * | |
560 | - * @param string $file new image filename | |
561 | - * @param integer $quality quality | |
562 | - * @return boolean | |
563 | - */ | |
564 | - protected function _do_save($file, $quality) | |
565 | - { | |
566 | - // Loads image if not yet loaded | |
567 | - $this->_load_image(); | |
568 | - | |
569 | - // Get the extension of the file | |
570 | - $extension = pathinfo($file, PATHINFO_EXTENSION); | |
571 | - | |
572 | - // Get the save function and IMAGETYPE | |
573 | - list($save, $type) = $this->_save_function($extension, $quality); | |
574 | - | |
575 | - // Save the image to a file | |
576 | - $status = isset($quality) ? $save($this->_image, $file, $quality) : $save($this->_image, $file); | |
577 | - | |
578 | - if ($status === TRUE AND $type !== $this->type) | |
579 | - { | |
580 | - // Reset the image type and mime type | |
581 | - $this->type = $type; | |
582 | - $this->mime = image_type_to_mime_type($type); | |
583 | - } | |
584 | - | |
585 | - return TRUE; | |
586 | - } | |
587 | - | |
588 | - /** | |
589 | - * Execute a render. | |
590 | - * | |
591 | - * @param string $type image type: png, jpg, gif, etc | |
592 | - * @param integer $quality quality | |
593 | - * @return string | |
594 | - */ | |
595 | - protected function _do_render($type, $quality) | |
596 | - { | |
597 | - // Loads image if not yet loaded | |
598 | - $this->_load_image(); | |
599 | - | |
600 | - // Get the save function and IMAGETYPE | |
601 | - list($save, $type) = $this->_save_function($type, $quality); | |
602 | - | |
603 | - // Capture the output | |
604 | - ob_start(); | |
605 | - | |
606 | - // Render the image | |
607 | - $status = isset($quality) ? $save($this->_image, NULL, $quality) : $save($this->_image, NULL); | |
608 | - | |
609 | - if ($status === TRUE AND $type !== $this->type) | |
610 | - { | |
611 | - // Reset the image type and mime type | |
612 | - $this->type = $type; | |
613 | - $this->mime = image_type_to_mime_type($type); | |
614 | - } | |
615 | - | |
616 | - return ob_get_clean(); | |
617 | - } | |
618 | - | |
619 | - /** | |
620 | - * Get the GD saving function and image type for this extension. | |
621 | - * Also normalizes the quality setting | |
622 | - * | |
623 | - * @param string $extension image type: png, jpg, etc | |
624 | - * @param integer $quality image quality | |
625 | - * @return array save function, IMAGETYPE_* constant | |
626 | - * @throws ErrorException | |
627 | - */ | |
628 | - protected function _save_function($extension, & $quality) | |
629 | - { | |
630 | - if ( ! $extension) | |
631 | - { | |
632 | - // Use the current image type | |
633 | - $extension = image_type_to_extension($this->type, FALSE); | |
634 | - } | |
635 | - | |
636 | - switch (strtolower($extension)) | |
637 | - { | |
638 | - case 'jpg': | |
639 | - case 'jpeg': | |
640 | - // Save a JPG file | |
641 | - $save = 'imagejpeg'; | |
642 | - $type = IMAGETYPE_JPEG; | |
643 | - break; | |
644 | - case 'gif': | |
645 | - // Save a GIF file | |
646 | - $save = 'imagegif'; | |
647 | - $type = IMAGETYPE_GIF; | |
648 | - | |
649 | - // GIFs do not a quality setting | |
650 | - $quality = NULL; | |
651 | - break; | |
652 | - case 'png': | |
653 | - // Save a PNG file | |
654 | - $save = 'imagepng'; | |
655 | - $type = IMAGETYPE_PNG; | |
656 | - | |
657 | - // Use a compression level of 9 (does not affect quality!) | |
658 | - $quality = 9; | |
659 | - break; | |
660 | - default: | |
661 | - throw new ErrorException(sprintf('Installed GD does not support %s images',$extension)); | |
662 | - break; | |
663 | - } | |
664 | - | |
665 | - return array($save, $type); | |
666 | - } | |
667 | - | |
668 | - /** | |
669 | - * Create an empty image with the given width and height. | |
670 | - * | |
671 | - * @param integer $width image width | |
672 | - * @param integer $height image height | |
673 | - * @return resource | |
674 | - */ | |
675 | - protected function _create($width, $height) | |
676 | - { | |
677 | - // Create an empty image | |
678 | - $image = imagecreatetruecolor($width, $height); | |
679 | - | |
680 | - // Do not apply alpha blending | |
681 | - imagealphablending($image, FALSE); | |
682 | - | |
683 | - // Save alpha levels | |
684 | - imagesavealpha($image, TRUE); | |
685 | - | |
686 | - return $image; | |
687 | - } | |
688 | - | |
689 | -} // End Image_GD |
common/components/artboximage/drivers/Kohana/Image/Imagick.php deleted
1 | -<?php | |
2 | -namespace common\components\artboximage\drivers\Kohana\Image; | |
3 | - | |
4 | -use common\components\artboximage\drivers\Kohana\Kohana_Image; | |
5 | -use yii\base\ErrorException; | |
6 | -use \Imagick; | |
7 | -use \ImagickPixel; | |
8 | -/** | |
9 | - * Support for image manipulation using [Imagick](http://php.net/Imagick). | |
10 | - * | |
11 | - * @package Kohana/Image | |
12 | - * @category Drivers | |
13 | - * @author Tamas Mihalik tamas.mihalik@gmail.com | |
14 | - * @copyright (c) 2009-2012 Kohana Team | |
15 | - * @license http://kohanaphp.com/license.html | |
16 | - */ | |
17 | - | |
18 | -class Kohana_Image_Imagick extends Kohana_Image { | |
19 | - | |
20 | - /** | |
21 | - * @var Imagick image magick object | |
22 | - */ | |
23 | - protected $im; | |
24 | - | |
25 | - /** | |
26 | - * Checks if ImageMagick is enabled. | |
27 | - * | |
28 | - * @throws ErrorException | |
29 | - * @return boolean | |
30 | - */ | |
31 | - public static function check() | |
32 | - { | |
33 | - if ( ! extension_loaded('imagick')) | |
34 | - { | |
35 | - throw new ErrorException('Imagick is not installed, or the extension is not loaded'); | |
36 | - } | |
37 | - | |
38 | - return Image_Imagick::$_checked = TRUE; | |
39 | - } | |
40 | - | |
41 | - /** | |
42 | - * Runs [Image_Imagick::check] and loads the image. | |
43 | - * | |
44 | - * @return void | |
45 | - * @throws ErrorException | |
46 | - */ | |
47 | - public function __construct($file) | |
48 | - { | |
49 | - if ( ! Image_Imagick::$_checked) | |
50 | - { | |
51 | - // Run the install check | |
52 | - Image_Imagick::check(); | |
53 | - } | |
54 | - | |
55 | - parent::__construct($file); | |
56 | - | |
57 | - $this->im = new Imagick; | |
58 | - $this->im->readImage($file); | |
59 | - | |
60 | - if ( ! $this->im->getImageAlphaChannel()) | |
61 | - { | |
62 | - // Force the image to have an alpha channel | |
63 | - $this->im->setImageAlphaChannel(Imagick::ALPHACHANNEL_SET); | |
64 | - } | |
65 | - } | |
66 | - | |
67 | - /** | |
68 | - * Destroys the loaded image to free up resources. | |
69 | - * | |
70 | - * @return void | |
71 | - */ | |
72 | - public function __destruct() | |
73 | - { | |
74 | - $this->im->clear(); | |
75 | - $this->im->destroy(); | |
76 | - } | |
77 | - | |
78 | - protected function _do_resize($width, $height) | |
79 | - { | |
80 | - if ($this->im->scaleImage($width, $height)) | |
81 | - { | |
82 | - // Reset the width and height | |
83 | - $this->width = $this->im->getImageWidth(); | |
84 | - $this->height = $this->im->getImageHeight(); | |
85 | - | |
86 | - return TRUE; | |
87 | - } | |
88 | - | |
89 | - return FALSE; | |
90 | - } | |
91 | - | |
92 | - /** | |
93 | - * Adaptation the image. | |
94 | - * | |
95 | - * @param integer $width image width | |
96 | - * @param integer $height image height | |
97 | - * @param integer $bg_width background width | |
98 | - * @param integer $bg_height background height | |
99 | - * @param integer $offset_x offset from the left | |
100 | - * @param integer $offset_y offset from the top | |
101 | - */ | |
102 | - protected function _do_adapt($width, $height, $bg_width, $bg_height, $offset_x, $offset_y) | |
103 | - { | |
104 | - $image = new Imagick(); | |
105 | - $image->newImage($bg_width, $bg_height, "none"); | |
106 | - $image->compositeImage($this->im, Imagick::COMPOSITE_ADD, $offset_x, $offset_y); | |
107 | - $this->im->clear(); | |
108 | - $this->im->destroy(); | |
109 | - $this->im = $image; | |
110 | - $this->width = $bg_width; | |
111 | - $this->height = $bg_height; | |
112 | - } | |
113 | - | |
114 | - protected function _do_crop($width, $height, $offset_x, $offset_y) | |
115 | - { | |
116 | - if ($this->im->cropImage($width, $height, $offset_x, $offset_y)) | |
117 | - { | |
118 | - // Reset the width and height | |
119 | - $this->width = $this->im->getImageWidth(); | |
120 | - $this->height = $this->im->getImageHeight(); | |
121 | - | |
122 | - // Trim off hidden areas | |
123 | - $this->im->setImagePage($this->width, $this->height, 0, 0); | |
124 | - | |
125 | - return TRUE; | |
126 | - } | |
127 | - | |
128 | - return FALSE; | |
129 | - } | |
130 | - | |
131 | - protected function _do_rotate($degrees) | |
132 | - { | |
133 | - if ($this->im->rotateImage(new ImagickPixel('transparent'), $degrees)) | |
134 | - { | |
135 | - // Reset the width and height | |
136 | - $this->width = $this->im->getImageWidth(); | |
137 | - $this->height = $this->im->getImageHeight(); | |
138 | - | |
139 | - // Trim off hidden areas | |
140 | - $this->im->setImagePage($this->width, $this->height, 0, 0); | |
141 | - | |
142 | - return TRUE; | |
143 | - } | |
144 | - | |
145 | - return FALSE; | |
146 | - } | |
147 | - | |
148 | - protected function _do_flip($direction) | |
149 | - { | |
150 | - if ($direction === Image::HORIZONTAL) | |
151 | - { | |
152 | - return $this->im->flopImage(); | |
153 | - } | |
154 | - else | |
155 | - { | |
156 | - return $this->im->flipImage(); | |
157 | - } | |
158 | - } | |
159 | - | |
160 | - protected function _do_sharpen($amount) | |
161 | - { | |
162 | - // IM not support $amount under 5 (0.15) | |
163 | - $amount = ($amount < 5) ? 5 : $amount; | |
164 | - | |
165 | - // Amount should be in the range of 0.0 to 3.0 | |
166 | - $amount = ($amount * 3.0) / 100; | |
167 | - | |
168 | - return $this->im->sharpenImage(0, $amount); | |
169 | - } | |
170 | - | |
171 | - protected function _do_reflection($height, $opacity, $fade_in) | |
172 | - { | |
173 | - // Clone the current image and flip it for reflection | |
174 | - $reflection = $this->im->clone(); | |
175 | - $reflection->flipImage(); | |
176 | - | |
177 | - // Crop the reflection to the selected height | |
178 | - $reflection->cropImage($this->width, $height, 0, 0); | |
179 | - $reflection->setImagePage($this->width, $height, 0, 0); | |
180 | - | |
181 | - // Select the fade direction | |
182 | - $direction = array('transparent', 'black'); | |
183 | - | |
184 | - if ($fade_in) | |
185 | - { | |
186 | - // Change the direction of the fade | |
187 | - $direction = array_reverse($direction); | |
188 | - } | |
189 | - | |
190 | - // Create a gradient for fading | |
191 | - $fade = new Imagick; | |
192 | - $fade->newPseudoImage($reflection->getImageWidth(), $reflection->getImageHeight(), vsprintf('gradient:%s-%s', $direction)); | |
193 | - | |
194 | - // Apply the fade alpha channel to the reflection | |
195 | - $reflection->compositeImage($fade, Imagick::COMPOSITE_DSTOUT, 0, 0); | |
196 | - | |
197 | - // NOTE: Using setImageOpacity will destroy alpha channels! | |
198 | - $reflection->evaluateImage(Imagick::EVALUATE_MULTIPLY, $opacity / 100, Imagick::CHANNEL_ALPHA); | |
199 | - | |
200 | - // Create a new container to hold the image and reflection | |
201 | - $image = new Imagick; | |
202 | - $image->newImage($this->width, $this->height + $height, new ImagickPixel); | |
203 | - | |
204 | - // Force the image to have an alpha channel | |
205 | - $image->setImageAlphaChannel(Imagick::ALPHACHANNEL_SET); | |
206 | - | |
207 | - // Force the background color to be transparent | |
208 | - // $image->setImageBackgroundColor(new ImagickPixel('transparent')); | |
209 | - | |
210 | - // Match the colorspace between the two images before compositing | |
211 | - $image->setColorspace($this->im->getColorspace()); | |
212 | - | |
213 | - // Place the image and reflection into the container | |
214 | - if ($image->compositeImage($this->im, Imagick::COMPOSITE_SRC, 0, 0) | |
215 | - AND $image->compositeImage($reflection, Imagick::COMPOSITE_OVER, 0, $this->height)) | |
216 | - { | |
217 | - // Replace the current image with the reflected image | |
218 | - $this->im = $image; | |
219 | - | |
220 | - // Reset the width and height | |
221 | - $this->width = $this->im->getImageWidth(); | |
222 | - $this->height = $this->im->getImageHeight(); | |
223 | - | |
224 | - return TRUE; | |
225 | - } | |
226 | - | |
227 | - return FALSE; | |
228 | - } | |
229 | - | |
230 | - protected function _do_watermark(Kohana_Image $image, $offset_x, $offset_y, $opacity) | |
231 | - { | |
232 | - // Convert the Image intance into an Imagick instance | |
233 | - $watermark = new Imagick; | |
234 | - $watermark->readImageBlob($image->render(), $image->file); | |
235 | - | |
236 | - if ($watermark->getImageAlphaChannel() !== Imagick::ALPHACHANNEL_ACTIVATE) | |
237 | - { | |
238 | - // Force the image to have an alpha channel | |
239 | - $watermark->setImageAlphaChannel(Imagick::ALPHACHANNEL_OPAQUE); | |
240 | - } | |
241 | - | |
242 | - if ($opacity < 100) | |
243 | - { | |
244 | - // NOTE: Using setImageOpacity will destroy current alpha channels! | |
245 | - $watermark->evaluateImage(Imagick::EVALUATE_MULTIPLY, $opacity / 100, Imagick::CHANNEL_ALPHA); | |
246 | - } | |
247 | - | |
248 | - // Match the colorspace between the two images before compositing | |
249 | - // $watermark->setColorspace($this->im->getColorspace()); | |
250 | - | |
251 | - // Apply the watermark to the image | |
252 | - return $this->im->compositeImage($watermark, Imagick::COMPOSITE_DISSOLVE, $offset_x, $offset_y); | |
253 | - } | |
254 | - | |
255 | - protected function _do_background($r, $g, $b, $opacity) | |
256 | - { | |
257 | - // Create a RGB color for the background | |
258 | - $color = sprintf('rgb(%d, %d, %d)', $r, $g, $b); | |
259 | - | |
260 | - // Create a new image for the background | |
261 | - $background = new Imagick; | |
262 | - $background->newImage($this->width, $this->height, new ImagickPixel($color)); | |
263 | - | |
264 | - if ( ! $background->getImageAlphaChannel()) | |
265 | - { | |
266 | - // Force the image to have an alpha channel | |
267 | - $background->setImageAlphaChannel(Imagick::ALPHACHANNEL_SET); | |
268 | - } | |
269 | - | |
270 | - // Clear the background image | |
271 | - $background->setImageBackgroundColor(new ImagickPixel('transparent')); | |
272 | - | |
273 | - // NOTE: Using setImageOpacity will destroy current alpha channels! | |
274 | - $background->evaluateImage(Imagick::EVALUATE_MULTIPLY, $opacity / 100, Imagick::CHANNEL_ALPHA); | |
275 | - | |
276 | - // Match the colorspace between the two images before compositing | |
277 | - $background->setColorspace($this->im->getColorspace()); | |
278 | - | |
279 | - if ($background->compositeImage($this->im, Imagick::COMPOSITE_DISSOLVE, 0, 0)) | |
280 | - { | |
281 | - // Replace the current image with the new image | |
282 | - $this->im = $background; | |
283 | - | |
284 | - return TRUE; | |
285 | - } | |
286 | - | |
287 | - return FALSE; | |
288 | - } | |
289 | - | |
290 | - protected function _do_save($file, $quality) | |
291 | - { | |
292 | - // Get the image format and type | |
293 | - list($format, $type) = $this->_get_imagetype(pathinfo($file, PATHINFO_EXTENSION)); | |
294 | - | |
295 | - // Set the output image type | |
296 | - $this->im->setFormat($format); | |
297 | - | |
298 | - // Set the output quality | |
299 | - $this->im->setImageCompressionQuality($quality); | |
300 | - | |
301 | - if ($this->im->writeImage($file)) | |
302 | - { | |
303 | - // Reset the image type and mime type | |
304 | - $this->type = $type; | |
305 | - $this->mime = image_type_to_mime_type($type); | |
306 | - | |
307 | - return TRUE; | |
308 | - } | |
309 | - | |
310 | - return FALSE; | |
311 | - } | |
312 | - | |
313 | - protected function _do_render($type, $quality) | |
314 | - { | |
315 | - // Get the image format and type | |
316 | - list($format, $type) = $this->_get_imagetype($type); | |
317 | - | |
318 | - // Set the output image type | |
319 | - $this->im->setFormat($format); | |
320 | - | |
321 | - // Set the output quality | |
322 | - $this->im->setImageCompressionQuality($quality); | |
323 | - | |
324 | - // Reset the image type and mime type | |
325 | - $this->type = $type; | |
326 | - $this->mime = image_type_to_mime_type($type); | |
327 | - | |
328 | - return (string) $this->im; | |
329 | - } | |
330 | - | |
331 | - /** | |
332 | - * Get the image type and format for an extension. | |
333 | - * | |
334 | - * @param string $extension image extension: png, jpg, etc | |
335 | - * @return string IMAGETYPE_* constant | |
336 | - * @throws ErrorException | |
337 | - */ | |
338 | - protected function _get_imagetype($extension) | |
339 | - { | |
340 | - // Normalize the extension to a format | |
341 | - $format = strtolower($extension); | |
342 | - | |
343 | - switch ($format) | |
344 | - { | |
345 | - case 'jpg': | |
346 | - case 'jpeg': | |
347 | - $type = IMAGETYPE_JPEG; | |
348 | - break; | |
349 | - case 'gif': | |
350 | - $type = IMAGETYPE_GIF; | |
351 | - break; | |
352 | - case 'png': | |
353 | - $type = IMAGETYPE_PNG; | |
354 | - break; | |
355 | - default: | |
356 | - throw new ErrorException(sprintf('Installed ImageMagick does not support %s images',$extension)); | |
357 | - break; | |
358 | - } | |
359 | - | |
360 | - return array($format, $type); | |
361 | - } | |
362 | -} // End Kohana_Image_Imagick |
common/components/artboximage/drivers/Kohana/Kohana_Image.php deleted
1 | -<?php | |
2 | -namespace common\components\artboximage\drivers\Kohana; | |
3 | - | |
4 | -use common\components\artboximage\drivers\Image; | |
5 | -use yii\base\ErrorException; | |
6 | -/** | |
7 | - * Image manipulation support. Allows images to be resized, cropped, etc. | |
8 | - * | |
9 | - * @package Kohana/Image | |
10 | - * @category Base | |
11 | - * @author Kohana Team | |
12 | - * @copyright (c) 2008-2009 Kohana Team | |
13 | - * @license http://kohanaphp.com/license.html | |
14 | - */ | |
15 | -abstract class Kohana_Image { | |
16 | - | |
17 | - // Resizing constraints | |
18 | - const NONE = 0x01; | |
19 | - const WIDTH = 0x02; | |
20 | - const HEIGHT = 0x03; | |
21 | - const AUTO = 0x04; | |
22 | - const INVERSE = 0x05; | |
23 | - const PRECISE = 0x06; | |
24 | - const ADAPT = 0x07; | |
25 | - const CROP = 0x08; | |
26 | - | |
27 | - // Flipping directions | |
28 | - const HORIZONTAL = 0x11; | |
29 | - const VERTICAL = 0x12; | |
30 | - | |
31 | - /** | |
32 | - * @var string default driver: GD, ImageMagick, etc | |
33 | - */ | |
34 | - public static $default_driver = 'GD'; | |
35 | - | |
36 | - // Status of the driver check | |
37 | - protected static $_checked = FALSE; | |
38 | - | |
39 | - /** | |
40 | - * Loads an image and prepares it for manipulation. | |
41 | - * | |
42 | - * $image = Image::factory('upload/test.jpg'); | |
43 | - * | |
44 | - * @param string $file image file path | |
45 | - * @param string $driver driver type: GD, ImageMagick, etc | |
46 | - * @return Image | |
47 | - * @uses Image::$default_driver | |
48 | - */ | |
49 | - public static function factory($file, $driver = NULL) | |
50 | - { | |
51 | - if ($driver === NULL) | |
52 | - { | |
53 | - | |
54 | - // Use the default driver | |
55 | - $driver = Image::$default_driver; | |
56 | - } | |
57 | - | |
58 | - // Set the class name | |
59 | - $class = 'Image_'.$driver; | |
60 | - | |
61 | - return new Image\Image_GD(); // $class($file); | |
62 | - } | |
63 | - | |
64 | - /** | |
65 | - * @var string image file path | |
66 | - */ | |
67 | - public $file; | |
68 | - | |
69 | - /** | |
70 | - * @var integer image width | |
71 | - */ | |
72 | - public $width; | |
73 | - | |
74 | - /** | |
75 | - * @var integer image height | |
76 | - */ | |
77 | - public $height; | |
78 | - | |
79 | - /** | |
80 | - * @var integer one of the IMAGETYPE_* constants | |
81 | - */ | |
82 | - public $type; | |
83 | - | |
84 | - /** | |
85 | - * @var string mime type of the image | |
86 | - */ | |
87 | - public $mime; | |
88 | - | |
89 | - /** | |
90 | - * Loads information about the image. Will throw an exception if the image | |
91 | - * does not exist or is not an image. | |
92 | - * | |
93 | - * @param string $file image file path | |
94 | - * @return void | |
95 | - * @throws ErrorException | |
96 | - */ | |
97 | - public function __construct($file) | |
98 | - { | |
99 | - try | |
100 | - { | |
101 | - // Get the real path to the file | |
102 | - $file = realpath($file); | |
103 | - | |
104 | - // Get the image information | |
105 | - $info = getimagesize($file); | |
106 | - } | |
107 | - catch (Exception $e) | |
108 | - { | |
109 | - // Ignore all errors while reading the image | |
110 | - } | |
111 | - | |
112 | - if (empty($file) OR empty($info)) | |
113 | - { | |
114 | - throw new ErrorException(sprintf('Not an image or invalid image: %s',$file)); | |
115 | - } | |
116 | - | |
117 | - // Store the image information | |
118 | - $this->file = $file; | |
119 | - $this->width = $info[0]; | |
120 | - $this->height = $info[1]; | |
121 | - $this->type = $info[2]; | |
122 | - $this->mime = image_type_to_mime_type($this->type); | |
123 | - } | |
124 | - | |
125 | - /** | |
126 | - * Render the current image. | |
127 | - * | |
128 | - * echo $image; | |
129 | - * | |
130 | - * [!!] The output of this function is binary and must be rendered with the | |
131 | - * appropriate Content-Type header or it will not be displayed correctly! | |
132 | - * | |
133 | - * @return string | |
134 | - */ | |
135 | - public function __toString() | |
136 | - { | |
137 | - try | |
138 | - { | |
139 | - // Render the current image | |
140 | - return $this->render(); | |
141 | - } | |
142 | - catch (ErrorException $e) | |
143 | - { | |
144 | - /* | |
145 | - if (is_object(Kohana::$log)) | |
146 | - { | |
147 | - // Get the text of the exception | |
148 | - $error = ErrorException::text($e); | |
149 | - | |
150 | - // Add this exception to the log | |
151 | - Yii::error($error); | |
152 | - } | |
153 | - */ | |
154 | - | |
155 | - // Showing any kind of error will be "inside" image data | |
156 | - return ''; | |
157 | - } | |
158 | - } | |
159 | - | |
160 | - /** | |
161 | - * Resize the image to the given size. Either the width or the height can | |
162 | - * be omitted and the image will be resized proportionally. | |
163 | - * | |
164 | - * // Resize to 200 pixels on the shortest side | |
165 | - * $image->resize(200, 200); | |
166 | - * | |
167 | - * // Resize to 200x200 pixels, keeping aspect ratio | |
168 | - * $image->resize(200, 200, Image::INVERSE); | |
169 | - * | |
170 | - * // Resize to 500 pixel width, keeping aspect ratio | |
171 | - * $image->resize(500, NULL); | |
172 | - * | |
173 | - * // Resize to 500 pixel height, keeping aspect ratio | |
174 | - * $image->resize(NULL, 500); | |
175 | - * | |
176 | - * // Resize to 200x500 pixels, ignoring aspect ratio | |
177 | - * $image->resize(200, 500, Image::NONE); | |
178 | - * | |
179 | - * // Resize to 400 pixels on the shortest side, puts it in the center | |
180 | - * // of the image with the transparent edges, keeping aspect ratio, | |
181 | - * // output size will be 400x400 pixels | |
182 | - * $image->resize(400, 400, Image::ADAPT); | |
183 | - * | |
184 | - * @param integer $width new width | |
185 | - * @param integer $height new height | |
186 | - * @param integer $master master dimension | |
187 | - * @return $this | |
188 | - * @uses Image::_do_resize | |
189 | - */ | |
190 | - public function resize($width = NULL, $height = NULL, $master = NULL) | |
191 | - { | |
192 | - if ($master === NULL) | |
193 | - { | |
194 | - // Choose the master dimension automatically | |
195 | - $master = Image::AUTO; | |
196 | - } | |
197 | - elseif ($master === Image::CROP) | |
198 | - { | |
199 | - if (empty($width) || empty($height)) | |
200 | - { | |
201 | - return $this->resize($width, $height, Image::AUTO); | |
202 | - } | |
203 | - | |
204 | - $master = $this->width / $this->height > $width / $height ? Image::HEIGHT : Image::WIDTH; | |
205 | - $this->resize($width, $height, $master); | |
206 | - | |
207 | - if ($this->width !== $width || $this->height !== $height) | |
208 | - { | |
209 | - $offset_x = round(($this->width - $width) / 2); | |
210 | - $offset_y = round(($this->height - $height) / 2); | |
211 | - $this->crop($width, $height, $offset_x, $offset_y); | |
212 | - } | |
213 | - | |
214 | - return $this; | |
215 | - } | |
216 | - // Image::WIDTH and Image::HEIGHT deprecated. You can use it in old projects, | |
217 | - // but in new you must pass empty value for non-master dimension | |
218 | - elseif ($master == Image::WIDTH AND ! empty($width)) | |
219 | - { | |
220 | - $master = Image::AUTO; | |
221 | - | |
222 | - // Set empty height for backward compatibility | |
223 | - $height = NULL; | |
224 | - } | |
225 | - elseif ($master == Image::HEIGHT AND ! empty($height)) | |
226 | - { | |
227 | - $master = Image::AUTO; | |
228 | - | |
229 | - // Set empty width for backward compatibility | |
230 | - $width = NULL; | |
231 | - } | |
232 | - elseif ($master === Image::ADAPT) | |
233 | - { | |
234 | - if (empty($width)) | |
235 | - { | |
236 | - $width = $this->width * $height / $this->height; | |
237 | - } | |
238 | - elseif (empty($height)) | |
239 | - { | |
240 | - $height = $this->height * $width / $this->width; | |
241 | - } | |
242 | - } | |
243 | - | |
244 | - if (empty($width)) | |
245 | - { | |
246 | - if ($master === Image::NONE) | |
247 | - { | |
248 | - // Use the current width | |
249 | - $width = $this->width; | |
250 | - } | |
251 | - else | |
252 | - { | |
253 | - // If width not set, master will be height | |
254 | - $master = Image::HEIGHT; | |
255 | - } | |
256 | - } | |
257 | - | |
258 | - if (empty($height)) | |
259 | - { | |
260 | - if ($master === Image::NONE) | |
261 | - { | |
262 | - // Use the current height | |
263 | - $height = $this->height; | |
264 | - } | |
265 | - else | |
266 | - { | |
267 | - // If height not set, master will be width | |
268 | - $master = Image::WIDTH; | |
269 | - } | |
270 | - } | |
271 | - | |
272 | - switch ($master) | |
273 | - { | |
274 | - case Image::AUTO: | |
275 | - // Choose direction with the greatest reduction ratio | |
276 | - $master = ($this->width / $width) > ($this->height / $height) ? Image::WIDTH : Image::HEIGHT; | |
277 | - break; | |
278 | - case Image::INVERSE: | |
279 | - // Choose direction with the minimum reduction ratio | |
280 | - $master = ($this->width / $width) > ($this->height / $height) ? Image::HEIGHT : Image::WIDTH; | |
281 | - break; | |
282 | - } | |
283 | - | |
284 | - switch ($master) | |
285 | - { | |
286 | - case Image::WIDTH: | |
287 | - // Recalculate the height based on the width proportions | |
288 | - $height = $this->height * $width / $this->width; | |
289 | - break; | |
290 | - case Image::HEIGHT: | |
291 | - // Recalculate the width based on the height proportions | |
292 | - $width = $this->width * $height / $this->height; | |
293 | - break; | |
294 | - case Image::PRECISE: | |
295 | - // Resize to precise size | |
296 | - $ratio = $this->width / $this->height; | |
297 | - | |
298 | - if ($width / $height > $ratio) | |
299 | - { | |
300 | - $height = $this->height * $width / $this->width; | |
301 | - } | |
302 | - else | |
303 | - { | |
304 | - $width = $this->width * $height / $this->height; | |
305 | - } | |
306 | - break; | |
307 | - } | |
308 | - | |
309 | - // Convert the width and height to integers, minimum value is 1px | |
310 | - $width = max(round($width), 1); | |
311 | - $height = max(round($height), 1); | |
312 | - | |
313 | - // Adapt the image if the ratios are not equivalent | |
314 | - if ($master === Image::ADAPT && $width / $height !== $this->width / $this->height) | |
315 | - { | |
316 | - $image_width = $bg_width = $this->width; | |
317 | - $image_height = $bg_height = $this->height; | |
318 | - | |
319 | - $offset_x = $offset_y = 0; | |
320 | - | |
321 | - if ($width / $height > $image_width / $image_height) | |
322 | - { | |
323 | - $bg_width = floor($image_height * $width / $height); | |
324 | - $offset_x = abs(floor(($bg_width - $image_width) / 2)); | |
325 | - } | |
326 | - else | |
327 | - { | |
328 | - $bg_height = floor($image_width * $height / $width); | |
329 | - $offset_y = abs(floor(($bg_height - $image_height) / 2)); | |
330 | - } | |
331 | - | |
332 | - $this->_do_adapt($image_width, $image_height, $bg_width, $bg_height, $offset_x, $offset_y); | |
333 | - } | |
334 | - | |
335 | - $this->_do_resize($width, $height); | |
336 | - | |
337 | - return $this; | |
338 | - } | |
339 | - | |
340 | - /** | |
341 | - * Crop an image to the given size. Either the width or the height can be | |
342 | - * omitted and the current width or height will be used. | |
343 | - * | |
344 | - * If no offset is specified, the center of the axis will be used. | |
345 | - * If an offset of TRUE is specified, the bottom of the axis will be used. | |
346 | - * | |
347 | - * // Crop the image to 200x200 pixels, from the center | |
348 | - * $image->crop(200, 200); | |
349 | - * | |
350 | - * @param integer $width new width | |
351 | - * @param integer $height new height | |
352 | - * @param mixed $offset_x offset from the left | |
353 | - * @param mixed $offset_y offset from the top | |
354 | - * @return $this | |
355 | - * @uses Image::_do_crop | |
356 | - */ | |
357 | - public function crop($width, $height, $offset_x = NULL, $offset_y = NULL) | |
358 | - { | |
359 | - if ($width > $this->width) | |
360 | - { | |
361 | - // Use the current width | |
362 | - $width = $this->width; | |
363 | - } | |
364 | - | |
365 | - if ($height > $this->height) | |
366 | - { | |
367 | - // Use the current height | |
368 | - $height = $this->height; | |
369 | - } | |
370 | - | |
371 | - if ($offset_x === NULL) | |
372 | - { | |
373 | - // Center the X offset | |
374 | - $offset_x = round(($this->width - $width) / 2); | |
375 | - } | |
376 | - elseif ($offset_x === TRUE) | |
377 | - { | |
378 | - // Bottom the X offset | |
379 | - $offset_x = $this->width - $width; | |
380 | - } | |
381 | - elseif ($offset_x < 0) | |
382 | - { | |
383 | - // Set the X offset from the right | |
384 | - $offset_x = $this->width - $width + $offset_x; | |
385 | - } | |
386 | - | |
387 | - if ($offset_y === NULL) | |
388 | - { | |
389 | - // Center the Y offset | |
390 | - $offset_y = round(($this->height - $height) / 2); | |
391 | - } | |
392 | - elseif ($offset_y === TRUE) | |
393 | - { | |
394 | - // Bottom the Y offset | |
395 | - $offset_y = $this->height - $height; | |
396 | - } | |
397 | - elseif ($offset_y < 0) | |
398 | - { | |
399 | - // Set the Y offset from the bottom | |
400 | - $offset_y = $this->height - $height + $offset_y; | |
401 | - } | |
402 | - | |
403 | - // Determine the maximum possible width and height | |
404 | - $max_width = $this->width - $offset_x; | |
405 | - $max_height = $this->height - $offset_y; | |
406 | - | |
407 | - if ($width > $max_width) | |
408 | - { | |
409 | - // Use the maximum available width | |
410 | - $width = $max_width; | |
411 | - } | |
412 | - | |
413 | - if ($height > $max_height) | |
414 | - { | |
415 | - // Use the maximum available height | |
416 | - $height = $max_height; | |
417 | - } | |
418 | - | |
419 | - $this->_do_crop($width, $height, $offset_x, $offset_y); | |
420 | - | |
421 | - return $this; | |
422 | - } | |
423 | - | |
424 | - /** | |
425 | - * Rotate the image by a given amount. | |
426 | - * | |
427 | - * // Rotate 45 degrees clockwise | |
428 | - * $image->rotate(45); | |
429 | - * | |
430 | - * // Rotate 90% counter-clockwise | |
431 | - * $image->rotate(-90); | |
432 | - * | |
433 | - * @param integer $degrees degrees to rotate: -360-360 | |
434 | - * @return $this | |
435 | - * @uses Image::_do_rotate | |
436 | - */ | |
437 | - public function rotate($degrees) | |
438 | - { | |
439 | - // Make the degrees an integer | |
440 | - $degrees = (int) $degrees; | |
441 | - | |
442 | - if ($degrees > 180) | |
443 | - { | |
444 | - do | |
445 | - { | |
446 | - // Keep subtracting full circles until the degrees have normalized | |
447 | - $degrees -= 360; | |
448 | - } | |
449 | - while ($degrees > 180); | |
450 | - } | |
451 | - | |
452 | - if ($degrees < -180) | |
453 | - { | |
454 | - do | |
455 | - { | |
456 | - // Keep adding full circles until the degrees have normalized | |
457 | - $degrees += 360; | |
458 | - } | |
459 | - while ($degrees < -180); | |
460 | - } | |
461 | - | |
462 | - $this->_do_rotate($degrees); | |
463 | - | |
464 | - return $this; | |
465 | - } | |
466 | - | |
467 | - /** | |
468 | - * Flip the image along the horizontal or vertical axis. | |
469 | - * | |
470 | - * // Flip the image from top to bottom | |
471 | - * $image->flip(Image::HORIZONTAL); | |
472 | - * | |
473 | - * // Flip the image from left to right | |
474 | - * $image->flip(Image::VERTICAL); | |
475 | - * | |
476 | - * @param integer $direction direction: Image::HORIZONTAL, Image::VERTICAL | |
477 | - * @return $this | |
478 | - * @uses Image::_do_flip | |
479 | - */ | |
480 | - public function flip($direction) | |
481 | - { | |
482 | - if ($direction !== Image::HORIZONTAL) | |
483 | - { | |
484 | - // Flip vertically | |
485 | - $direction = Image::VERTICAL; | |
486 | - } | |
487 | - | |
488 | - $this->_do_flip($direction); | |
489 | - | |
490 | - return $this; | |
491 | - } | |
492 | - | |
493 | - /** | |
494 | - * Sharpen the image by a given amount. | |
495 | - * | |
496 | - * // Sharpen the image by 20% | |
497 | - * $image->sharpen(20); | |
498 | - * | |
499 | - * @param integer $amount amount to sharpen: 1-100 | |
500 | - * @return $this | |
501 | - * @uses Image::_do_sharpen | |
502 | - */ | |
503 | - public function sharpen($amount) | |
504 | - { | |
505 | - // The amount must be in the range of 1 to 100 | |
506 | - $amount = min(max($amount, 1), 100); | |
507 | - | |
508 | - $this->_do_sharpen($amount); | |
509 | - | |
510 | - return $this; | |
511 | - } | |
512 | - | |
513 | - /** | |
514 | - * Add a reflection to an image. The most opaque part of the reflection | |
515 | - * will be equal to the opacity setting and fade out to full transparent. | |
516 | - * Alpha transparency is preserved. | |
517 | - * | |
518 | - * // Create a 50 pixel reflection that fades from 0-100% opacity | |
519 | - * $image->reflection(50); | |
520 | - * | |
521 | - * // Create a 50 pixel reflection that fades from 100-0% opacity | |
522 | - * $image->reflection(50, 100, TRUE); | |
523 | - * | |
524 | - * // Create a 50 pixel reflection that fades from 0-60% opacity | |
525 | - * $image->reflection(50, 60, TRUE); | |
526 | - * | |
527 | - * [!!] By default, the reflection will be go from transparent at the top | |
528 | - * to opaque at the bottom. | |
529 | - * | |
530 | - * @param integer $height reflection height | |
531 | - * @param integer $opacity reflection opacity: 0-100 | |
532 | - * @param boolean $fade_in TRUE to fade in, FALSE to fade out | |
533 | - * @return $this | |
534 | - * @uses Image::_do_reflection | |
535 | - */ | |
536 | - public function reflection($height = NULL, $opacity = 100, $fade_in = FALSE) | |
537 | - { | |
538 | - if ($height === NULL OR $height > $this->height) | |
539 | - { | |
540 | - // Use the current height | |
541 | - $height = $this->height; | |
542 | - } | |
543 | - | |
544 | - // The opacity must be in the range of 0 to 100 | |
545 | - $opacity = min(max($opacity, 0), 100); | |
546 | - | |
547 | - $this->_do_reflection($height, $opacity, $fade_in); | |
548 | - | |
549 | - return $this; | |
550 | - } | |
551 | - | |
552 | - /** | |
553 | - * Add a watermark to an image with a specified opacity. Alpha transparency | |
554 | - * will be preserved. | |
555 | - * | |
556 | - * If no offset is specified, the center of the axis will be used. | |
557 | - * If an offset of TRUE is specified, the bottom of the axis will be used. | |
558 | - * | |
559 | - * // Add a watermark to the bottom right of the image | |
560 | - * $mark = Image::factory('upload/watermark.png'); | |
561 | - * $image->watermark($mark, TRUE, TRUE); | |
562 | - * | |
563 | - * @param Kohana_Image $watermark watermark Image instance | |
564 | - * @param integer $offset_x offset from the left | |
565 | - * @param integer $offset_y offset from the top | |
566 | - * @param integer $opacity opacity of watermark: 1-100 | |
567 | - * @return $this | |
568 | - * @uses Image::_do_watermark | |
569 | - */ | |
570 | - public function watermark(Kohana_Image $watermark, $offset_x = NULL, $offset_y = NULL, $opacity = 100) | |
571 | - { | |
572 | - if ($offset_x === NULL) | |
573 | - { | |
574 | - // Center the X offset | |
575 | - $offset_x = round(($this->width - $watermark->width) / 2); | |
576 | - } | |
577 | - elseif ($offset_x === TRUE) | |
578 | - { | |
579 | - // Bottom the X offset | |
580 | - $offset_x = $this->width - $watermark->width; | |
581 | - } | |
582 | - elseif ($offset_x < 0) | |
583 | - { | |
584 | - // Set the X offset from the right | |
585 | - $offset_x = $this->width - $watermark->width + $offset_x; | |
586 | - } | |
587 | - | |
588 | - if ($offset_y === NULL) | |
589 | - { | |
590 | - // Center the Y offset | |
591 | - $offset_y = round(($this->height - $watermark->height) / 2); | |
592 | - } | |
593 | - elseif ($offset_y === TRUE) | |
594 | - { | |
595 | - // Bottom the Y offset | |
596 | - $offset_y = $this->height - $watermark->height; | |
597 | - } | |
598 | - elseif ($offset_y < 0) | |
599 | - { | |
600 | - // Set the Y offset from the bottom | |
601 | - $offset_y = $this->height - $watermark->height + $offset_y; | |
602 | - } | |
603 | - | |
604 | - // The opacity must be in the range of 1 to 100 | |
605 | - $opacity = min(max($opacity, 1), 100); | |
606 | - | |
607 | - $this->_do_watermark($watermark, $offset_x, $offset_y, $opacity); | |
608 | - | |
609 | - return $this; | |
610 | - } | |
611 | - | |
612 | - /** | |
613 | - * Set the background color of an image. This is only useful for images | |
614 | - * with alpha transparency. | |
615 | - * | |
616 | - * // Make the image background black | |
617 | - * $image->background('#000'); | |
618 | - * | |
619 | - * // Make the image background black with 50% opacity | |
620 | - * $image->background('#000', 50); | |
621 | - * | |
622 | - * @param string $color hexadecimal color value | |
623 | - * @param integer $opacity background opacity: 0-100 | |
624 | - * @return $this | |
625 | - * @uses Image::_do_background | |
626 | - */ | |
627 | - public function background($color, $opacity = 100) | |
628 | - { | |
629 | - if ($color[0] === '#') | |
630 | - { | |
631 | - // Remove the pound | |
632 | - $color = substr($color, 1); | |
633 | - } | |
634 | - | |
635 | - if (strlen($color) === 3) | |
636 | - { | |
637 | - // Convert shorthand into longhand hex notation | |
638 | - $color = preg_replace('/./', '$0$0', $color); | |
639 | - } | |
640 | - | |
641 | - // Convert the hex into RGB values | |
642 | - list ($r, $g, $b) = array_map('hexdec', str_split($color, 2)); | |
643 | - | |
644 | - // The opacity must be in the range of 0 to 100 | |
645 | - $opacity = min(max($opacity, 0), 100); | |
646 | - | |
647 | - $this->_do_background($r, $g, $b, $opacity); | |
648 | - | |
649 | - return $this; | |
650 | - } | |
651 | - | |
652 | - /** | |
653 | - * Save the image. If the filename is omitted, the original image will | |
654 | - * be overwritten. | |
655 | - * | |
656 | - * // Save the image as a PNG | |
657 | - * $image->save('saved/cool.png'); | |
658 | - * | |
659 | - * // Overwrite the original image | |
660 | - * $image->save(); | |
661 | - * | |
662 | - * [!!] If the file exists, but is not writable, an exception will be thrown. | |
663 | - * | |
664 | - * [!!] If the file does not exist, and the directory is not writable, an | |
665 | - * exception will be thrown. | |
666 | - * | |
667 | - * @param string $file new image path | |
668 | - * @param integer $quality quality of image: 1-100 | |
669 | - * @return boolean | |
670 | - * @uses Image::_save | |
671 | - * @throws ErrorException | |
672 | - */ | |
673 | - public function save($file = NULL, $quality = 100) | |
674 | - { | |
675 | - if ($file === NULL) | |
676 | - { | |
677 | - // Overwrite the file | |
678 | - $file = $this->file; | |
679 | - } | |
680 | - | |
681 | - if (is_file($file)) | |
682 | - { | |
683 | - if ( ! is_writable($file)) | |
684 | - { | |
685 | - throw new ErrorException(sprintf('File must be writable: %s',$file)); | |
686 | - } | |
687 | - } | |
688 | - else | |
689 | - { | |
690 | - // Get the directory of the file | |
691 | - $directory = realpath(pathinfo($file, PATHINFO_DIRNAME)); | |
692 | - | |
693 | - if ( ! is_dir($directory) OR ! is_writable($directory)) | |
694 | - { | |
695 | - throw new ErrorException(sprintf('Directory must be writable: %s',$directory)); | |
696 | - } | |
697 | - } | |
698 | - | |
699 | - // The quality must be in the range of 1 to 100 | |
700 | - $quality = min(max($quality, 1), 100); | |
701 | - | |
702 | - return $this->_do_save($file, $quality); | |
703 | - } | |
704 | - | |
705 | - /** | |
706 | - * Render the image and return the binary string. | |
707 | - * | |
708 | - * // Render the image at 50% quality | |
709 | - * $data = $image->render(NULL, 50); | |
710 | - * | |
711 | - * // Render the image as a PNG | |
712 | - * $data = $image->render('png'); | |
713 | - * | |
714 | - * @param string $type image type to return: png, jpg, gif, etc | |
715 | - * @param integer $quality quality of image: 1-100 | |
716 | - * @return string | |
717 | - * @uses Image::_do_render | |
718 | - */ | |
719 | - public function render($type = NULL, $quality = 100) | |
720 | - { | |
721 | - if ($type === NULL) | |
722 | - { | |
723 | - // Use the current image type | |
724 | - $type = image_type_to_extension($this->type, FALSE); | |
725 | - } | |
726 | - | |
727 | - return $this->_do_render($type, $quality); | |
728 | - } | |
729 | - | |
730 | - /** | |
731 | - * Execute a resize. | |
732 | - * | |
733 | - * @param integer $width new width | |
734 | - * @param integer $height new height | |
735 | - * @return void | |
736 | - */ | |
737 | - abstract protected function _do_resize($width, $height); | |
738 | - | |
739 | - /** | |
740 | - * Adaptation the image. | |
741 | - * | |
742 | - * @param integer $width image width | |
743 | - * @param integer $height image height | |
744 | - * @param integer $bg_width background width | |
745 | - * @param integer $bg_height background height | |
746 | - * @param integer $offset_x offset from the left | |
747 | - * @param integer $offset_y offset from the top | |
748 | - */ | |
749 | - abstract protected function _do_adapt($width, $height, $bg_width, $bg_height, $offset_x, $offset_y); | |
750 | - | |
751 | - /** | |
752 | - * Execute a crop. | |
753 | - * | |
754 | - * @param integer $width new width | |
755 | - * @param integer $height new height | |
756 | - * @param integer $offset_x offset from the left | |
757 | - * @param integer $offset_y offset from the top | |
758 | - * @return void | |
759 | - */ | |
760 | - abstract protected function _do_crop($width, $height, $offset_x, $offset_y); | |
761 | - | |
762 | - /** | |
763 | - * Execute a rotation. | |
764 | - * | |
765 | - * @param integer $degrees degrees to rotate | |
766 | - * @return void | |
767 | - */ | |
768 | - abstract protected function _do_rotate($degrees); | |
769 | - | |
770 | - /** | |
771 | - * Execute a flip. | |
772 | - * | |
773 | - * @param integer $direction direction to flip | |
774 | - * @return void | |
775 | - */ | |
776 | - abstract protected function _do_flip($direction); | |
777 | - | |
778 | - /** | |
779 | - * Execute a sharpen. | |
780 | - * | |
781 | - * @param integer $amount amount to sharpen | |
782 | - * @return void | |
783 | - */ | |
784 | - abstract protected function _do_sharpen($amount); | |
785 | - | |
786 | - /** | |
787 | - * Execute a reflection. | |
788 | - * | |
789 | - * @param integer $height reflection height | |
790 | - * @param integer $opacity reflection opacity | |
791 | - * @param boolean $fade_in TRUE to fade out, FALSE to fade in | |
792 | - * @return void | |
793 | - */ | |
794 | - abstract protected function _do_reflection($height, $opacity, $fade_in); | |
795 | - | |
796 | - /** | |
797 | - * Execute a watermarking. | |
798 | - * | |
799 | - * @param Kohana_Image $image watermarking Kohana_Image | |
800 | - * @param integer $offset_x offset from the left | |
801 | - * @param integer $offset_y offset from the top | |
802 | - * @param integer $opacity opacity of watermark | |
803 | - * @return void | |
804 | - */ | |
805 | - abstract protected function _do_watermark(Kohana_Image $image, $offset_x, $offset_y, $opacity); | |
806 | - | |
807 | - /** | |
808 | - * Execute a background. | |
809 | - * | |
810 | - * @param integer $r red | |
811 | - * @param integer $g green | |
812 | - * @param integer $b blue | |
813 | - * @param integer $opacity opacity | |
814 | - * @return void | |
815 | - */ | |
816 | - abstract protected function _do_background($r, $g, $b, $opacity); | |
817 | - | |
818 | - /** | |
819 | - * Execute a save. | |
820 | - * | |
821 | - * @param string $file new image filename | |
822 | - * @param integer $quality quality | |
823 | - * @return boolean | |
824 | - */ | |
825 | - abstract protected function _do_save($file, $quality); | |
826 | - | |
827 | - /** | |
828 | - * Execute a render. | |
829 | - * | |
830 | - * @param string $type image type: png, jpg, gif, etc | |
831 | - * @param integer $quality quality | |
832 | - * @return string | |
833 | - */ | |
834 | - abstract protected function _do_render($type, $quality); | |
835 | - | |
836 | -} // End Image |
common/config/main.php
... | ... | @@ -132,9 +132,9 @@ return [ |
132 | 132 | ], |
133 | 133 | |
134 | 134 | 'modules' => [ |
135 | - 'file' => [ | |
136 | - 'class' => 'common\modules\file\Module', | |
137 | - ], | |
135 | +// 'file' => [ | |
136 | +// 'class' => 'common\modules\file\Module', | |
137 | +// ], | |
138 | 138 | 'relation' => [ |
139 | 139 | 'class' => 'common\modules\relation\Module', |
140 | 140 | 'relations' => [ | ... | ... |
1 | +<?php | |
2 | + | |
3 | +namespace common\modules\artboxfile; | |
4 | + | |
5 | +/** | |
6 | + * product module definition class | |
7 | + */ | |
8 | +class Module extends \yii\base\Module | |
9 | +{ | |
10 | + /** | |
11 | + * @inheritdoc | |
12 | + */ | |
13 | + public $controllerNamespace = 'common\modules\artboxfile\controllers'; | |
14 | + | |
15 | + /** | |
16 | + * @inheritdoc | |
17 | + */ | |
18 | + public function init() | |
19 | + { | |
20 | + parent::init(); | |
21 | + | |
22 | + \Yii::configure($this, require(__DIR__.'/config.php')); | |
23 | + } | |
24 | +} | ... | ... |
common/modules/artboxfile/controllers/ActionController.php
0 โ 100644
1 | +<?php | |
2 | + | |
3 | +namespace common\modules\artboxfile\controllers; | |
4 | + | |
5 | +use common\modules\artboxfile\models\File; | |
6 | +use Yii; | |
7 | +use yii\web\Controller; | |
8 | +use yii\web\UploadedFile; | |
9 | + | |
10 | +class ActionController extends Controller { | |
11 | + | |
12 | + public $tmpDir = '@storage/tmp'; | |
13 | + | |
14 | + public function actionUpload() { | |
15 | + $request = Yii::$app->request->post(); | |
16 | + | |
17 | + $multiple = !empty($request['multiple']); | |
18 | + $fileField = empty($request['fileField']) ? 'file' : $request['fileField']; | |
19 | + $formField = empty($request['formField']) ? 'fileUpload' : $request['formField']; | |
20 | + | |
21 | + | |
22 | + if($_FILES[$formField]) | |
23 | + { | |
24 | + if (($_FILES[$formField] == "none") OR (empty($_FILES[$formField]['name'])) ) | |
25 | + { | |
26 | + $error = "ะั ะฝะต ะฒัะฑัะฐะปะธ ัะฐะนะป"; | |
27 | + } | |
28 | + else if ($_FILES[$formField]["size"] == 0 OR $_FILES[$formField]["size"] > 2050000) | |
29 | + { | |
30 | + $error = "ะ ะฐะทะผะตั ัะฐะนะปะฐ ะฝะต ัะพะพัะฒะตัััะฒัะตั ะฝะพัะผะฐะผ"; | |
31 | + } | |
32 | + /*else if (($_FILES['upload']["type"] != "image/jpeg") AND ($_FILES['upload']["type"] != "image/jpeg") AND ($_FILES['upload']["type"] != "image/png") AND ($_FILES['upload']['type'] != 'image/gif')) | |
33 | + { | |
34 | + $message = "ะะพะฟััะบะฐะตััั ะทะฐะณััะทะบะฐ ัะพะปัะบะพ ะบะฐััะธะฝะพะบ JPG, GIF ะธ PNG."; | |
35 | + }*/ | |
36 | + else if (!is_uploaded_file($_FILES[$formField]["tmp_name"])) | |
37 | + { | |
38 | + $error = "ะงัะพ-ัะพ ะฟะพัะปะพ ะฝะต ัะฐะบ. ะะพะฟััะฐะนัะตัั ะทะฐะณััะทะธัั ัะฐะนะป ะตัั ัะฐะท."; | |
39 | + } | |
40 | + else{ | |
41 | + $ext = $this->getex($_FILES[$formField]['name']); | |
42 | + $name = $_FILES[$formField]['name'] .'.'. $ext; | |
43 | + $tmpName = uniqid('tmpfile_') .'.'. $ext; | |
44 | + | |
45 | + $path = Yii::getAlias($this->tmpDir); | |
46 | + if(!is_dir($path)) { | |
47 | + mkdir($path, 0755, true); | |
48 | + } | |
49 | + | |
50 | + if (!move_uploaded_file($_FILES[$formField]['tmp_name'], $path .'/'. $tmpName)) { | |
51 | + $error = "File not uploaded"; | |
52 | + } else { | |
53 | + $full_path = $path .'/'. $tmpName; | |
54 | + $message = "ะคะฐะนะป " . $_FILES[$formField]['name'] . " in $full_path ะทะฐะณััะถะตะฝ"; | |
55 | + } | |
56 | + } | |
57 | + } | |
58 | + if (!empty($error)) { | |
59 | + print json_encode(['error' => $error]); | |
60 | + } else { | |
61 | + print json_encode(['message' => $message, 'tmpfile' => $full_path, 'filename' => $name]); | |
62 | + } | |
63 | + exit; | |
64 | + } | |
65 | + | |
66 | + protected function getex($filename) { | |
67 | + return end(explode(".", $filename)); | |
68 | + } | |
69 | +} | |
0 | 70 | \ No newline at end of file | ... | ... |
1 | +<?php | |
2 | + | |
3 | +namespace common\modules\artboxfile\models; | |
4 | + | |
5 | +use Yii; | |
6 | +use yii\db\ActiveRecord; | |
7 | + | |
8 | +class File extends ActiveRecord { | |
9 | + | |
10 | + public $fileUploadName; | |
11 | + | |
12 | + /** | |
13 | + * @inheritdoc | |
14 | + */ | |
15 | + public function rules() | |
16 | + { | |
17 | + return [ | |
18 | + [['file'], 'string', 'max' => 255], | |
19 | + [[$this->fileUploadName], 'safe'], | |
20 | + [[$this->fileUploadName], 'file', 'extensions' => 'jpg, gif, png'], | |
21 | + ]; | |
22 | + } | |
23 | + | |
24 | + /** | |
25 | + * @inheritdoc | |
26 | + */ | |
27 | + public function attributeLabels() | |
28 | + { | |
29 | + return [ | |
30 | + 'file' => Yii::t('artbox', 'File name'), | |
31 | + 'fileUpload' => Yii::t('artbox', 'File'), | |
32 | + ]; | |
33 | + } | |
34 | +} | |
0 | 35 | \ No newline at end of file | ... | ... |
frontend/config/main.php
... | ... | @@ -41,21 +41,6 @@ return [ |
41 | 41 | 'errorHandler' => [ |
42 | 42 | 'errorAction' => 'site/error', |
43 | 43 | ], |
44 | - 'imageCache' => [ | |
45 | - 'class' => 'iutbay\yii2imagecache\ImageCache', | |
46 | - 'sourcePath' => '@app/web/images', | |
47 | - 'sourceUrl' => '@web/images', | |
48 | - 'resizeMode' => 'inset', | |
49 | - 'sizes' => [ | |
50 | - 'brandlist' => [138, 78], | |
51 | - 'product' => [300, 300], | |
52 | - 'product_trumb' => [80, 80], | |
53 | - 'product_list' => [130, 70], | |
54 | - 'product_list2' => [130, 70], | |
55 | - 'mainmenu' => [160, 170], | |
56 | - 'large' => [600, 600], | |
57 | - ], | |
58 | - ], | |
59 | 44 | 'urlManager' => [ |
60 | 45 | 'baseUrl' => '/', |
61 | 46 | 'enablePrettyUrl' => true, |
... | ... | @@ -72,7 +57,6 @@ return [ |
72 | 57 | 'page/<translit:[A-Za-z0-9_-]++>'=>'page/show', |
73 | 58 | 'event/view/<alias:[A-Za-z0-9_-]+>'=>'event/view', |
74 | 59 | 'service/view/<alias:[A-Za-z0-9_-]+>'=>'service/view', |
75 | - 'thumbs/<path:.*>' => 'site/thumb', | |
76 | 60 | //// 'catalog' => 'catalog/category', |
77 | 61 | // 'catalog/<alias:[A-Za-z0-9_-]+>' => 'catalog/category', |
78 | 62 | // 'catalog/<alias:[A-Za-z0-9_-]+>/<filter>' => 'catalog/category', | ... | ... |