-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathimage_resized.php
More file actions
163 lines (140 loc) · 4.35 KB
/
image_resized.php
File metadata and controls
163 lines (140 loc) · 4.35 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
<?php
/**
* phpwcms content management system
*
* @author Oliver Georgi <og@phpwcms.org>
* @copyright Copyright (c) 2002-2026, Oliver Georgi
* @license http://opensource.org/licenses/GPL-2.0 GNU GPL-2
* @link http://www.phpwcms.org
*
**/
// <img src="image_resized.php?format=jpg&w=100&h=200&q=85&imgfile=test.jpg" alt="" border="0">
$img_target = isset($_GET['format']) ? strtolower(trim($_GET['format'])) : 'jpg';
$img_file = isset($_GET['imgfile']) ? trim(urldecode($_GET['imgfile'])) : 'img/leer.gif';
$img_width = isset($_GET['w']) ? (int)$_GET['w'] : 0;
$img_height = isset($_GET['h']) ? (int)$_GET['h'] : 0;
$img_quality = isset($_GET['q']) && (int)$_GET['q'] <= 100 && (int)$_GET['q'] ? (int)$_GET['q'] : 75;
$result = false;
// Ensure no protocol handlers (http://…) or something like C:\ or C:/ or ./ or ../ is part of the file name
if (
$img_file
&&
(
$img_file[0] === '.'
||
$img_file[0] === '/'
||
$img_file[0] === '\\'
||
strpos($img_file, ':/') !== false
||
strpos($img_file, './') !== false
||
strpos($img_file, ':\\') !== false
||
strpos($img_file, '.\\') !== false
)
) {
$img_file = '';
} else {
// Absolute path only related to the current directory
$img_file = __DIR__ . '/' . $img_file;
}
switch ($img_target) {
case 'png':
$img_mimetype = 'image/png';
$img_target = 'jpg';
break;
case 'gif':
if (function_exists('imagegif')) {
$img_mimetype = 'image/gif';
$img_target = 'gif';
} else {
$img_target = 'png';
$img_mimetype = 'image/png';
}
break;
case 'webp':
$img_mimetype = 'image/webp';
$img_target = 'webp';
break;
case 'jpeg':
case 'jpg':
default:
$img_mimetype = 'image/jpeg';
$img_target = 'jpg';
}
if ($img_file !== '' && is_readable($img_file) && $img_info = getimagesize($img_file)) {
if (!$img_width || $img_width >= $img_info[0]) {
$percent_width = 1;
} else {
$percent_width = $img_width / $img_info[0];
}
if (!$img_height || $img_height >= $img_info[1]) {
$percent_height = 1;
} else {
$percent_height = $img_height / $img_info[1];
}
if ($percent_height < $percent_width) {
$percent = $percent_height;
} elseif ($percent_height > $percent_width) {
$percent = $percent_width;
} else {
$percent = $percent_width;
}
$img_width = $img_info[0] * $percent;
$img_height = $img_info[1] * $percent;
switch ($img_target) {
case 'jpg':
case 'png':
case 'webp':
$new_img = imagecreatetruecolor($img_width, $img_height);
break;
case 'gif':
$new_img = imagecreate($img_width, $img_height);
break;
}
switch ($img_info[2]) {
case IMAGETYPE_GIF: // GIF
$img_source = imagecreatefromgif($img_file);
break;
case IMAGETYPE_JPEG: // JPG
$img_source = imagecreatefromjpeg($img_file);
break;
case IMAGETYPE_PNG: // PNG
$img_source = imagecreatefrompng($img_file);
break;
case IMAGETYPE_WEBP: // WEBP
$img_source = imagecreatefromwebp($img_file);
break;
}
$result = imagecopyresized($new_img, $img_source, 0, 0, 0, 0, $img_width, $img_height, $img_info[0], $img_info[1]);
if ($result) {
header('Content-type: ' . $img_mimetype);
switch ($img_target) {
case 'jpg':
$result = imagejpeg($new_img, NULL, $img_quality);
break;
case 'webp':
$result = imagewebp($new_img, NULL, $img_quality);
break;
case 'png':
$result = imagepng($new_img, NULL, 9);
break;
case 'gif':
$result = imagegif($new_img);
break;
}
}
imagedestroy($new_img);
imagedestroy($img_source);
}
// Error / no image
if (!$result) {
header('Content-type: image/png');
$new_img = imagecreatetruecolor(75, 20);
$text_color = imagecolorallocate($new_img, 255, 255, 255);
imagestring($new_img, 1, 5, 5, 'Image Error', $text_color);
imagepng($new_img, NULL, 9);
imagedestroy($new_img);
}