Image   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 58
dl 0
loc 122
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A upload() 0 19 4
A checkAngle() 0 18 5
A imageCreate() 0 22 4
A imageGenerate() 0 22 4
1
<?php
2
3
namespace CoffeeCode\Uploader;
4
5
/**
6
 * Class CoffeeCode Image
7
 *
8
 * @author Robson V. Leite <https://github.com/robsonvleite>
9
 * @package CoffeeCode\Uploader
10
 */
11
class Image extends Uploader
12
{
13
    /**
14
     * Allow jpg, png and gif images, use from check. For new extensions check the imageCrete method
15
     * @var array allowed media types
16
     */
17
    protected static $allowTypes = [
18
        "image/jpeg",
19
        "image/png",
20
        "image/gif",
21
    ];
22
23
    /**
24
     * @param array $image
25
     * @param string $name
26
     * @param int $width
27
     * @param array|null $quality
28
     * @return string
29
     * @throws \Exception
30
     */
31
    public function upload(array $image, string $name, int $width = 2000, ?array $quality = null): string
32
    {
33
        if (empty($image['type'])) {
34
            throw new \Exception("Not a valid data from image");
35
        }
36
37
        if (!$this->imageCreate($image)) {
38
            throw new \Exception("Not a valid image type or extension");
39
        } else {
40
            $this->name($name);
41
        }
42
43
        if ($this->ext == "gif") {
44
            move_uploaded_file("{$image['tmp_name']}", "{$this->path}/{$this->name}");
45
            return "{$this->path}/{$this->name}";
46
        }
47
48
        $this->imageGenerate($width, ($quality ?? ["jpg" => 75, "png" => 5]));
49
        return "{$this->path}/{$this->name}";
50
    }
51
52
    /**
53
     * Image create and valid extension from mime-type
54
     * https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#Image_types
55
     *
56
     * @param array $image
57
     * @return bool
58
     */
59
    protected function imageCreate(array $image): bool
60
    {
61
        if ($image['type'] == "image/jpeg") {
62
            $this->file = imagecreatefromjpeg($image['tmp_name']);
63
            $this->ext = "jpg";
64
            $this->checkAngle($image);
65
            return true;
66
        }
67
68
        if ($image['type'] == "image/png") {
69
            $this->file = imagecreatefrompng($image['tmp_name']);
70
            $this->ext = "png";
71
            $this->checkAngle($image);
72
            return true;
73
        }
74
75
        if ($image['type'] == "image/gif") {
76
            $this->ext = "gif";
77
            return true;
78
        }
79
80
        return false;
81
    }
82
83
    /**
84
     * @param int $width
85
     * @param array $quality
86
     */
87
    private function imageGenerate(int $width, array $quality): void
88
    {
89
        $fileX = imagesx($this->file);
90
        $fileY = imagesy($this->file);
91
        $imageW = ($width < $fileX ? $width : $fileX);
92
        $imageH = ($imageW * $fileY) / $fileX;
93
        $imageCreate = imagecreatetruecolor($imageW, $imageH);
94
95
        if ($this->ext == "jpg") {
96
            imagecopyresampled($imageCreate, $this->file, 0, 0, 0, 0, $imageW, $imageH, $fileX, $fileY);
97
            imagejpeg($imageCreate, "{$this->path}/{$this->name}", $quality['jpg']);
98
        }
99
100
        if ($this->ext == "png") {
101
            imagealphablending($imageCreate, false);
102
            imagesavealpha($imageCreate, true);
103
            imagecopyresampled($imageCreate, $this->file, 0, 0, 0, 0, $imageW, $imageH, $fileX, $fileY);
104
            imagepng($imageCreate, "{$this->path}/{$this->name}", $quality['png']);
105
        }
106
107
        imagedestroy($this->file);
108
        imagedestroy($imageCreate);
109
    }
110
111
    /**
112
     * Check image (JPG, PNG) angle and rotate from exif data.
113
     * @param $image
114
     */
115
    private function checkAngle($image): void
116
    {
117
        $exif = @exif_read_data($image["tmp_name"]);
118
        $orientation = (!empty($exif["Orientation"]) ? $exif["Orientation"] : null);
119
120
        switch ($orientation) {
121
            case 8:
122
                $this->file = imagerotate($this->file, 90, 0);
123
                break;
124
            case 3:
125
                $this->file = imagerotate($this->file, 180, 0);
126
                break;
127
            case 6:
128
                $this->file = imagerotate($this->file, -90, 0);
129
                break;
130
        }
131
132
        return;
133
    }
134
}