1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: floor12 |
5
|
|
|
* Date: 03.01.2018 |
6
|
|
|
* Time: 19:37 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace floor12\files\logic; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use floor12\files\models\File; |
13
|
|
|
use floor12\files\models\FileType; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\base\ErrorException; |
16
|
|
|
use yii\web\BadRequestHttpException; |
17
|
|
|
use yii\web\NotFoundHttpException; |
18
|
|
|
|
19
|
|
|
class FileCropRotate |
20
|
|
|
{ |
21
|
|
|
private $_file; |
22
|
|
|
private $_width; |
23
|
|
|
private $_height; |
24
|
|
|
private $_top; |
25
|
|
|
private $_left; |
26
|
|
|
private $_rotated; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
public function __construct(array $data) |
30
|
|
|
{ |
31
|
|
|
$this->_file = File::findOne($data['id']); |
32
|
|
|
|
33
|
|
|
if (!$this->_file) |
34
|
|
|
throw new NotFoundHttpException('File not found'); |
35
|
|
|
|
36
|
|
|
if ($this->_file->type != FileType::IMAGE) |
37
|
|
|
throw new BadRequestHttpException('Requested file is not an image.'); |
38
|
|
|
|
39
|
|
|
if (!file_exists($this->_file->rootPath)) |
|
|
|
|
40
|
|
|
throw new BadRequestHttpException('File not found in file storage.'); |
41
|
|
|
|
42
|
|
|
$this->_height = (int)$data['height']; |
43
|
|
|
$this->_width = (int)$data['width']; |
44
|
|
|
$this->_top = (int)$data['top']; |
45
|
|
|
$this->_left = (int)$data['left']; |
46
|
|
|
$this->_rotated = (int)$data['rotated']; |
47
|
|
|
|
48
|
|
|
if (!$this->_height && !$this->_width) { |
49
|
|
|
list($this->_width, $this->_height) = getimagesize($this->_file->rootPath); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function execute() |
56
|
|
|
{ |
57
|
|
|
|
58
|
|
|
$src = $this->imageCreateFromAny(); |
59
|
|
|
|
60
|
|
|
$src = imagerotate($src, -$this->_rotated, 0); |
61
|
|
|
|
62
|
|
|
$dest = imagecreatetruecolor($this->_width, $this->_height); |
63
|
|
|
|
64
|
|
|
imagecopy($dest, $src, 0, 0, $this->_left, $this->_top, $this->_width, $this->_height); |
65
|
|
|
|
66
|
|
|
$newName = new PathGenerator(Yii::$app->getModule('files')->storageFullPath) . '.jpeg'; |
67
|
|
|
|
68
|
|
|
$newPath = Yii::$app->getModule('files')->storageFullPath . '/' . $newName; |
|
|
|
|
69
|
|
|
|
70
|
|
|
$oldPath = $this->_file->rootPath; |
71
|
|
|
|
72
|
|
|
imagejpeg($dest, $newPath, 80); |
73
|
|
|
|
74
|
|
|
imagedestroy($dest); |
75
|
|
|
|
76
|
|
|
imagedestroy($src); |
77
|
|
|
|
78
|
|
|
$this->_file->filename = $newName; |
79
|
|
|
$this->_file->content_type = $this->_file->mime_content_type($newPath); |
80
|
|
|
$this->_file->size = filesize($newPath); |
81
|
|
|
$this->_file->changeHash(); |
82
|
|
|
if ($this->_file->save()) { |
83
|
|
|
@unlink($oldPath); |
|
|
|
|
84
|
|
|
return $this->_file->href; |
85
|
|
|
} else |
86
|
|
|
throw new ErrorException("Error while saving file model."); |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Method to read files from any mime types |
94
|
|
|
* @return resource |
95
|
|
|
* @throws BadRequestHttpException |
96
|
|
|
*/ |
97
|
|
|
|
98
|
|
|
private function imageCreateFromAny() |
99
|
|
|
{ |
100
|
|
|
$type = exif_imagetype($this->_file->rootPath); |
|
|
|
|
101
|
|
|
$allowedTypes = array( |
102
|
|
|
1, // [] gif |
103
|
|
|
2, // [] jpg |
104
|
|
|
3, // [] png |
105
|
|
|
6 // [] bmp |
106
|
|
|
); |
107
|
|
|
if (!in_array($type, $allowedTypes)) { |
108
|
|
|
throw new BadRequestHttpException('File must have GIF, JPG, PNG or BMP mime-type.'); |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
switch ($type) { |
112
|
|
|
case 1 : |
113
|
|
|
$im = imageCreateFromGif($this->_file->rootPath); |
114
|
|
|
break; |
115
|
|
|
case 2 : |
116
|
|
|
$im = imageCreateFromJpeg($this->_file->rootPath); |
117
|
|
|
break; |
118
|
|
|
case 3 : |
119
|
|
|
$im = imageCreateFromPng($this->_file->rootPath); |
120
|
|
|
break; |
121
|
|
|
case 6 : |
122
|
|
|
$im = imageCreateFromBmp($this->_file->rootPath); |
123
|
|
|
break; |
124
|
|
|
} |
125
|
|
|
return $im; |
|
|
|
|
126
|
|
|
} |
127
|
|
|
} |