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