-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleImage.php
242 lines (218 loc) · 9.1 KB
/
SimpleImage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* @author Foma Tuturov <fomiash@yandex.ru>
*/
namespace Phphleb\Imageresizer;
class SimpleImage
{
protected $filename;
protected $image;
protected $image_type;
protected $image_format;
// Загрузка исходного файла по ссылке
function load($filename)
{
$image_info = getimagesize($filename);
$this->filename = $filename;
$this->image_type = $image_info[2];
$this->image_format = trim(image_type_to_extension($this->image_type), ".");
if ($this->image_type == IMAGETYPE_JPEG) {
$this->image = imagecreatefromjpeg($filename);
} elseif ($this->image_type == IMAGETYPE_GIF) {
$this->image = imagecreatefromgif($filename);
} elseif ($this->image_type == IMAGETYPE_PNG) {
$this->image = imagecreatefrompng($filename);
} elseif ($this->image_type == IMAGETYPE_WEBP) {
$this->image = imagecreatefromwebp($filename);
} elseif ($this->image_type == IMAGETYPE_BMP) {
$this->image = imagecreatefrombmp($filename);
} elseif ($this->image_type == IMAGETYPE_WBMP) {
$this->image = imagecreatefromwbmp($filename);
}
return empty($this->image) === false;
}
// Сохранение файла
function save($result_filename, $image_type, $compression = 100)
{
if ($image_type == IMAGETYPE_JPEG || strtolower($image_type) == "jpeg" || strtolower($image_type) == "jpg") {
return imagejpeg($this->image, $result_filename, $compression);
} elseif ($image_type == IMAGETYPE_GIF || strtolower($image_type) == "gif") {
return imagegif($this->image, $result_filename);
} elseif ($image_type == IMAGETYPE_PNG || strtolower($image_type) == "png") {
return imagepng($this->image, $result_filename);
} elseif ($image_type == IMAGETYPE_WEBP || strtolower($image_type) == "webp") {
return imagewebp($this->image, $result_filename);
} elseif ($image_type == IMAGETYPE_BMP || strtolower($image_type) == "bmp") {
return imagebmp($this->image, $result_filename);
} elseif ($image_type == IMAGETYPE_WBMP || strtolower($image_type) == "wbmp") {
return imagewbmp($this->image, $result_filename);
}
return false;
}
// Вывод изображения в браузер
function output($image_type = IMAGETYPE_JPEG)
{
if ($image_type == IMAGETYPE_JPEG || strtolower($image_type) == "jpeg" || strtolower($image_type) == "jpg") {
return imagejpeg($this->image);
} elseif ($image_type == IMAGETYPE_GIF || strtolower($image_type) == "gif") {
return imagegif($this->image);
} elseif ($image_type == IMAGETYPE_PNG || strtolower($image_type) == "png") {
return imagepng($this->image);
} elseif ($image_type == IMAGETYPE_WEBP || strtolower($image_type) == "webp") {
return imagewebp($this->image);
} elseif ($image_type == IMAGETYPE_BMP || strtolower($image_type) == "bmp") {
return imagebmp($this->image);
} elseif ($image_type == IMAGETYPE_WBMP || strtolower($image_type) == "wbmp") {
return imagewbmp($this->image);
}
return false;
}
// Возврат данных изображения
function getImage()
{
return $this->image;
}
// Ширина исходного изображения
function getWidth()
{
return imagesx($this->image) ?: 1;
}
// Высота исходного изображения
function getHeight()
{
return imagesy($this->image) ?: 1;
}
// Тип исходного файла
function getImageType()
{
return $this->image_type;
}
// Расширение (по типу) исходного файла
function getImageFormat()
{
return $this->image_format;
}
// Название исходного файла
function getFilePath()
{
return $this->filename;
}
// Пропорциональное изменение размера по высоте
function resizeToHeight($height)
{
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width, $height);
}
// Пропорциональное изменение размера по ширине
function resizeToWidth($width)
{
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width, $height);
}
// Пропорциональное изменение размера в процентном отношении
function scale($scale)
{
$width = $this->getWidth() * $scale / 100;
$height = $this->getheight() * $scale / 100;
$this->resize($width, $height);
}
// Изменение размера по указанным ширине и высоте
function resize($width, $height)
{
$width = ceil($width);
$height = ceil($height);
$new_image = imagecreatetruecolor($width, $height);
$this->imageCopyResampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
// Пропорциональное изменение размера с ориентацией по центру и без пустых пространств по сторонам (лишнее обрезается)
function resizeInCenter($width, $height)
{
$width = ceil($width);
$height = ceil($height);
$new_image = imagecreatetruecolor($width, $height);
$img_width = $this->getWidth();
$img_height = $this->getHeight();
if($img_width == 0 || $img_height == 0) return;
$new_width = $width;
$new_height = $height;
$x = $y = 0;
$dw = ceil( $width / $img_width);
$dh = ceil($height / $img_height);
if ($dw > $dh) {
$new_height = $img_height * $dw;
$y = ceil(($height - $new_height) / 2);
} else if ($dh > $dw) {
$new_width = $img_width * $dh;
$x = ceil(($width - $new_width) / 2);
}
$this->imageCopyResampled($new_image, $this->image, $x, $y, 0, 0, $new_width, $new_height, $img_width, $img_height);
$this->image = $new_image;
}
// Обрезание изображения по указанной области
function cropBySelectedRegion($width, $height, $x, $y)
{
$width = ceil($width);
$height = ceil($height);
$x = ceil($x);
$y = ceil($y);
$new_image = imagecreatetruecolor($width, $height);
$img_width = $this->getWidth();
$img_height = $this->getHeight();
$this->imageCopyResampled($new_image, $this->image, 0, 0, $x, $y, $img_width, $img_height, $img_width, $img_height);
$this->image = $new_image;
}
// Для установки фона в формате RGB (методу resizeAllInCenter)
function addRgbColor($red, $green, $blue)
{
return [$red, $green, $blue];
}
// Пропорциональное изменение размера на прозрачном фоне для PNG (с опциональным закрашиванием для всех типов)
function resizeAllInCenter($width, $height, $background = null)
{
$width = ceil($width);
$height = ceil($height);
list($r, $g, $b) = $background ? (is_array($background) ? $background : sscanf($background, "#%02x%02x%02x")) : [0, 0, 0];
$new_image = imagecreatetruecolor($width, $height);
$color = imagecolorallocate($new_image, $r, $g, $b);
if ($background) {
imagefilledrectangle($new_image, 0, 0, $width, $height, $color);
} else {
imagecolortransparent($new_image, $color);
}
$img_width = $this->getWidth();
$img_height = $this->getHeight();
if($img_width == 0 || $img_height == 0) return;
$new_width = $width;
$new_height = $height;
$x = $y = 0;
$dw = ceil($width / $img_width);
$dh = ceil($height / $img_height);
if ($dw < $dh) {
$new_height = $img_height * $dw;
$y = ceil(($height - $new_height) / 2);
} else if ($dh < $dw) {
$new_width = $img_width * $dh;
$x = ceil(($width - $new_width) / 2);
}
$this->imageCopyResampled($new_image, $this->image, $x, $y, 0, 0, $new_width, $new_height, $img_width, $img_height);
$this->image = $new_image;
}
protected function imageCopyResampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
{
imagecopyresampled(
$dst_image,
$src_image,
intval(ceil($dst_x)),
intval(ceil($dst_y)),
intval(ceil($src_x)),
intval(ceil($src_y)),
intval(ceil($dst_w)),
intval(ceil($dst_h)),
intval($src_w),
intval($src_h)
);
}
}