ImageController.php
3.37 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
<?php
namespace backend\controllers;
use backend\components\croppers\CropContext;
use backend\components\croppers\CropFactory;
use backend\components\Helper;
use Yii;
use yii\helpers\FileHelper;
use yii\web\Controller;
use backend\models\UploadForm;
use yii\web\UploadedFile;
class ImageController extends Controller
{
public function beforeAction($action) {
$this->enableCsrfValidation = false;
return parent::beforeAction($action);
}
public function actionIndex()
{
$request = Yii::$app->request;
$model = new UploadForm();
$ukrSeeds = Yii::$app->params['ukrSeeds'];
if ($request->isPost) {
$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
$crop_id = $request->post('crop_id');
$model->upload($ukrSeeds[$crop_id]);
$cropFactory = CropFactory::getInstance();
$minHeight = $cropFactory->findMinHeight();
$cropContext = new CropContext($cropFactory->getCrop($crop_id));
$basePath = dirname(dirname(__DIR__)) . '/uploads/'. $ukrSeeds[$crop_id] . '/';
foreach($model->imageFiles as $file) {
$baseName = Helper::transliterate($file->name);
$path = $basePath . $baseName;
$image = Yii::$app->imagine->open($path);
$image = $cropContext->cropImage($image, $path);
$box = $image->getSize()->heighten($minHeight);
$image->resize($box)
->save($basePath . 'resized-' . $baseName);
}
}
return $this->render('index', ['model' => $model, 'ukrSeeds' => $ukrSeeds]);
}
public function actionGallery()
{
$files = FileHelper::findFiles(dirname(dirname(__DIR__)) . '/uploads/');
$images = [];
foreach($files as $file) {
if(Helper::isImage($file)) {
$images[] = Helper::findUrl($file, 2);
}
}
return $this->render('gallery', ['files' => $images]);
}
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);
}
}
public function actionDownloadAll()
{
$files = FileHelper::findFiles(dirname(dirname(__DIR__)) . '/uploads/');
$zip = new \ZipArchive();
$filename = dirname(dirname(__DIR__)) . '/uploads/images.zip';
$zip->open($filename, \ZipArchive::OVERWRITE);
foreach($files as $k => $file) {
if(Helper::isImage($file)) {
$zip->addFile($file, Helper::findUrl($file, 1));
}
}
$zip->close();
$response = Yii::$app->response;
$response->sendFile($filename);
}
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);
}
}
}