Issues (8)

src/Image.php (2 issues)

1
<?php
2
3
namespace CodeBlog\DataUploader;
4
5
/**
6
 * Class CodeBlog Image
7
 *
8
 * @author Whallysson Avelino <https://github.com/whallysson>
9
 * @package CodeBlog\DataUploader
10
 */
11
class Image extends DataUploader {
12
13
    /** @var array */
14
    protected static $quality = ["jpg" => 75, "png" => 5];
15
16
    /** @var array */
17
    protected static $allowJPG = [
18
        "image/jpg",
19
        "image/jpeg",
20
        "image/pjpeg"
21
    ];
22
23
    /** @var array */
24
    protected static $allowPNG = [
25
        "image/png",
26
        "image/x-png"
27
    ];
28
29
    /** @var array */
30
    protected static $allowGIF = [
31
        "image/gif"
32
    ];
33
    
34
    /**
35
     * Allow zip, rar, bzip, pdf, doc, docx files
36
     * @var array allowed file types
37
     * https://www.freeformatter.com/mime-types-list.html
38
     */
39
    protected static $allowTypes = [
40
        
41
    ];
42
43
    /**
44
     * @param array $image
45
     * @param string $name
46
     * @param int $width
47
     * @param array|null $quality
48
     * @return string
49
     * @throws \Exception
50
     */
51
    public function upload(array $image, string $name, int $width = 2000, ?array $quality = null) {
52
        if (empty($image['type'])) {
53
            throw new \Exception("Not a valid data from image");
54
        }
55
56
        if (!$this->create($image)) {
57
            throw new \Exception("Not a valid image type or extension");
58
        } else {
59
            $this->name($name);
60
        }
61
62
        if ($this->ext == "gif") {
63
            move_uploaded_file("{$image['tmp_name']}", "{$this->path}/{$this->name}");
64
            return "{$this->path}/{$this->name}";
65
        }
66
67
        $this->generate($width, ($quality ? $quality : static::$quality));
68
        return "{$this->path}/{$this->name}";
69
    }
70
71
    /**
72
     * Image create and valid extension from mime-type
73
     * https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#Image_types
74
     *
75
     * @param array $image
76
     * @return bool
77
     */
78
    protected function create(array $image) {
79
        if (in_array($image['type'], static::$allowJPG)) {
80
            $this->file = imagecreatefromjpeg($image['tmp_name']);
0 ignored issues
show
Documentation Bug introduced by
It seems like imagecreatefromjpeg($image['tmp_name']) can also be of type false. However, the property $file is declared as type resource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
81
            $this->ext = "jpg";
82
            $this->angle($image);
83
            return true;
84
        }
85
86
        if (in_array($image['type'], static::$allowPNG)) {
87
            $this->file = imagecreatefrompng($image['tmp_name']);
88
            $this->ext = "png";
89
            $this->angle($image);
90
            return true;
91
        }
92
93
        if (in_array($image['type'], static::$allowGIF)) {
94
            $this->ext = "gif";
95
            return true;
96
        }
97
98
        return false;
99
    }
100
101
    /**
102
     * @param int $width
103
     * @param array $quality
104
     */
105
    private function generate(int $width, array $quality) {
106
        $fileX = imagesx($this->file);
107
        $fileY = imagesy($this->file);
108
        $imageW = ($width < $fileX ? $width : $fileX);
109
        $imageH = ($imageW * $fileY) / $fileX;
110
        $imageCreate = imagecreatetruecolor($imageW, $imageH);
111
112
        if ($this->ext == "jpg") {
113
            imagecopyresampled($imageCreate, $this->file, 0, 0, 0, 0, $imageW, $imageH, $fileX, $fileY);
114
            imagejpeg($imageCreate, "{$this->path}/{$this->name}", $quality['jpg']);
115
        }
116
117
        if ($this->ext == "png") {
118
            imagealphablending($imageCreate, false);
119
            imagesavealpha($imageCreate, true);
120
            imagecopyresampled($imageCreate, $this->file, 0, 0, 0, 0, $imageW, $imageH, $fileX, $fileY);
121
            imagepng($imageCreate, "{$this->path}/{$this->name}", $quality['png']);
122
        }
123
124
        imagedestroy($this->file);
125
        imagedestroy($imageCreate);
126
    }
127
128
    /**
129
     * Check image (JPG, PNG) angle and rotate from exif data.
130
     * @param $image
131
     */
132
    private function angle($image) {
133
        $exif = @exif_read_data($image["tmp_name"]);
134
        $orientation = (!empty($exif["Orientation"]) ? $exif["Orientation"] : null);
135
136
        switch ($orientation) {
137
            case 8:
138
                $this->file = imagerotate($this->file, 90, 0);
0 ignored issues
show
Documentation Bug introduced by
It seems like imagerotate($this->file, 90, 0) can also be of type false. However, the property $file is declared as type resource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
139
                break;
140
            case 3:
141
                $this->file = imagerotate($this->file, 180, 0);
142
                break;
143
            case 6:
144
                $this->file = imagerotate($this->file, -90, 0);
145
                break;
146
        }
147
148
        return;
149
    }
150
151
}
152