54ada04a
Mihail
add base classes ...
|
1
2
3
4
5
6
7
8
9
|
<?php
/**
* Created by PhpStorm.
* User: Cibermag
* Date: 31.08.2015
* Time: 9:58
*/
namespace backend\components\base;
|
4828b892
Mihail
after merge with ...
|
10
11
12
|
use Yii;
use yii\web\UploadedFile;
use backend\models\ImageSizerForm;
|
54ada04a
Mihail
add base classes ...
|
13
|
use yii\web\Controller;
|
4828b892
Mihail
after merge with ...
|
14
15
16
|
use Imagine\Gd\Imagine;
use Imagine\Image\Box;
use yii\imagine\Image;
|
54ada04a
Mihail
add base classes ...
|
17
18
19
|
class BaseController extends Controller {
|
bc1a3552
Administrator
Importers CRUD
|
20
|
|
4828b892
Mihail
after merge with ...
|
21
22
23
24
25
|
private function resizeImg($w, $h, $imageAlias,$imageAliasSave){
$img = Image::getImagine()->open(Yii::getAlias($imageAlias));
$size = $img->getSize();
|
4828b892
Mihail
after merge with ...
|
26
|
|
992d7eaa
Administrator
Importers CRUD
|
27
28
29
30
31
32
33
34
35
36
37
38
39
|
$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;
}
|
4c977682
Administrator
Importers CRUD
|
40
41
42
|
Image::crop($imageAlias, $smallestSide, $smallestSide,[$x,$y])
->save(Yii::getAlias($imageAliasSave), ['quality' =>
100]);
|
992d7eaa
Administrator
Importers CRUD
|
43
44
|
|
4c977682
Administrator
Importers CRUD
|
45
46
47
48
|
$imagine = new Imagine();
$imagine->open($imageAliasSave)
->resize(new Box($w, $h))
->save($imageAliasSave, array('flatten' => false));
|
4828b892
Mihail
after merge with ...
|
49
|
|
4828b892
Mihail
after merge with ...
|
50
51
|
|
4828b892
Mihail
after merge with ...
|
52
53
54
|
}
|
992d7eaa
Administrator
Importers CRUD
|
55
|
|
bc1a3552
Administrator
Importers CRUD
|
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
|
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);
}
}
|
4828b892
Mihail
after merge with ...
|
85
86
87
88
89
|
public function actionDownloadPhoto()
{
$model = new ImageSizerForm();
|
ea4ecf3d
Mihail
console csv parsing
|
90
|
|
4828b892
Mihail
after merge with ...
|
91
92
|
if ($model->load(Yii::$app->request->post())) {
|
bc1a3552
Administrator
Importers CRUD
|
93
94
|
$this->deleteImages($model->old_img);
|
4828b892
Mihail
after merge with ...
|
95
96
97
|
$model->file = UploadedFile::getInstance($model, 'file');
$md5_file = md5_file($model->file->tempName);
|
4c977682
Administrator
Importers CRUD
|
98
|
|
4828b892
Mihail
after merge with ...
|
99
|
$imgDir = Yii::getAlias('@storage/'.$md5_file.'/');
|
4c977682
Administrator
Importers CRUD
|
100
101
|
$imageOrigAlias = Yii::getAlias($imgDir.'original'.'.'.$model->file->extension);
|
ea4ecf3d
Mihail
console csv parsing
|
102
|
|
4828b892
Mihail
after merge with ...
|
103
104
105
|
if(!is_dir($imgDir)) {
mkdir($imgDir, 0755, true);
}
|
4c977682
Administrator
Importers CRUD
|
106
107
108
109
110
111
112
113
114
115
116
117
118
|
$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 {
|
bc1a3552
Administrator
Importers CRUD
|
119
|
$imageLink = '/storage/'.$md5_file.'/'.'original'.'.'.$model->file->extension;
|
4c977682
Administrator
Importers CRUD
|
120
121
122
|
}
|
4c977682
Administrator
Importers CRUD
|
123
|
|
bc1a3552
Administrator
Importers CRUD
|
124
|
if($model->multi){
|
4c977682
Administrator
Importers CRUD
|
125
126
127
128
129
|
$view = $this->renderPartial('@app/components/views/_gallery_item', [
'item' => ['image'=>$imageLink],
]);
return json_encode(['link'=>$imageLink, 'view' =>$view]);
|
bc1a3552
Administrator
Importers CRUD
|
130
131
132
133
|
} else {
return json_encode(['link'=>$imageLink]);
|
4c977682
Administrator
Importers CRUD
|
134
135
|
}
|
4828b892
Mihail
after merge with ...
|
136
137
138
|
}
}
|
54ada04a
Mihail
add base classes ...
|
139
|
|
b6247a0e
Mihail
rewrite parser wr...
|
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
|
/**
* @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');
}
}
|
54ada04a
Mihail
add base classes ...
|
172
|
}
|