-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathimage_resized.php
145 lines (112 loc) · 3.69 KB
/
image_resized.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
<?php
/**
* phpwcms content management system
*
* @author Oliver Georgi <[email protected]>
* @copyright Copyright (c) 2002-2024, 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($_GET['imgfile']) : 'img/leer.gif';
$img_width = (isset($_GET['w'])) ? intval($_GET['w']) : 0;
$img_height = (isset($_GET['h'])) ? intval($_GET['h']) : 0;
$img_quality= (isset($_GET['q']) && intval($_GET['q']) <= 100 && intval($_GET['q'])) ? intval($_GET['q']) : 75;
$img_file = str_replace(array('http://', 'https://'), '', $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(is_file($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;
}
imagecopyresized($new_img, $img_source, 0, 0, 0, 0, $img_width, $img_height, $img_info[0], $img_info[1]);
header('Content-type: '.$img_mimetype);
switch($img_target) {
case 'jpg':
imagejpeg($new_img, NULL, $img_quality);
break;
case 'webp':
imagewebp($new_img, NULL, $img_quality);
break;
case 'png':
imagepng($new_img, NULL, 9);
break;
case 'gif':
imagegif($new_img);
break;
}
imagedestroy($new_img);
imagedestroy($img_source);
} else {
// error / no image
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);
}