-
Notifications
You must be signed in to change notification settings - Fork 25
/
image.php
155 lines (145 loc) · 4.32 KB
/
image.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
<?php
/**
* Dynamic Dummy Image Generator — as seen on DummyImage.com
*
* This script enables you to create placeholder images in a breeze.
* Please refer to the README on how to use it.
*
* (Original idea by Russel Heimlich. When I first published this script,
* DummyImage.com was not Open Source, so I had to write a small script to
* replace the function on my own server.)
*
* @author Fabian Beiner <fb@fabianbeiner.de>
* @license MIT
* @link https://github.com/FabianBeiner/PHP-Dummy-Image-Generator/
* @version 0.3.0 <2017-12-26>
*/
/**
* Handle the “size” parameter.
*/
$size = '640x480';
if (isset($_GET['size'])) {
$size = $_GET['size'];
}
list($imgWidth, $imgHeight) = explode('x', $size . 'x');
if ($imgHeight === '') {
$imgHeight = $imgWidth;
}
$filterOptions = [
'options' => [
'min_range' => 0,
'max_range' => 9999
]
];
if (filter_var($imgWidth, FILTER_VALIDATE_INT, $filterOptions) === false) {
$imgWidth = '640';
}
if (filter_var($imgHeight, FILTER_VALIDATE_INT, $filterOptions) === false) {
$imgHeight = '480';
}
/**
* Handle the “type” parameter.
*/
$type = 'png';
if (isset($_GET['type']) && in_array(strtolower($_GET['type']), ['png', 'gif', 'jpg', 'jpeg'])) {
$type = strtolower($_GET['type']);
}
/**
* Handle the “text” parameter.
*/
$text = $imgWidth . '×' . $imgHeight;
if (isset($_GET['text']) && strlen($_GET['text'])) {
$text = filter_var(trim($_GET['text']), FILTER_SANITIZE_STRING);
}
$encoding = mb_detect_encoding($text, 'UTF-8, ISO-8859-1');
if ($encoding !== 'UTF-8') {
$text = mb_convert_encoding($text, 'UTF-8', $encoding);
}
$text = mb_encode_numericentity($text,
[0x0, 0xffff, 0, 0xffff],
'UTF-8');
/**
* Handle the “bg” parameter.
*/
$bg = 'CC0099';
if (isset($_GET['bg']) && (strlen($_GET['bg']) === 6 || strlen($_GET['bg']) === 3)) {
$bg = strtoupper($_GET['bg']);
if (strlen($_GET['bg']) === 3) {
$bg =
strtoupper($_GET['bg'][0] .
$_GET['bg'][0] .
$_GET['bg'][1] .
$_GET['bg'][1] .
$_GET['bg'][2] .
$_GET['bg'][2]);
}
}
list($bgRed, $bgGreen, $bgBlue) = sscanf($bg, "%02x%02x%02x");
/**
* Handle the “color” parameter.
*/
$color = 'FFFFFF';
if (isset($_GET['color']) && (strlen($_GET['color']) === 6 || strlen($_GET['color']) === 3)) {
$color = strtoupper($_GET['color']);
if (strlen($_GET['color']) === 3) {
$color =
strtoupper($_GET['color'][0] .
$_GET['color'][0] .
$_GET['color'][1] .
$_GET['color'][1] .
$_GET['color'][2] .
$_GET['color'][2]);
}
}
list($colorRed, $colorGreen, $colorBlue) = sscanf($color, "%02x%02x%02x");
/**
* Define the typeface settings.
*/
$fontFile = realpath(__DIR__) . DIRECTORY_SEPARATOR . 'RobotoMono-Regular.ttf';
if ( ! is_readable($fontFile)) {
$fontFile = 'arial';
}
$fontSize = round(($imgWidth - 50) / 8);
if ($fontSize <= 9) {
$fontSize = 9;
}
/**
* Generate the image.
*/
$image = imagecreatetruecolor($imgWidth, $imgHeight);
$colorFill = imagecolorallocate($image, $colorRed, $colorGreen, $colorBlue);
$bgFill = imagecolorallocate($image, $bgRed, $bgGreen, $bgBlue);
imagefill($image, 0, 0, $bgFill);
$textBox = imagettfbbox($fontSize, 0, $fontFile, $text);
while ($textBox[4] >= $imgWidth) {
$fontSize -= round($fontSize / 2);
$textBox = imagettfbbox($fontSize, 0, $fontFile, $text);
if ($fontSize <= 9) {
$fontSize = 9;
break;
}
}
$textWidth = abs($textBox[4] - $textBox[0]);
$textHeight = abs($textBox[5] - $textBox[1]);
$textX = ($imgWidth - $textWidth) / 2;
$textY = ($imgHeight + $textHeight) / 2;
imagettftext($image, $fontSize, 0, $textX, $textY, $colorFill, $fontFile, $text);
/**
* Return the image and destroy it afterwards.
*/
switch ($type) {
case 'png':
header('Content-Type: image/png');
imagepng($image, null, 9);
break;
case 'gif':
header('Content-Type: image/gif');
imagegif($image);
break;
case 'jpg':
case 'jpeg':
header('Content-Type: image/jpeg');
imagejpeg($image);
break;
}
imagedestroy($image);