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
|
|
|
* @var Bookable |
53
|
|
|
* @ORM\ManyToOne(targetEntity="Bookable") |
54
|
|
|
* @ORM\JoinColumns({ |
55
|
|
|
* @ORM\JoinColumn(onDelete="CASCADE") |
56
|
|
|
* }) |
57
|
|
|
*/ |
58
|
|
|
private $bookable; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get image width |
62
|
|
|
* |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
2 |
|
public function getWidth(): int |
66
|
|
|
{ |
67
|
2 |
|
return $this->width; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set image width |
72
|
|
|
* |
73
|
|
|
* @API\Exclude |
74
|
|
|
* |
75
|
|
|
* @param int $width |
76
|
|
|
*/ |
77
|
2 |
|
public function setWidth(int $width): void |
78
|
|
|
{ |
79
|
2 |
|
$this->width = $width; |
80
|
2 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get image height |
84
|
|
|
* |
85
|
|
|
* @return int |
86
|
|
|
*/ |
87
|
2 |
|
public function getHeight(): int |
88
|
|
|
{ |
89
|
2 |
|
return $this->height; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set image height |
94
|
|
|
* |
95
|
|
|
* @API\Exclude |
96
|
|
|
* |
97
|
|
|
* @param int $height |
98
|
|
|
*/ |
99
|
2 |
|
public function setHeight(int $height): void |
100
|
|
|
{ |
101
|
2 |
|
$this->height = $height; |
102
|
2 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set the file |
106
|
|
|
* |
107
|
|
|
* @param UploadedFileInterface $file |
108
|
|
|
* |
109
|
|
|
* @throws \Exception |
110
|
|
|
*/ |
111
|
1 |
|
public function setFile(UploadedFileInterface $file): void |
112
|
|
|
{ |
113
|
1 |
|
parent::setFile($file); |
114
|
1 |
|
$this->readFileInfo(); |
115
|
1 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Read dimension and size from file on disk |
119
|
|
|
*/ |
120
|
1 |
|
private function readFileInfo(): void |
121
|
|
|
{ |
122
|
1 |
|
global $container; |
123
|
1 |
|
$path = $this->getPath(); |
124
|
|
|
|
125
|
|
|
/** @var ImagineInterface $imagine */ |
126
|
1 |
|
$imagine = $container->get(ImagineInterface::class); |
127
|
1 |
|
$image = $imagine->open($path); |
128
|
|
|
|
129
|
|
|
// Auto-rotate image if EXIF says it's rotated |
130
|
1 |
|
$autorotate = new Autorotate(); |
131
|
1 |
|
$autorotate->apply($image); |
132
|
1 |
|
$image->save($path); |
133
|
|
|
|
134
|
1 |
|
$size = $image->getSize(); |
135
|
|
|
|
136
|
1 |
|
$this->setWidth($size->getWidth()); |
137
|
1 |
|
$this->setHeight($size->getHeight()); |
138
|
1 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return Bookable |
142
|
|
|
*/ |
143
|
|
|
public function getBookable(): Bookable |
144
|
|
|
{ |
145
|
|
|
return $this->bookable; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param Bookable $bookable |
150
|
|
|
*/ |
151
|
1 |
|
public function setBookable(Bookable $bookable): void |
152
|
|
|
{ |
153
|
1 |
|
$this->bookable = $bookable; |
154
|
1 |
|
} |
155
|
|
|
} |
156
|
|
|
|