Issues (55)

src/images/Image.php (2 issues)

1
<?php
2
/**
3
 * Image
4
 *
5
 * @Author : Jens Kooij
6
 * @Version: 1.0
7
 * @package: JNS MVC
8
 * @Licence: http://creativecommons.org/licenses/by-nc-nd/3.0/ Attribution-NonCommercial-NoDerivs 3.0 Unported
9
 */
10
11
namespace CloudControl\Cms\images {
12
13
    class Image
14
    {
15
        /**
16
         * @var resource
17
         */
18
        private $_imageResource;
19
20
        /**
21
         * Load the a image resource into $this->_imageResource
22
         * automagically :-)
23
         *
24
         * @param resource|string $imageContainer
25
         *
26
         * @throws \Exception
27
         */
28
        public function loadImage($imageContainer)
29
        {
30
            if ($this->createImageResourceFromResource($imageContainer) ||
31
                $this->createImageResourceFromFile($imageContainer) ||
32
                $this->createImageResourceFromString($imageContainer)
33
            ) {
34
                return;
35
            }
36
37
            throw new \RuntimeException('Could not create image resource, accepted inputs are: "resource of type (gd)", path_to_image and "string". <br /><pre>' . var_export($imageContainer,
38
                    true) . '</pre>');
39
        }
40
41
        /**
42
         * @param $imageContainer
43
         * @return bool
44
         */
45
        private function createImageResourceFromResource($imageContainer)
46
        {
47
            if (is_resource($imageContainer) && get_resource_type($imageContainer) === 'gd') {
48
                $this->_imageResource = $imageContainer;
49
                return true;
50
            }
51
            return false;
52
        }
53
54
        /**
55
         * @param $imageContainer
56
         * @return bool
57
         * @throws \Exception
58
         */
59
        private function createImageResourceFromFile($imageContainer)
60
        {
61
            if (is_string($imageContainer) && file_exists($imageContainer)) {
62
                if ($this->getImageMimeType($imageContainer) === IMAGETYPE_BMP) {
63
                    $this->_imageResource = BitmapFactory::createImageFromBmp($imageContainer);
64
                } else {
65
                    $imageContent = file_get_contents($imageContainer);
66
                    $this->_imageResource = imagecreatefromstring($imageContent);
0 ignored issues
show
Documentation Bug introduced by
It seems like imagecreatefromstring($imageContent) can also be of type false. However, the property $_imageResource 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...
67
                }
68
                return true;
69
            }
70
            return false;
71
        }
72
73
        /**
74
         * @param $imageContainer
75
         * @return bool
76
         */
77
        private function createImageResourceFromString($imageContainer)
78
        {
79
            if (is_string($imageContainer)) {
80
                $this->_imageResource = imagecreatefromstring($imageContainer);
0 ignored issues
show
Documentation Bug introduced by
It seems like imagecreatefromstring($imageContainer) can also be of type false. However, the property $_imageResource 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
                return true;
82
            }
83
            return false;
84
        }
85
86
        /**
87
         * Saves the image to a file
88
         *
89
         * @param string $path
90
         * @param int $mimeTypeConstantValue
91
         * @param int $quality
92
         * @param resource $imageResource If no resource is given, uses $this->_imageResource
93
         *
94
         * @return bool
95
         * @throws \Exception
96
         */
97
        public function saveImage($path, $mimeTypeConstantValue, $quality = 100, $imageResource = null)
98
        {
99
            if ($imageResource === null) {
100
                $imageResource = $this->getImageResource();
101
            }
102
103
            if ($mimeTypeConstantValue == IMAGETYPE_GIF) {
104
                return imagegif($imageResource, $path);
105
            }
106
107
            if ($mimeTypeConstantValue == IMAGETYPE_JPEG) {
108
                return imagejpeg($imageResource, $path, $quality);
109
            }
110
111
            if ($mimeTypeConstantValue == IMAGETYPE_PNG) {
112
                return imagepng($imageResource, $path, ((int)($quality / 10) - 1));
113
            }
114
115
            throw new \RuntimeException('Not a valid mimetypeconstant given see function documentation');
116
        }
117
118
        /**
119
         * Returns either the Mime-Type Constant value
120
         * or the default extension for the detected mime-type;
121
         *
122
         * @see http://www.php.net/manual/en/function.image-type-to-mime-type.php
123
         *
124
         * @param string $imagePath
125
         * @param bool $getExtension
126
         *
127
         * @return integer
128
         */
129
        public function getImageMimeType($imagePath, $getExtension = false)
130
        {
131
            if (function_exists('exif_imagetype')) {
132
                $exif = exif_imagetype($imagePath);
133
            } else {
134
                $exif = false;
135
                if ((list($width, $height, $type, $attr) = getimagesize($imagePath)) !== false) {
136
                    $exif = $type;
137
                }
138
            }
139
140
            return $getExtension ? image_type_to_extension($exif) : $exif;
141
        }
142
143
144
145
        /**
146
         * Returns the image resource
147
         * @return resource
148
         * @throws \Exception
149
         */
150
        final public function getImageResource()
151
        {
152
            if (is_resource($this->_imageResource) && get_resource_type($this->_imageResource) === 'gd') {
153
                return $this->_imageResource;
154
            }
155
156
            throw new \RuntimeException('Image resource is not set. Use $this->LoadImage to load an image into the resource');
157
        }
158
    }
159
}