01ebf78c
Administrator
Initial commit
|
1
2
3
4
5
6
7
|
<?php
namespace backend\controllers;
use backend\components\croppers\CropContext;
use backend\components\croppers\CropFactory;
|
d3e38cf8
Administrator
Adding transliter...
|
8
|
use backend\components\Helper;
|
01ebf78c
Administrator
Initial commit
|
9
|
use Yii;
|
f9404a23
Administrator
Start gallery
|
10
|
use yii\helpers\FileHelper;
|
01ebf78c
Administrator
Initial commit
|
11
12
13
14
15
16
|
use yii\web\Controller;
use backend\models\UploadForm;
use yii\web\UploadedFile;
class ImageController extends Controller
{
|
75ea1564
Administrator
Adding deletion c...
|
17
18
19
20
21
|
public function beforeAction($action) {
$this->enableCsrfValidation = false;
return parent::beforeAction($action);
}
|
01ebf78c
Administrator
Initial commit
|
22
23
24
25
26
27
28
29
|
public function actionIndex()
{
$request = Yii::$app->request;
$model = new UploadForm();
$ukrSeeds = Yii::$app->params['ukrSeeds'];
if ($request->isPost) {
$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
|
01ebf78c
Administrator
Initial commit
|
30
|
$crop_id = $request->post('crop_id');
|
b5f053d9
Administrator
Adding dirs for p...
|
31
32
|
$model->upload($ukrSeeds[$crop_id]);
|
01ebf78c
Administrator
Initial commit
|
33
|
$cropFactory = CropFactory::getInstance();
|
bfa22e8e
Administrator
Adding resizing a...
|
34
|
$minHeight = $cropFactory->findMinHeight();
|
01ebf78c
Administrator
Initial commit
|
35
|
$cropContext = new CropContext($cropFactory->getCrop($crop_id));
|
b5f053d9
Administrator
Adding dirs for p...
|
36
|
$basePath = dirname(dirname(__DIR__)) . '/uploads/'. $ukrSeeds[$crop_id] . '/';
|
01ebf78c
Administrator
Initial commit
|
37
|
foreach($model->imageFiles as $file) {
|
5e673d8b
Administrator
Adding deletion c...
|
38
39
|
$baseName = Helper::transliterate($file->name);
$path = $basePath . $baseName;
|
01ebf78c
Administrator
Initial commit
|
40
|
$image = Yii::$app->imagine->open($path);
|
bfa22e8e
Administrator
Adding resizing a...
|
41
42
43
|
$image = $cropContext->cropImage($image, $path);
$box = $image->getSize()->heighten($minHeight);
$image->resize($box)
|
5e673d8b
Administrator
Adding deletion c...
|
44
|
->save($basePath . 'resized-' . $baseName);
|
01ebf78c
Administrator
Initial commit
|
45
46
47
48
49
|
}
}
return $this->render('index', ['model' => $model, 'ukrSeeds' => $ukrSeeds]);
}
|
f9404a23
Administrator
Start gallery
|
50
51
52
|
public function actionGallery()
{
$files = FileHelper::findFiles(dirname(dirname(__DIR__)) . '/uploads/');
|
bfa22e8e
Administrator
Adding resizing a...
|
53
54
55
|
$images = [];
foreach($files as $file) {
|
b5f053d9
Administrator
Adding dirs for p...
|
56
57
|
if(Helper::isImage($file)) {
$images[] = Helper::findUrl($file, 2);
|
bfa22e8e
Administrator
Adding resizing a...
|
58
|
}
|
3ff61e27
Administrator
Adding basic gallery
|
59
|
}
|
bfa22e8e
Administrator
Adding resizing a...
|
60
|
return $this->render('gallery', ['files' => $images]);
|
f9404a23
Administrator
Start gallery
|
61
62
|
}
|
01ebf78c
Administrator
Initial commit
|
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
public function actionGetParams($crop_id)
{
if (Yii::$app->request->isAjax) {
$cropFactory = CropFactory::getInstance();
$res = [
'body' => $cropFactory->getCrop($crop_id)->getConstants(),
'success' => true,
];
return json_encode($res);
}
}
|
bfa22e8e
Administrator
Adding resizing a...
|
77
78
79
80
81
82
83
|
public function actionDownloadAll()
{
$files = FileHelper::findFiles(dirname(dirname(__DIR__)) . '/uploads/');
$zip = new \ZipArchive();
$filename = dirname(dirname(__DIR__)) . '/uploads/images.zip';
|
b5f053d9
Administrator
Adding dirs for p...
|
84
|
$zip->open($filename, \ZipArchive::OVERWRITE);
|
bfa22e8e
Administrator
Adding resizing a...
|
85
|
foreach($files as $k => $file) {
|
b5f053d9
Administrator
Adding dirs for p...
|
86
87
|
if(Helper::isImage($file)) {
$zip->addFile($file, Helper::findUrl($file, 1));
|
bfa22e8e
Administrator
Adding resizing a...
|
88
89
90
91
92
93
94
|
}
}
$zip->close();
$response = Yii::$app->response;
$response->sendFile($filename);
}
|
5e673d8b
Administrator
Adding deletion c...
|
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
public function actionDeleteImages()
{
if (Yii::$app->request->isAjax) {
$data = Yii::$app->request->post();
$files = $data['images'];
$basePath = dirname(dirname(__DIR__));
foreach($files as $file) {
$path = $basePath . $file;
if(file_exists($path))
unlink($path);
}
return json_encode($data);
}
}
|
01ebf78c
Administrator
Initial commit
|
114
|
}
|