BaseController.php
1.73 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
<?php
/**
* Created by PhpStorm.
* User: Cibermag
* Date: 31.08.2015
* Time: 9:58
*/
namespace backend\components\base;
use yii\web\Controller;
class BaseController extends Controller {
public function beforeAction($action) {
$this->enableCsrfValidation = false;
return parent::beforeAction($action);
}
private function resizeImg($w, $h, $imageAlias,$imageAliasSave){
$img = Image::getImagine()->open(Yii::getAlias($imageAlias));
$size = $img->getSize();
$ratio = $size->getWidth()/$size->getHeight();
$height = $h;
$width = round($height * $ratio);
$imagine = new Imagine();
$imagine->open($imageAlias)
->resize(new Box($width, $h))
->save($imageAlias, array('flatten' => false));
Image::crop($imageAlias, $w, $h,[($width/2)-($w/2),0])
->save(Yii::getAlias($imageAliasSave), ['quality' =>
100]);
}
public function actionDownloadPhoto()
{
die('here');
$model = new UploadForm();
if ($model->load(Yii::$app->request->post())) {
$model->file = UploadedFile::getInstance($model, 'file');
$md5_file = md5_file($model->file->tempName);
$imgDir = Yii::getAlias('@storage/'.$md5_file.'/');
$imageAlias = Yii::getAlias($imgDir.'480x365'.'.'.$model->file->extension);
$imageLink = '/storage/'.$md5_file.'/480x365'.'.'.$model->file->extension;
if(!is_dir($imgDir)) {
mkdir($imgDir, 0777, true);
}
$model->file->saveAs($imageAlias);
$this->resizeImg(480, 365, $imageAlias,$imageAlias);
return json_encode(['link'=>$imageLink]);
}
}
}