1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/** |
5
|
|
|
* Created by PhpStorm. |
6
|
|
|
* User: Valery Maslov |
7
|
|
|
* Date: 18.08.2018 |
8
|
|
|
* Time: 17:29. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace App\Service; |
12
|
|
|
|
13
|
|
|
use Gumlet\ImageResize; |
14
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
15
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
16
|
|
|
use Symfony\Component\Validator\Constraints\File; |
17
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
18
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
19
|
|
|
use Symfony\Component\Validator\Validation; |
20
|
|
|
|
21
|
|
|
final class FileUploader |
22
|
|
|
{ |
23
|
|
|
private $targetDirectory; |
24
|
|
|
|
25
|
|
|
public function __construct($targetDirectory) |
26
|
|
|
{ |
27
|
|
|
$this->targetDirectory = $targetDirectory; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function validate(UploadedFile $file): ConstraintViolationListInterface |
31
|
|
|
{ |
32
|
|
|
$validator = Validation::createValidator(); |
33
|
|
|
$violations = $validator->validate( |
34
|
|
|
$file, |
35
|
|
|
[ |
36
|
|
|
new NotBlank([ |
37
|
|
|
'message' => 'Please select a file to upload', |
38
|
|
|
]), |
39
|
|
|
new File([ |
40
|
|
|
'maxSize' => '10M', |
41
|
|
|
'mimeTypes' => [ |
42
|
|
|
'image/*', |
43
|
|
|
], |
44
|
|
|
]), |
45
|
|
|
] |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
return $violations; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function upload(UploadedFile $file): string |
52
|
|
|
{ |
53
|
|
|
$fileSystem = new Filesystem(); |
54
|
|
|
|
55
|
|
|
$fileName = md5(uniqid()).'.'.$file->guessExtension(); |
56
|
|
|
|
57
|
|
|
$file->move($this->getTargetDirectory(), $fileName); |
58
|
|
|
|
59
|
|
|
// Small |
60
|
|
|
$image = new ImageResize($this->getTargetDirectory().'/'.$fileName); |
61
|
|
|
$image->resize(500, 300, $allow_enlarge = true); |
62
|
|
|
$image->save($this->getTargetDirectory().'/small/'.$fileName); |
63
|
|
|
|
64
|
|
|
// Medium |
65
|
|
|
$image = new ImageResize($this->getTargetDirectory().'/'.$fileName); |
66
|
|
|
$image->resize(700, 420, $allow_enlarge = true); |
67
|
|
|
$image->save($this->getTargetDirectory().'/medium/'.$fileName); |
68
|
|
|
|
69
|
|
|
// Large |
70
|
|
|
$image = new ImageResize($this->getTargetDirectory().'/'.$fileName); |
71
|
|
|
$image->resizeToBestFit(1200, 800, $allow_enlarge = true); |
72
|
|
|
$image->save($this->getTargetDirectory().'/large/'.$fileName); |
73
|
|
|
|
74
|
|
|
// Full |
75
|
|
|
$fileSystem->rename($this->getTargetDirectory().'/'.$fileName, $this->getTargetDirectory().'/full/'.$fileName); |
76
|
|
|
|
77
|
|
|
return $fileName; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function remove(string $fileName): void |
81
|
|
|
{ |
82
|
|
|
$fileSystem = new Filesystem(); |
83
|
|
|
|
84
|
|
|
$folders = [ |
85
|
|
|
'/small/', |
86
|
|
|
'/medium/', |
87
|
|
|
'/full/', |
88
|
|
|
'/large/', |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
foreach ($folders as $folder) { |
92
|
|
|
$fileSystem->remove($this->getTargetDirectory().$folder.$fileName); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getTargetDirectory(): string |
97
|
|
|
{ |
98
|
|
|
return $this->targetDirectory; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|