|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Model; |
|
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
|
|
|
* A card containing an image and some information about it |
|
15
|
|
|
* |
|
16
|
|
|
* @ORM\HasLifecycleCallbacks |
|
17
|
|
|
* @ORM\Entity(repositoryClass="Application\Repository\ImageRepository") |
|
18
|
|
|
*/ |
|
19
|
|
|
class Image extends AbstractFile |
|
20
|
|
|
{ |
|
21
|
3 |
|
protected function getBasePath(): string |
|
22
|
|
|
{ |
|
23
|
3 |
|
return 'data/images/'; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
1 |
|
protected function getAcceptedMimeTypes(): array |
|
27
|
|
|
{ |
|
28
|
|
|
return [ |
|
29
|
1 |
|
'image/bmp', |
|
30
|
|
|
'image/gif', |
|
31
|
|
|
'image/jpeg', |
|
32
|
|
|
'image/pjpeg', |
|
33
|
|
|
'image/png', |
|
34
|
|
|
'image/svg+xml', |
|
35
|
|
|
'image/webp', |
|
36
|
|
|
]; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var int |
|
41
|
|
|
* @ORM\Column(type="integer") |
|
42
|
|
|
*/ |
|
43
|
|
|
private $width = 0; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var int |
|
47
|
|
|
* @ORM\Column(type="integer") |
|
48
|
|
|
*/ |
|
49
|
|
|
private $height = 0; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get image width |
|
53
|
|
|
* |
|
54
|
|
|
* @return int |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function getWidth(): int |
|
57
|
|
|
{ |
|
58
|
2 |
|
return $this->width; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Set image width |
|
63
|
|
|
* |
|
64
|
|
|
* @API\Exclude |
|
65
|
|
|
* |
|
66
|
|
|
* @param int $width |
|
67
|
|
|
*/ |
|
68
|
2 |
|
public function setWidth(int $width): void |
|
69
|
|
|
{ |
|
70
|
2 |
|
$this->width = $width; |
|
71
|
2 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get image height |
|
75
|
|
|
* |
|
76
|
|
|
* @return int |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function getHeight(): int |
|
79
|
|
|
{ |
|
80
|
2 |
|
return $this->height; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Set image height |
|
85
|
|
|
* |
|
86
|
|
|
* @API\Exclude |
|
87
|
|
|
* |
|
88
|
|
|
* @param int $height |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function setHeight(int $height): void |
|
91
|
|
|
{ |
|
92
|
2 |
|
$this->height = $height; |
|
93
|
2 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Set the file |
|
97
|
|
|
* |
|
98
|
|
|
* @param UploadedFileInterface $file |
|
99
|
|
|
* |
|
100
|
|
|
* @throws \Exception |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public function setFile(UploadedFileInterface $file): void |
|
103
|
|
|
{ |
|
104
|
1 |
|
parent::setFile($file); |
|
105
|
1 |
|
$this->readFileInfo(); |
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Read dimension and size from file on disk |
|
110
|
|
|
*/ |
|
111
|
1 |
|
private function readFileInfo(): void |
|
112
|
|
|
{ |
|
113
|
1 |
|
global $container; |
|
114
|
1 |
|
$path = $this->getPath(); |
|
115
|
|
|
|
|
116
|
|
|
/** @var ImagineInterface $imagine */ |
|
117
|
1 |
|
$imagine = $container->get(ImagineInterface::class); |
|
118
|
1 |
|
$image = $imagine->open($path); |
|
119
|
|
|
|
|
120
|
|
|
// Auto-rotate image if EXIF says it's rotated |
|
121
|
1 |
|
$autorotate = new Autorotate(); |
|
122
|
1 |
|
$autorotate->apply($image); |
|
123
|
1 |
|
$image->save($path); |
|
124
|
|
|
|
|
125
|
1 |
|
$size = $image->getSize(); |
|
126
|
|
|
|
|
127
|
1 |
|
$this->setWidth($size->getWidth()); |
|
128
|
1 |
|
$this->setHeight($size->getHeight()); |
|
129
|
1 |
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|