-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.php
More file actions
203 lines (181 loc) · 5.85 KB
/
Image.php
File metadata and controls
203 lines (181 loc) · 5.85 KB
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
<?php
namespace Colibri\Util;
/**
* Image manipulations.
*/
class Image
{
/**
* Type of resize.
*/
const RESIZE_FILLED = 'filled';
/**
* Type of resize.
*/
const RESIZE_CROPPED = 'cropped';
/**
* @param string $path
* @param int $width
* @param int $height
* @param string $resizeType
* @param int $bgColor
*
* @return string binary stream with thumbnail
*
* @throws \Exception
*/
public static function createThumbnail($path, $width = 100, $height = 100, $resizeType = self::RESIZE_FILLED, $bgColor = 0xfff5ee)
{
$img = self::createFromFileByMime($path);
// Build the thumbnail
$new_img = imagecreatetruecolor($width, $height);
if ($resizeType == self::RESIZE_FILLED) {
// Fill the image gray
if ( ! imagefilledrectangle($new_img, 0, 0, $width - 1, $height - 1, $bgColor)) {
throw new \Exception('can`t create thumbnail: can`t fill new image by bg-color');
}
}
// Resize and copy to the new image
list(
$src_x, $src_y, $src_w, $src_h,
$dst_x, $dst_y, $dst_w, $dst_h
)
= self::calcResizeParams($resizeType, $img, $width, $height);
if ( ! imagecopyresampled($new_img, $img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)) {
throw new \Exception('can`t create thumbnail: can`t resize image');
}
return self::getJpegToVar($new_img);
}
/**
* @param string $resizeType
* @param resource $img
* @param int $tmbWidth
* @param int $tmbHeight
*
* @return array
*
* @throws \Exception
*/
private static function calcResizeParams($resizeType, $img, $tmbWidth, $tmbHeight)
{
list($width, $height) = self::getImageSize($img);
switch ($resizeType) {
case self::RESIZE_FILLED:
$tmb_ratio = $tmbWidth / $tmbHeight;
$img_ratio = $width / $height;
if ($tmb_ratio > $img_ratio) {
$new_width = $img_ratio * $tmbHeight;
$new_height = $tmbHeight;
} else {
$new_width = $tmbWidth;
$new_height = $tmbWidth / $img_ratio;
}
if ($new_height > $tmbHeight) {
$new_height = $tmbHeight;
}
if ($new_width > $tmbWidth) {
$new_height = $tmbWidth;
}
return [
0, // $src_x
0, // $src_y
$width, // $src_w
$height, // $src_h
($tmbWidth - $new_width) / 2, // $dst_x
($tmbHeight - $new_height) / 2, // $dst_y
$new_width, // $dst_w
$new_height, // $dst_h
];
case self::RESIZE_CROPPED:
if ($tmbWidth != $tmbHeight) {
throw new \Exception('this resize type implemented only for square thumbnails');
}
if ($width > $height) {
$x = ($width - $height) / 2;
$y = 0;
$w =
$h = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$w =
$h = $width;
}
return [
$x, // $src_x
$y, // $src_y
$w, // $src_w
$h, // $src_h
0, // $dst_x
0, // $dst_y
$tmbWidth, // $dst_w
$tmbHeight, // $dst_h
];
default:
throw new \Exception('unknown resize type');
}
}
/**
* @param string $path
*
* @return resource GD image
*
* @throws \Exception
*/
private static function createFromFileByMime($path)
{
$mime = File::getMimeType($path);
switch ($mime) {
case 'image/jpeg':
$img = imagecreatefromjpeg($path);
break;
case 'image/gif':
$img = imagecreatefromgif($path);
break;
case 'image/png':
$img = imagecreatefrompng($path);
break;
default:
throw new \Exception('can`t create thumbnail: unknown image type');
}
if ( ! $img) {
throw new \Exception('can`t create thumbnail: can`t create image handle from file ' . $path);
}
return $img;
}
/**
* @param resource $img GD image
*
* @return array
*
* @throws \Exception
*/
private static function getImageSize($img)
{
$width = imagesx($img);
$height = imagesy($img);
if ( ! $width || ! $height) {
throw new \Exception('can`t create thumbnail: can`t get image size, invalid image width or height');
}
return [$width, $height];
}
/**
* @param resource $img
*
* @return string
*
* @throws \Exception
*/
private static function getJpegToVar($img)
{
// Use a output buffering to load the image into a variable
ob_start();
if ( ! imagejpeg($img)) {
ob_end_clean();
throw new \Exception('can`t create thumbnail: can`t output thumbnail into var');
}
$imageVariable = ob_get_contents();
ob_end_clean();
return $imageVariable;
}
}