1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ecodev\Felix\Model\Traits; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping as ORM; |
8
|
|
|
use GraphQL\Doctrine\Annotation as API; |
9
|
|
|
use Imagine\Filter\Basic\Autorotate; |
10
|
|
|
use Imagine\Image\ImagineInterface; |
11
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* An image and some information about it |
15
|
|
|
*/ |
16
|
|
|
trait Image |
17
|
|
|
{ |
18
|
|
|
use AbstractFile { |
19
|
|
|
setFile as abstractFileSetFile; |
20
|
|
|
} |
21
|
|
|
|
22
|
1 |
|
protected function getBasePath(): string |
23
|
|
|
{ |
24
|
1 |
|
return 'data/images/'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function getAcceptedMimeTypes(): array |
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
|
|
'image/bmp', |
31
|
|
|
'image/gif', |
32
|
|
|
'image/jpeg', |
33
|
|
|
'image/pjpeg', |
34
|
|
|
'image/png', |
35
|
|
|
'image/svg+xml', |
36
|
|
|
'image/webp', |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int |
42
|
|
|
* @ORM\Column(type="integer") |
43
|
|
|
*/ |
44
|
|
|
private $width = 0; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
* @ORM\Column(type="integer") |
49
|
|
|
*/ |
50
|
|
|
private $height = 0; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get image width |
54
|
|
|
*/ |
55
|
1 |
|
public function getWidth(): int |
56
|
|
|
{ |
57
|
1 |
|
return $this->width; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set image width |
62
|
|
|
* |
63
|
|
|
* @API\Exclude |
64
|
|
|
*/ |
65
|
1 |
|
public function setWidth(int $width): void |
66
|
|
|
{ |
67
|
1 |
|
$this->width = $width; |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get image height |
72
|
|
|
*/ |
73
|
1 |
|
public function getHeight(): int |
74
|
|
|
{ |
75
|
1 |
|
return $this->height; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set image height |
80
|
|
|
* |
81
|
|
|
* @API\Exclude |
82
|
|
|
*/ |
83
|
1 |
|
public function setHeight(int $height): void |
84
|
|
|
{ |
85
|
1 |
|
$this->height = $height; |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the file |
90
|
|
|
*/ |
91
|
|
|
public function setFile(UploadedFileInterface $file): void |
92
|
|
|
{ |
93
|
|
|
$this->abstractFileSetFile($file); |
94
|
|
|
$this->readFileInfo(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Read dimension and size from file on disk |
99
|
|
|
*/ |
100
|
|
|
private function readFileInfo(): void |
101
|
|
|
{ |
102
|
|
|
global $container; |
103
|
|
|
$path = $this->getPath(); |
104
|
|
|
|
105
|
|
|
/** @var ImagineInterface $imagine */ |
106
|
|
|
$imagine = $container->get(ImagineInterface::class); |
107
|
|
|
$image = $imagine->open($path); |
108
|
|
|
|
109
|
|
|
// Auto-rotate image if EXIF says it's rotated, but only JPG, otherwise it might deteriorate other format (SVG) |
110
|
|
|
if ($this->getMime() === 'image/jpeg') { |
111
|
|
|
$autorotate = new Autorotate(); |
112
|
|
|
$autorotate->apply($image); |
113
|
|
|
$image->save($path); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$size = $image->getSize(); |
117
|
|
|
|
118
|
|
|
$this->setWidth($size->getWidth()); |
119
|
|
|
$this->setHeight($size->getHeight()); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|