SimpleImage::resizeToWidth()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: floor12
5
 * Date: 03.04.2016
6
 * Time: 21:21
7
 */
8
9
namespace floor12\files\components;
10
11
12
use yii\base\ErrorException;
13
use function PHPUnit\Framework\fileExists;
14
15
class SimpleImage
16
{
17
18
    var $image;
19
    var $image_type;
20
21
    function load($filename)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22
    {
23
        $image_info = getimagesize($filename);
24
        $this->image_type = $image_info[2];
25
        if ($this->image_type == IMAGETYPE_JPEG) {
26
            $this->image = imagecreatefromjpeg($filename);
27
        } elseif ($this->image_type == IMAGETYPE_GIF) {
28
            $this->image = imagecreatefromgif($filename);
29
            imageSaveAlpha($this->image, true);
30
        } elseif ($this->image_type == IMAGETYPE_PNG) {
31
            $this->image = @imagecreatefrompng($filename); // https://stackoverflow.com/questions/22745076/libpng-warning-iccp-known-incorrect-srgb-profile
32
            imageSaveAlpha($this->image, true);
0 ignored issues
show
Bug introduced by
It seems like $this->image can also be of type false; however, parameter $image of imagesavealpha() does only seem to accept GdImage|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
            imageSaveAlpha(/** @scrutinizer ignore-type */ $this->image, true);
Loading history...
33
        } elseif ($this->image_type == IMAGETYPE_WEBP) {
34
            $this->image = imagecreatefromwebp($filename);
35
        }
36
    }
37
38
    function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 75, $permissions = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
39
    {
40
        if ($image_type == IMAGETYPE_JPEG) {
41
            imagejpeg($this->image, $filename, $compression);
42
        } elseif ($image_type == IMAGETYPE_GIF) {
43
            imagegif($this->image, $filename);
44
        } elseif ($image_type == IMAGETYPE_PNG) {
45
            imagepng($this->image, $filename);
46
        } elseif ($image_type == IMAGETYPE_WEBP) {
47
            $dst = imagecreatetruecolor(imagesx($this->image), imagesy($this->image));
48
            imagealphablending($dst, false);
49
            imagesavealpha($dst, true);
50
            $transparent = imagecolorallocatealpha($dst, 255, 255, 255, 127);
51
            imagefilledrectangle($dst, 0, 0, imagesx($this->image), imagesy($this->image), $transparent);
52
            imagecopy($dst, $this->image, 0, 0, 0, 0, imagesx($this->image), imagesy($this->image));
53
            imagewebp($dst, $filename);
54
        }
55
56
        if ($permissions != null) {
57
            chmod($filename, $permissions);
58
        }
59
    }
60
61
    function output($image_type = IMAGETYPE_JPEG)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
62
    {
63
        ob_start();
64
        if ($image_type == IMAGETYPE_JPEG) {
65
            imagejpeg($this->image);
66
        } elseif ($image_type == IMAGETYPE_GIF) {
67
            imagegif($this->image);
68
        } elseif ($image_type == IMAGETYPE_PNG) {
69
            imagepng($this->image);
70
        } elseif ($image_type == IMAGETYPE_WEBP) {
71
            $dst = imagecreatetruecolor(imagesx($this->image), imagesy($this->image));
72
            imagealphablending($dst, false);
73
            imagesavealpha($dst, true);
74
            $transparent = imagecolorallocatealpha($dst, 255, 255, 255, 127);
75
            imagefilledrectangle($dst, 0, 0, imagesx($this->image), imagesy($this->image), $transparent);
76
            imagecopy($dst, $this->image, 0, 0, 0, 0, imagesx($this->image), imagesy($this->image));
77
            imagewebp($dst);
78
        }
79
        return ob_get_clean();
80
    }
81
82
    function resizeToHeight($height)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
83
    {
84
        $ratio = $height / $this->getHeight();
85
        $width = $this->getWidth() * $ratio;
86
        $this->resize($width, $height);
87
    }
88
89
    function getHeight()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
90
    {
91
        try {
92
            return imagesy($this->image);
93
        } catch (\Throwable $exception) {
94
            throw new ErrorException('Unable to get height of image. Probably the image is corrupted.');
95
        }
96
97
    }
98
99
    function getWidth()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
100
    {
101
        try {
102
            return imagesx($this->image);
103
        } catch (\Throwable $exception) {
104
            throw new ErrorException('Unable to get width of image. Probably the image is corrupted.');
105
        }
106
    }
107
108
    function resize($width, $height)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
109
    {
110
        $new_image = imagecreatetruecolor($width, $height);
111
        imagealphablending($new_image, false);
112
        imagesavealpha($new_image, true);
113
        $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
114
        imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
115
        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
116
        $this->image = $new_image;
117
    }
118
119
    function resizeToWidth($width)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
120
    {
121
        $ratio = $width / $this->getWidth();
122
        $height = $this->getheight() * $ratio;
123
        $this->resize((int)$width, (int)$height);
124
    }
125
126
    function scale($scale)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
127
    {
128
        $width = $this->getWidth() * $scale / 100;
129
        $height = $this->getheight() * $scale / 100;
130
        $this->resize((int)$width, (int)$height);
131
    }
132
133
    function rotate($direction)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
134
    {
135
        $degrees = 90;
136
        if ($direction == 2)
137
            $degrees = 270;
138
        $this->image = imagerotate($this->image, $degrees, 0);
139
    }
140
141
    function rotateDegrees($degrees)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
142
    {
143
        $this->image = imagerotate($this->image, $degrees, 0);
144
    }
145
146
147
    public function watermark($path)
148
    {
149
        $stamp = imagecreatefrompng($path);
150
151
        $transparentStamp = imagecreatetruecolor($this->getWidth(), $this->getHeight());
152
        imagealphablending($transparentStamp, false);
153
        imagesavealpha($transparentStamp, true);
154
        $transparent = imagecolorallocatealpha($transparentStamp, 255, 255, 255, 127);
155
        imagecolortransparent($transparentStamp, $transparent);
156
        imagefilledrectangle($transparentStamp, 0, 0, $this->getWidth(), $this->getHeight(), $transparent);
157
        imagecopyresampled($transparentStamp, $stamp, 0, 0, 0, 0, $this->getWidth(), $this->getHeight(), $this->getWidth(), $this->getHeight());
158
159
        $newImage = imagecreatetruecolor($this->getWidth(), $this->getHeight());
160
        imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $this->getWidth(), $this->getHeight(), $this->getWidth(), $this->getHeight());
161
        imagecopyresampled($newImage, $transparentStamp, 0, 0, 0, 0, $this->getWidth(), $this->getHeight(), $this->getWidth(), $this->getHeight());
162
        $this->image = $newImage;
163
    }
164
}
165