Failed Conditions
Push — master ( f78e48...913fd9 )
by Adrien
15:58
created

Image::readFileInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 20
ccs 0
cts 11
cp 0
crap 6
rs 9.9
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/x-ms-bmp',
32
            'image/gif',
33
            'image/jpeg',
34
            'image/pjpeg',
35
            'image/png',
36
            'image/svg+xml',
37
            'image/webp',
38
        ];
39
    }
40
41
    /**
42
     * @var int
43
     * @ORM\Column(type="integer")
44
     */
45
    private $width = 0;
46
47
    /**
48
     * @var int
49
     * @ORM\Column(type="integer")
50
     */
51
    private $height = 0;
52
53
    /**
54
     * Get image width.
55
     */
56 1
    public function getWidth(): int
57
    {
58 1
        return $this->width;
59
    }
60
61
    /**
62
     * Set image width.
63
     *
64
     * @API\Exclude
65
     */
66 1
    public function setWidth(int $width): void
67
    {
68 1
        $this->width = $width;
69
    }
70
71
    /**
72
     * Get image height.
73
     */
74 1
    public function getHeight(): int
75
    {
76 1
        return $this->height;
77
    }
78
79
    /**
80
     * Set image height.
81
     *
82
     * @API\Exclude
83
     */
84 1
    public function setHeight(int $height): void
85
    {
86 1
        $this->height = $height;
87
    }
88
89
    /**
90
     * Set the file.
91
     */
92
    public function setFile(UploadedFileInterface $file): void
93
    {
94
        $this->abstractFileSetFile($file);
95
        $this->readFileInfo();
96
    }
97
98
    /**
99
     * Read dimension and size from file on disk.
100
     */
101
    private function readFileInfo(): void
102
    {
103
        global $container;
104
        $path = $this->getPath();
105
106
        /** @var ImagineInterface $imagine */
107
        $imagine = $container->get(ImagineInterface::class);
108
        $image = $imagine->open($path);
109
110
        // Auto-rotate image if EXIF says it's rotated, but only JPG, otherwise it might deteriorate other format (SVG)
111
        if ($this->getMime() === 'image/jpeg') {
112
            $autorotate = new Autorotate();
113
            $autorotate->apply($image);
114
            $image->save($path);
115
        }
116
117
        $size = $image->getSize();
118
119
        $this->setWidth($size->getWidth());
120
        $this->setHeight($size->getHeight());
121
    }
122
}
123