|
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) |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
$ratio = $height / $this->getHeight(); |
|
85
|
|
|
$width = $this->getWidth() * $ratio; |
|
86
|
|
|
$this->resize($width, $height); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
function getHeight() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
$ratio = $width / $this->getWidth(); |
|
122
|
|
|
$height = $this->getheight() * $ratio; |
|
123
|
|
|
$this->resize((int)$width, (int)$height); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
function scale($scale) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.