BaseController.php
4.74 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Created by PhpStorm.
* User: Cibermag
* Date: 31.08.2015
* Time: 9:58
*/
namespace backend\components\base;
use Yii;
use yii\web\UploadedFile;
use backend\models\ImageSizerForm;
use yii\web\Controller;
use Imagine\Gd\Imagine;
use Imagine\Image\Box;
use yii\imagine\Image;
class BaseController extends Controller {
private function resizeImg($w, $h, $imageAlias,$imageAliasSave){
$img = Image::getImagine()->open(Yii::getAlias($imageAlias));
$size = $img->getSize();
$width = $size->getWidth();
$height = $size->getHeight();
if($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}
Image::crop($imageAlias, $smallestSide, $smallestSide,[$x,$y])
->save(Yii::getAlias($imageAliasSave), ['quality' =>
100]);
$imagine = new Imagine();
$imagine->open($imageAliasSave)
->resize(new Box($w, $h))
->save($imageAliasSave, array('flatten' => false));
}
private function deleteImages($old_img){
if(!empty($old_img) && file_exists($_SERVER['DOCUMENT_ROOT'].$old_img)){
$rootDir = explode("/", $old_img);
$row = $_SERVER['DOCUMENT_ROOT'].'/'.$rootDir[1].'/'.$rootDir[2].'/';
$allFiles = scandir($row);
$allFiles = array_slice($allFiles, 2);
foreach($allFiles as $oldFile){
unlink($row.$oldFile);
}
}
}
public function actionDeleteImage(){
$old_img = Yii::$app->request->post('old_img');
if ($old_img) {
$this->deleteImages($old_img);
}
}
public function actionDownloadPhoto()
{
$model = new ImageSizerForm();
if ($model->load(Yii::$app->request->post())) {
$this->deleteImages($model->old_img);
$model->file = UploadedFile::getInstance($model, 'file');
$md5_file = md5_file($model->file->tempName);
$imgDir = Yii::getAlias('@storage/'.$md5_file.'/');
$imageOrigAlias = Yii::getAlias($imgDir.'original'.'.'.$model->file->extension);
if(!is_dir($imgDir)) {
mkdir($imgDir, 0755, true);
}
$model->file->saveAs($imageOrigAlias);
if($model->width && $model->height){
$imageAlias = Yii::getAlias($imgDir.$model->width.'x'.$model->height.'.'.$model->file->extension);
$imageLink = '/storage/'.$md5_file.'/'.$model->width.'x'.$model->height.'.'.$model->file->extension;
$this->resizeImg($model->width, $model->height, $imageOrigAlias,$imageAlias);
} else {
$imageLink = '/storage/'.$md5_file.'/'.'original'.'.'.$model->file->extension;
}
if($model->multi){
$view = $this->renderPartial('@app/components/views/_gallery_item', [
'item' => ['image'=>$imageLink],
]);
return json_encode(['link'=>$imageLink, 'view' =>$view]);
} else {
return json_encode(['link'=>$imageLink]);
}
}
}
/**
* @param $mode - int: 0 - fetch from cache, - 1 - put in cache, <2 - delete from cache
* @param $data - array
* @param $configuration - array
* @throws \ErrorException
*/
protected function parserCacheHandler( $mode, &$data = [], &$configuration = [] ){
switch ( $mode ) {
case 0:
if (Yii::$app->getCache()->get('parser_data') && Yii::$app->getCache()->get('parser_configuration')) {
$data = json_decode(Yii::$app->getCache()->get('parser_data'), true);
$configuration = unserialize(Yii::$app->getCache()->get('parser_configuration'));
} else {
throw new \ErrorException('Ошибка кеша');
}
break;
case 1:
Yii::$app->getCache()->set('parser_data', json_encode($data), 1800);
// сохраняем в кеш модель - в ней настройки для дальнейшей обработки данных
Yii::$app->getCache()->set('parser_configuration', serialize($configuration), 1800);
break;
default:
if( Yii::$app->getCache()->exists('parser_data') )
Yii::$app->getCache()->delete('parser_data');
if( Yii::$app->getCache()->exists('parser_configuration') )
Yii::$app->getCache()->delete('parser_configuration');
}
}
}