|
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 Gregwar\Image\Image; |
|
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
|
|
|
private $fileSystem; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct($targetDirectory) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->targetDirectory = $targetDirectory; |
|
30
|
|
|
$this->fileSystem = new Filesystem(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function validate(UploadedFile $file): ConstraintViolationListInterface |
|
34
|
|
|
{ |
|
35
|
|
|
$validator = Validation::createValidator(); |
|
36
|
|
|
$violations = $validator->validate( |
|
37
|
|
|
$file, |
|
38
|
|
|
[ |
|
39
|
|
|
new NotBlank([ |
|
40
|
|
|
'message' => 'Please select a file to upload', |
|
41
|
|
|
]), |
|
42
|
|
|
new File([ |
|
43
|
|
|
'maxSize' => '10M', |
|
44
|
|
|
'mimeTypes' => [ |
|
45
|
|
|
'image/*', |
|
46
|
|
|
], |
|
47
|
|
|
]), |
|
48
|
|
|
] |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
return $violations; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function upload(UploadedFile $file): string |
|
55
|
|
|
{ |
|
56
|
|
|
$fileName = md5(uniqid()).'.'.$file->guessExtension(); |
|
57
|
|
|
$file->move($this->targetDirectory, $fileName); |
|
58
|
|
|
|
|
59
|
|
|
// Small |
|
60
|
|
|
Image::open($this->targetDirectory.'/'.$fileName) |
|
61
|
|
|
->zoomCrop(500, 300, 'transparent', 'center', 'center') |
|
62
|
|
|
->save($this->targetDirectory.'/small/'.$fileName); |
|
63
|
|
|
|
|
64
|
|
|
// Medium |
|
65
|
|
|
Image::open($this->targetDirectory.'/'.$fileName) |
|
66
|
|
|
->zoomCrop(700, 420, 'transparent', 'center', 'center') |
|
67
|
|
|
->save($this->targetDirectory.'/medium/'.$fileName); |
|
68
|
|
|
|
|
69
|
|
|
// Large |
|
70
|
|
|
Image::open($this->targetDirectory.'/'.$fileName) |
|
71
|
|
|
->cropResize(1200, 800, 'transparent') |
|
72
|
|
|
->save($this->targetDirectory.'/large/'.$fileName); |
|
73
|
|
|
|
|
74
|
|
|
// Full |
|
75
|
|
|
$this->fileSystem->rename( |
|
76
|
|
|
$this->targetDirectory.'/'.$fileName, $this->targetDirectory.'/full/'.$fileName |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
return $fileName; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function remove(string $fileName): void |
|
83
|
|
|
{ |
|
84
|
|
|
$folders = [ |
|
85
|
|
|
'/small/', |
|
86
|
|
|
'/medium/', |
|
87
|
|
|
'/full/', |
|
88
|
|
|
'/large/', |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
foreach ($folders as $folder) { |
|
92
|
|
|
$this->fileSystem->remove($this->targetDirectory.$folder.$fileName); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|