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
|
|
|
* @return int |
56
|
|
|
*/ |
57
|
1 |
|
public function getWidth(): int |
58
|
|
|
{ |
59
|
1 |
|
return $this->width; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set image width |
64
|
|
|
* |
65
|
|
|
* @API\Exclude |
66
|
|
|
* |
67
|
|
|
* @param int $width |
68
|
|
|
*/ |
69
|
1 |
|
public function setWidth(int $width): void |
70
|
|
|
{ |
71
|
1 |
|
$this->width = $width; |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get image height |
76
|
|
|
* |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
1 |
|
public function getHeight(): int |
80
|
|
|
{ |
81
|
1 |
|
return $this->height; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set image height |
86
|
|
|
* |
87
|
|
|
* @API\Exclude |
88
|
|
|
* |
89
|
|
|
* @param int $height |
90
|
|
|
*/ |
91
|
1 |
|
public function setHeight(int $height): void |
92
|
|
|
{ |
93
|
1 |
|
$this->height = $height; |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set the file |
98
|
|
|
* |
99
|
|
|
* @param UploadedFileInterface $file |
100
|
|
|
* |
101
|
|
|
* @throws \Exception |
102
|
|
|
*/ |
103
|
|
|
public function setFile(UploadedFileInterface $file): void |
104
|
|
|
{ |
105
|
|
|
$this->abstractFileSetFile($file); |
106
|
|
|
$this->readFileInfo(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Read dimension and size from file on disk |
111
|
|
|
*/ |
112
|
|
|
private function readFileInfo(): void |
113
|
|
|
{ |
114
|
|
|
global $container; |
115
|
|
|
$path = $this->getPath(); |
116
|
|
|
|
117
|
|
|
/** @var ImagineInterface $imagine */ |
118
|
|
|
$imagine = $container->get(ImagineInterface::class); |
119
|
|
|
$image = $imagine->open($path); |
120
|
|
|
|
121
|
|
|
// Auto-rotate image if EXIF says it's rotated, but only JPG, otherwise it might deteriorate other format (SVG) |
122
|
|
|
if ($this->getMime() === 'image/jpeg') { |
123
|
|
|
$autorotate = new Autorotate(); |
124
|
|
|
$autorotate->apply($image); |
125
|
|
|
$image->save($path); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$size = $image->getSize(); |
129
|
|
|
|
130
|
|
|
$this->setWidth($size->getWidth()); |
131
|
|
|
$this->setHeight($size->getHeight()); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|