BCGFontPhp.php
3.5 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
/**
*--------------------------------------------------------------------
*
* Holds font for PHP.
*
*--------------------------------------------------------------------
* Copyright (C) Jean-Sebastien Goupil
* http://www.barcodephp.com
*/
include_once('BCGFont.php');
include_once('BCGColor.php');
class BCGFontPhp implements BCGFont {
private $font;
private $text;
private $rotationAngle;
private $backgroundColor;
/**
* Constructor.
*
* @param int $font
*/
public function __construct($font) {
$this->font = max(0, intval($font));
$this->setRotationAngle(0);
$this->setBackgroundColor(new BCGColor('white'));
}
/**
* Gets the text associated to the font.
*
* @return string
*/
public function getText() {
return $this->text;
}
/**
* Sets the text associated to the font.
*
* @param string text
*/
public function setText($text) {
$this->text = $text;
}
/**
* Gets the rotation in degree.
*
* @return int
*/
public function getRotationAngle() {
return $this->rotationAngle;
}
/**
* Sets the rotation in degree.
*
* @param int
*/
public function setRotationAngle($rotationAngle) {
$this->rotationAngle = (int)$rotationAngle;
if ($this->rotationAngle !== 90 && $this->rotationAngle !== 180 && $this->rotationAngle !== 270) {
$this->rotationAngle = 0;
}
}
/**
* Gets the background color.
*
* @return BCGColor
*/
public function getBackgroundColor() {
return $this->backgroundColor;
}
/**
* Sets the background color.
*
* @param BCGColor $backgroundColor
*/
public function setBackgroundColor($backgroundColor) {
$this->backgroundColor = $backgroundColor;
}
/**
* Returns the width and height that the text takes to be written.
*
* @return int[]
*/
public function getDimension() {
$w = imagefontwidth($this->font) * strlen($this->text);
$h = imagefontheight($this->font);
if ($this->rotationAngle === 90 || $this->rotationAngle === 270) {
return array($h, $w);
} else {
return array($w, $h);
}
}
/**
* Draws the text on the image at a specific position.
* $x and $y represent the left bottom corner.
*
* @param resource $im
* @param int $color
* @param int $x
* @param int $y
*/
public function draw($im, $color, $x, $y) {
if ($this->rotationAngle !== 0) {
if (!function_exists('imagerotate')) {
throw new BCGDrawException('The method imagerotate doesn\'t exist on your server. Do not use any rotation.');
}
$w = imagefontwidth($this->font) * strlen($this->text);
$h = imagefontheight($this->font);
$gd = imagecreatetruecolor($w, $h);
imagefilledrectangle($gd, 0, 0, $w - 1, $h - 1, $this->backgroundColor->allocate($gd));
imagestring($gd, $this->font, 0, 0, $this->text, $color);
$gd = imagerotate($gd, $this->rotationAngle, 0);
imagecopy($im, $gd, $x, $y, 0, 0, imagesx($gd), imagesy($gd));
} else {
imagestring($im, $this->font, $x, $y, $this->text, $color);
}
}
}
?>