-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextImage.php
More file actions
131 lines (110 loc) · 3.69 KB
/
TextImage.php
File metadata and controls
131 lines (110 loc) · 3.69 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
<?php // vim:ts=3:sts=3:sw=3:et:
/**
* Renders an image containing text
*
* PHP Version 5
*
* @category PHP
* @package MindFrame2
* @author Bryan C. Geraghty <bryan@ravensight.org>
* @copyright 2005-2011 Bryan C. Geraghty
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL
* @link https://github.com/archwisp/MindFrame2
*/
/**
* Renders an image containing text
*
* @category PHP
* @package MindFrame2
* @author Bryan C. Geraghty <bryan@ravensight.org>
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL
* @link https://github.com/archwisp/MindFrame2
* @todo Enable customization of text size
*/
class MindFrame2_TextImage
{
private $_char_height = 14;
private $_char_width = 8;
private $_file_name;
private $_height;
private $_padding = 2;
private $_width;
public function __construct($width, $height, $file_name)
{
$this->_file_name = $file_name;
$this->_height = $height;
$this->_width = $width;
}
/**
* @todo Enable color customization
* @todo Remove magic numbers
*/
public function build($text)
{
$image = imagecreate($this->_width, $this->_height);
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
$column_count = $this->_calculateColumnCount();
$line_count = $this->_calculateLineCount();
// If there is more than one line worth of text to render, we need to
// break it into chunks that will fit into the column count for each
// line. We'll then count the number of chunks we have and subtract half
// of that number times the height of a character (to get pixels) from
// the normal Y position (middle). We'll then write each line and
// increment the Y position by the height of a character each time so
// each line is written on a new line.
if (strlen($text) >= $column_count)
{
$chunks = str_split($text, $column_count);
$y_position = $this->_calculateYPosition();
$y_position -= round(count($chunks) / 2) * $this->_char_height;
foreach ($chunks as $chunk)
{
$x_position = $this->_calculateXPosition($column_count, $chunk);
imagestring($image, 4, $x_position, $y_position, $chunk, $black);
$y_position += $this->_char_height;
}
// end foreach // ($chunks as $chunk) //
}
else
{
$x_position = $this->_calculateXPosition($column_count, $text);
$y_position = $this->_calculateYPosition();
imagestring($image, 4, $x_position, $y_position, $text, $black);
}
imagepng($image, $this->_file_name);
imagecolordeallocate($image, $black);
imagecolordeallocate($image, $white);
imagedestroy($image);
}
private function _calculateColumnCount()
{
return round($this->_width/$this->_char_width, 0) -
($this->_padding * 2) - 1;
}
private function _calculateLineCount()
{
return round($this->_height/$this->_char_height, 0) -
($this->_padding * 2) - 1;
}
private function _calculateXPosition($column_count, $text)
{
$position = NULL;
if (strlen($text) >= $column_count)
{
$position = ($this->_padding * $this->_char_width);
}
else
{
$middle_pixel = round(($this->_width / 2), 2);
$text_middle_char = round((strlen($text) / 2), 2);
$text_middle_pixel = $text_middle_char * $this->_char_width;
$position = $middle_pixel - $text_middle_pixel;
}
return $position;
}
private function _calculateYPosition()
{
return round($this->_height / 2, 0);
}
}