-
Notifications
You must be signed in to change notification settings - Fork 1
/
lolcats.inc.php
executable file
·330 lines (249 loc) · 9.54 KB
/
lolcats.inc.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/*
(C)2007 Dustin Spicuzza
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License (GPL)
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
To read the license please visit http://www.gnu.org/copyleft/gpl.html
-----------
This is a (relatively) simple PHP object that generates a lolcat meme. Yes,
there are other sites/programs you can use to generate them, but as far as
I know there is no open source lolcat generator. So now you can incorporate
lolcats into your website without relying on external sites!
Of course, the creative among you will figure out how to integrate this with
other tools and make this script actually pseudo useful...
*/
class Lolcat {
var $img; // Lolcat image
var $imgtype; // type of image
/*
Create()
This creates the LOLCat image. Parameters:
image
filename/url of image. WARNING: the function assumes that the
image parameter has already been validated!! Otherwise, the user
could try and view arbitrary image files on your server..
text
text to be written as a lolcat string to the specified image
align
'tl' top left, 'tr' top right, 'bl' bottom left, 'br' bottom right
If no position given or invalid, it defaults to the top left
font
This is the physical path to the TTF file for the font.
fontsize
(from docs) The font size. Depending on your version of GD, this
should be specified as the pixel size (GD1) or point size (GD2).
Defaults to 15
resize_width (optional)
width to resize the outputted image to. The height is proportional
to this number
padding (optional)
the amount of space around the font
text_color (optional)
an array of three values to be passed to imagecolorallocate (R, G, B).
Defaults to white.
shadow_color (optional)
an array of three values to be passed to imagecolorallocate (R, G, B).
Defaults to black.
*/
function Create($image,$text,$align,$font,$fontsize = 15,$resize_width = 0, $padding = 5, $text_color = -1, $shadow_color = -1){
$this->img = false;
set_error_handler(array($this,"ErrorHandler"));
// second, make sure its an image
if (!(list($width, $height, $this->imgtype, $attr) = @getimagesize($image)))
return $this->ShowError("Error: Cannot get parameters of specified image");
// ensure position is correct
if (!in_array($align,array('tl','tr','bl','br')))
$align = 'tl';
// fix up the font size
$fontsize = str_replace('px','',trim($fontsize));
if ($fontsize == '' || !is_int($fontsize))
$fontsize = 15;
// verify the font
if (!($font = realpath($font)))
return $this->ShowError("Error: Invalid font specified!");
switch ($this->imgtype){
case 1: // gif
$this->img = @imagecreatefromgif($image);
break;
case 2: // jpg
$this->img = @imagecreatefromjpeg($image);
break;
case 3: // png
$this->img = @imagecreatefrompng($image);
break;
default: // i don't care about other formats
return false;
}
if (!$this->img)
return $this->ShowError("Error: Cannot use specified image");
// see if we need to resize the image first
if ($resize_width != 0){
// todo: Add a 'max-width, max-height' parameter...
$ratio = $width / $resize_width;
$resize_height = intval($height / $ratio);
$thumb_img = imagecreatetruecolor($resize_width, $resize_height);
if (!imagecopyresampled($thumb_img, $this->img, 0, 0, 0, 0, $resize_width, $resize_height, $width, $height))
return $this->ShowError("Error: Could not resize image!");
// fix these
$height = $resize_height;
$width = $resize_width;
// destroy original object
imagedestroy($this->img);
$this->img = $thumb_img;
}
// allocate colors for text
if ($text_color === -1)
$text_color = imagecolorallocate($this->img, 255, 255, 255);
else
$text_color = imagecolorallocate($this->img, $text_color[0], $text_color[1], $text_color[2]);
// allocate colors for text shadow
if ($shadow_color === -1)
$shadow_color = imagecolorallocate($this->img, 0, 0, 0);
else
$shadow_color = imagecolorallocate($this->img, $shadow_color[0], $shadow_color[1], $shadow_color[2]);
// next, we should try to create the text.. hopefully it wraps nicely
putenv('GDFONTPATH=' . realpath('.')); // hack, just in case
// grab the font height, M is supposed to be big, with any random chars lying around
$font_height = $this->img_height(imagettfbbox($fontsize,0,$font,'Mjg'));
$row_spacing = intval($font_height * .2); // purely arbitrary value
$space_width = $this->img_width(imagettfbbox($fontsize,0,$font,' '));
// try and do our best imitation of wordwrapping
$text_elements = explode(' ',str_replace("\n",'',str_replace("\r\n",'',$text)));
// adjust this depending on alignment
$top = $padding + $font_height;
$left = $padding;
// initialize
$line_width = 0;
$line_beginning = 0; // index of beginning of line
$inc = 1;
$c = count($text_elements);
$i = 0;
if ($align[0] == 'b'){
$top = $height - $padding;
$font_height = $font_height * -1;
$row_spacing = $row_spacing * -1;
$inc = -1;
$i = $c -1;
}
$dbg = get_get_var('dbg');
$line_beginning = $i;
// draw text elements starting from alignment position..
for (;$i >= 0 && $i < $c;$i += $inc){
$lf_width = $this->img_width(imagettfbbox($fontsize,0,$font,$text_elements[$i]));
// add a space
if ($i != $line_beginning)
$lf_width += $space_width;
// see if we've exceeded the max width
if ($lf_width + $line_width + $padding * 2 > $width){
// draw it out then!
if ($align[1] == 'r')
$left = $width - $padding - $line_width;
if ($align[0] == 'b')
$text = implode(' ',array_slice($text_elements,$i+1,$line_beginning-$i));
else
$text = implode(' ',array_slice($text_elements,$line_beginning,$i - $line_beginning));
// draw the text
imagettftext($this->img,$fontsize,0,$left-1,$top+1,$shadow_color,$font,$text);
imagettftext($this->img,$fontsize,0,$left,$top,$text_color,$font,$text);
// keep moving, reset params
$top += $font_height + $row_spacing;
$line_beginning = $i;
$line_width = $lf_width;
}else{
// keep trucking
$line_width += $lf_width;
}
}
// get the last line too
if ($line_width != 0){
if ($align[1] == 'r')
$left = $width - $padding - $line_width;
if ($align[0] == 'b')
$text = implode(' ',array_slice($text_elements,$i+1,$line_beginning-$i));
else
$text = implode(' ',array_slice($text_elements,$line_beginning,$i - $line_beginning));
imagettftext($this->img,$fontsize,0,$left-1,$top+1,$shadow_color,$font,$text);
imagettftext($this->img,$fontsize,0,$left,$top,$text_color,$font,$text);
}
imagecolordeallocate($this->img,$shadow_color);
imagecolordeallocate($this->img,$text_color);
restore_error_handler();
return true;
}
function Show($destroy = true){
// if the image is still valid, then we should output it at the end
if (!$this->img)
return false;
switch ($this->imgtype){
case 1: // gif
header("Content-Type: image/gif");
$this->img = imagegif($this->img);
break;
case 2: // jpg
header("Content-Type: image/jpeg");
$this->img = imagejpeg($this->img);
break;
case 3: // png
header("Content-Type: image/png");
$this->img = imagepng($this->img);
break;
default: // i don't care about other formats
return false;
}
if ($destroy)
imagedestroy($this->img);
return true;
}
function WriteToFile($file, $destroy = true){
// if the image is still valid, then we should output it at the end
if (!$this->img)
return false;
switch ($this->imgtype){
case 1: // gif
return @imagegif($this->img,$file);
case 2: // jpg
return @imagejpeg($this->img,$file);
case 3: // png
return @imagepng($this->img,$file);
}
if ($destroy)
imagedestroy($this->img);
return false;
}
// utility functions
function img_width($sz_array){
return abs(max($sz_array[0] - $sz_array[2], $sz_array[4] - $sz_array[6]));
}
function img_height($sz_array){
return abs(max($sz_array[7] - $sz_array[1], $sz_array[5] - $sz_array[3]));
}
// shows an error... always returns false
function ShowError($text){
if (headers_sent())
return false;
$img = imagecreate(400,50);
imagefill($img,0,0,imagecolorallocate($img,0,0,0));
$top = 10;
foreach (explode("\n",$text) as $t){
imagestring($img,2,10,$top,$t,imagecolorallocate($img,255,255,255));
$top+=15;
}
header("Content-Type: image/gif");
imagegif($img);
imagedestroy($img);
return false;
}
// used to catch any errors that may occur.. shows them in image format,
// so the user can actually see them!
function ErrorHandler($errno,$errstr, $errfile = "", $errline = 0, $errcontext = null){
$this->ShowError("Error $errno: $errstr" . ($errfile != "" ? "\n" . basename($errfile) . " at line $errline" : ""));
die();
}
}
?>