-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageManipulate.php
232 lines (209 loc) · 6.71 KB
/
ImageManipulate.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
<?php
namespace makroxyz\fileupload;
class ImageManipulate
{
protected static function imageCreateFromAny($filepath)
{
$type = @exif_imagetype($filepath);
$allowedTypes = [
1, // [] gif
2, // [] jpg
3, // [] png
//6 // [] bmp
];
if ($type === false || !in_array($type, $allowedTypes)) {
return false;
}
$savetype = '';
switch ($type) {
case 1 :
$savetype = 'gif';
$im = @imageCreateFromGif($filepath);
break;
case 2 :
$savetype = 'jpeg';
$im = @imageCreateFromJpeg($filepath);
if ($im !== false) {
$exif = @exif_read_data($filepath);
if ($exif!== false && isset($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 8:
$im = imagerotate($im, 90, 0);
break;
case 3:
$im = imagerotate($im, 180, 0);
break;
case 6:
$im = imagerotate($im, -90, 0);
break;
}
}
}
break;
case 3 :
$savetype = 'png';
$im = @imageCreateFromPng($filepath);
break;
/*case 6 :
$savetype = 'png';
$im = imageCreateFromWBmp($filepath);
break;*/
}
if ($im === false) {
return false;
}
return [
'savetype'=>$savetype,
'image'=>$im
];
}
protected static function raiseMemoryLimit()
{
$orig_limit = ini_get('memory_limit');
$mem_limit = trim($orig_limit);
$last = strtolower($mem_limit[strlen($mem_limit)-1]);
switch($last) {
case 'g':
case 'gb':
$mem_limit *= 1024;
case 'm':
case 'mb':
$mem_limit *= 1024;
case 'k':
case 'kb':
$mem_limit *= 1024;
}
if ($mem_limit < 512*1024*1024) {
ini_set('memory_limit', '512M');
}
return $orig_limit;
}
protected static function loadImage($filename)
{
$orig_limit = self::raiseMemoryLimit();
$image_arr = self::imageCreateFromAny($filename);
ini_set('memory_limit', $orig_limit);
if ($image_arr === false) {
return false;
}
$image = $image_arr['image'];
$savetype = $image_arr['savetype'];
$width = imagesx($image);
$height = imagesy($image);
return [$image,$savetype,$width,$height];
}
protected static function saveImage($thumb, $target_fn, $savetype)
{
switch ($savetype) {
case 'gif' :
return imagegif($thumb, $target_fn);
break;
case 'jpeg' :
return imagejpeg($thumb, $target_fn);
break;
case 'png' :
return imagepng($thumb, $target_fn, 9);
break;
}
return false;
}
/**
* Resize an image so that it fits inside given dimensions
* @param string $filename input file name
* @param string $target_fn output file name
* @param int $max_width width
* @param int $max_height height
*/
public static function resize($filename, $target_fn, $max_width, $max_height)
{
$load = self::loadImage($filename);
if ($load === false) {
return false;
}
list($image, $savetype, $width, $height) = $load;
$new_width = $width;
$new_height = $height;
$width_diff = $max_width / $width;
$height_diff = $max_height / $height;
if ($max_width < $width && $max_height < $height) {
if ($height_diff > $width_diff) {
$new_width = $max_width;
$new_height = $height * $width_diff;
} else {
$new_height = $max_height;
$new_width = $width * $height_diff;
}
} elseif ($max_width < $width) {
$new_width = $max_width;
$new_height = $height * $width_diff;
} elseif ($max_height < $height) {
$new_height = $max_height;
$new_width = $width * $height_diff;
}
$new_image = imagecreatetruecolor($new_width, $new_height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$ret = imagecopyresampled(
$new_image,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height
);
if (!$ret) {
return false;
}
return self::saveImage($new_image, $target_fn, $savetype);
}
/**
* Resize and crop an image to exact dimensions
* @param string $filename input file name
* @param string $target_fn output file name
* @param int $thumb_width width
* @param int $thumb_height height
*/
public static function crop($filename, $target_fn, $thumb_width, $thumb_height)
{
$load = self::loadImage($filename);
if ($load === false) {
return false;
}
list($image, $savetype, $width, $height) = $load;
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ($original_aspect >= $thumb_aspect) {
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
} else {
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
// Resize and crop
$ret = imagecopyresampled(
$thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0,
0,
$new_width,
$new_height,
$width,
$height
);
if (!$ret) {
return false;
}
return self::saveImage($thumb, $target_fn, $savetype);
}
}