text2png_class.php
2.22 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
<?
require 'library.php';
class textPNG
{
var $font = 'fonts/times.ttf'; //default font. directory relative to script directory.
var $msg = "undefineds"; // default text to display.
var $size = 10;
var $rot = 0; // rotation in degrees.
var $pad = 0; // padding.
var $transparent = 1; // transparency set to on.
var $red = 0; // white text...
var $grn = 0;
var $blu = 0;
var $bg_red = 255; // on black background.
var $bg_grn = 255;
var $bg_blu = 255;
function draw()
{
$width = 0;
$height = 0;
$offset_x = 0;
$offset_y = 0;
$bounds = array();
$image = "";
// determine font height.
$bounds = ImageTTFBBox($this->size, $this->rot, $this->font, "W");
if ($this->rot < 0)
{
$font_height = abs($bounds[7]-$bounds[1]);
}
else if ($this->rot > 0)
{
$font_height = abs($bounds[1]-$bounds[7]);
}
else
{
$font_height = abs($bounds[7]-$bounds[1]);
}
// determine bounding box.
$bounds = ImageTTFBBox($this->size, $this->rot, $this->font, $this->msg);
if ($this->rot < 0)
{
$width = abs($bounds[4]-$bounds[0]);
$height = abs($bounds[3]-$bounds[7]);
$offset_y = $font_height;
$offset_x = 0;
}
else if ($this->rot > 0)
{
$width = abs($bounds[2]-$bounds[6]);
$height = abs($bounds[1]-$bounds[5]);
$offset_y = abs($bounds[7]-$bounds[5])+$font_height;
$offset_x = abs($bounds[0]-$bounds[6]);
}
else
{
$width = abs($bounds[4]-$bounds[6]);
$height = abs($bounds[7]-$bounds[1]);
$offset_y = $font_height;;
$offset_x = 0;
}
$image = ImageCreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);
$background = ImageColorAllocate($this->$image, $this->bg_red, $this->bg_grn, $this->bg_blu);
$foreground = ImageColorAllocate($this->$image, $this->red, $this->grn, $this->blu);
if ($this->transparent) ImageColorTransparent($this->$image, $background);
ImageInterlace($this->$image, false);
// render it.
ImageTTFText($this->$image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg);
// output PNG object.
ImagePNG($this->$image);
ImageDestroy($this->$image);
}
}
?>