Failed Conditions
Push — master ( 389cea...9cc91e )
by Adrien
13:06
created

Image::setFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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