|
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'] ?? 0); |
|
47
|
|
|
|
|
48
|
|
|
if (!$this->_height && !$this->_width) { |
|
49
|
|
|
list($this->_width, $this->_height) = getimagesize($this->_file->rootPath); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function execute() |
|
55
|
|
|
{ |
|
56
|
|
|
|
|
57
|
|
|
$src = $this->imageCreateFromAny(); |
|
58
|
|
|
|
|
59
|
|
|
$src = imagerotate($src, -$this->_rotated, 0); |
|
60
|
|
|
|
|
61
|
|
|
$dest = imagecreatetruecolor($this->_width, $this->_height); |
|
62
|
|
|
|
|
63
|
|
|
imagecopy($dest, $src, 0, 0, $this->_left, $this->_top, $this->_width, $this->_height); |
|
64
|
|
|
|
|
65
|
|
|
$newName = new PathGenerator(Yii::$app->getModule('files')->storageFullPath) . '.jpeg'; |
|
66
|
|
|
|
|
67
|
|
|
$newPath = Yii::$app->getModule('files')->storageFullPath . '/' . $newName; |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
$oldPath = $this->_file->rootPath; |
|
70
|
|
|
|
|
71
|
|
|
imagejpeg($dest, $newPath, 80); |
|
72
|
|
|
|
|
73
|
|
|
imagedestroy($dest); |
|
74
|
|
|
|
|
75
|
|
|
imagedestroy($src); |
|
76
|
|
|
|
|
77
|
|
|
$this->_file->filename = $newName; |
|
78
|
|
|
$this->_file->content_type = $this->_file->mime_content_type($newPath); |
|
79
|
|
|
$this->_file->size = filesize($newPath); |
|
80
|
|
|
$this->_file->changeHash(); |
|
81
|
|
|
if ($this->_file->save()) { |
|
82
|
|
|
@unlink($oldPath); |
|
|
|
|
|
|
83
|
|
|
return $this->_file->href; |
|
84
|
|
|
} else |
|
85
|
|
|
throw new ErrorException("Error while saving file model."); |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Method to read files from any mime types |
|
93
|
|
|
* @return resource |
|
94
|
|
|
* @throws BadRequestHttpException |
|
95
|
|
|
*/ |
|
96
|
|
|
|
|
97
|
|
|
private function imageCreateFromAny() |
|
98
|
|
|
{ |
|
99
|
|
|
$type = exif_imagetype($this->_file->rootPath); |
|
|
|
|
|
|
100
|
|
|
$allowedTypes = array( |
|
101
|
|
|
1, // [] gif |
|
102
|
|
|
2, // [] jpg |
|
103
|
|
|
3, // [] png |
|
104
|
|
|
6 // [] bmp |
|
105
|
|
|
); |
|
106
|
|
|
if (!in_array($type, $allowedTypes)) { |
|
107
|
|
|
throw new BadRequestHttpException('File must have GIF, JPG, PNG or BMP mime-type.'); |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
switch ($type) { |
|
111
|
|
|
case 1 : |
|
112
|
|
|
$im = imageCreateFromGif($this->_file->rootPath); |
|
113
|
|
|
break; |
|
114
|
|
|
case 2 : |
|
115
|
|
|
$im = imageCreateFromJpeg($this->_file->rootPath); |
|
116
|
|
|
break; |
|
117
|
|
|
case 3 : |
|
118
|
|
|
$im = imageCreateFromPng($this->_file->rootPath); |
|
119
|
|
|
break; |
|
120
|
|
|
case 6 : |
|
121
|
|
|
$im = imageCreateFromBmp($this->_file->rootPath); |
|
122
|
|
|
break; |
|
123
|
|
|
} |
|
124
|
|
|
return $im; |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
} |