-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.PhrasePNGBuilder.php
109 lines (95 loc) · 3.58 KB
/
class.PhrasePNGBuilder.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
<?php
require_once('class.Phrase.php');
class PhrasePNGBuilder
{
private $textFont;
private $authorFont;
private $textSize;
private $authorSize;
private $text;
private $author;
private $spaceBelowText;
private $offset;
function __construct($textFont, $authorFont, $textSize = 12, $authorSize = 12, $backgroundColor = '#fff', $borderColor = '#000', $textColor = '#000', $authorColor = '#000')
{
$this->textFont = $textFont;
$this->authorFont = $authorFont;
$this->textSize = $textSize;
$this->authorSize = $authorSize;
$this->backgroundColor = $backgroundColor;
$this->borderColor = $borderColor;
$this->textColor = $textColor;
$this->authorColor = $authorColor;
$this->spaceBelowText = 5;
$this->borderSize = 3;
$this->padding = 5;
$this->text = '';
$this->author = '';
}
private function colorFromHex($im, $hex)
{
$hex = str_replace('#', '', $hex);
switch (strlen($hex)) {
case 6:
$red = substr($hex, 0, 2);
$green = substr($hex, 2, 2);
$blue = substr($hex, 4, 2);
break;
case 3:
$red = str_repeat(substr($hex, 0, 1), 2);
$green = str_repeat(substr($hex, 1, 1), 2);
$blue = str_repeat(substr($hex, 2, 1), 2);
break;
default:
$red = '0';
$green = '0';
$blue = '0';
break;
}
return imagecolorallocate($im, hexdec($red), hexdec($green), hexdec($blue));
}
function phrase($phrase, $charactersPerLine = 0)
{
if ($charactersPerLine <= 0) $this->text = $phrase->phrase();
else $this->text = wordwrap($phrase->phrase(), $charactersPerLine);
$this->author = '-' . $phrase->author();
}
function build()
{
// Calculate text boundaries and position.
$box = imagettfbbox($this->textSize, 0, $this->textFont, $this->text);
$textWidth = $box[2] - $box[6];
$textHeight = $box[3] - $box[7];
$offset = $this->padding + $this->borderSize;
$textX = -$box[6] + $offset;
$textY = -$box[7] + $offset;
// Calculate author boundaries and position.
$box = imagettfbbox($this->authorSize, 0, $this->authorFont, $this->author);
$authorWidth = $box[2] - $box[6];
$authorHeight = $box[3] - $box[7];
$authorX = -$box[6] + $offset;
$authorY = -$box[7] + $textHeight + $this->spaceBelowText + $offset;
// Calculate image size
$imageWidth = max($textWidth, $authorWidth) + $offset * 2;
$imageHeight = $textHeight + $this->spaceBelowText + $authorHeight + $offset * 2;
$backgroundWidth = $imageWidth - 2 * ($this->borderSize - 1);
$backgroundHeight = $imageHeight - 2 * ($this->borderSize - 1);
// Signature is right aligned, so we have to recalculate it.
// Offset is substracted twice because it was added before, and we want right alignment.
$authorX = $imageWidth - $authorWidth + $authorX - 2 * $offset;
// Create a black image.
$im = imagecreatetruecolor($imageWidth, $imageHeight);
// Create the colours to be used.
$backgroundColor = $this->colorFromHex($im, $this->backgroundColor);
$borderColor = $this->colorFromHex($im, $this->borderColor);
$textColor = $this->colorFromHex($im, $this->textColor);
$authorColor = $this->colorFromHex($im, $this->authorColor);
// Draw the border.
imagefilledrectangle($im, 0, 0, $imageWidth, $imageHeight, $borderColor);
imagefilledrectangle($im, $this->borderSize, $this->borderSize, $backgroundWidth, $backgroundHeight, $backgroundColor);
// Put the text and author. Coordinates are the lower left pixel of the first character.
imagettftext($im, $this->textSize, 0, $textX, $textY, $textColor, $this->textFont, $this->text);
imagettftext($im, $this->authorSize, 0, $authorX, $authorY, $authorColor, $this->authorFont, $this->author);
return $im;
}
}