-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImagePlaceholder.php
171 lines (149 loc) · 4.79 KB
/
ImagePlaceholder.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
<?php
class ImagePlaceholder
{
/** @param string */
protected $text;
protected $fontFamily;
/** @param int */
protected $fontSize;
/**
* Construct Image class
*
* @param int $fontSize
* @param string $text
*/
public function __construct(int $fontSize = null, string $fontFamily = null)
{
$this->fontSize = $fontSize;
$this->fontFamily = isset($fontFamily) ? $fontFamily : './Roboto.ttf';
}
/**
* Render an image with given width, height and colours.
*
* @param int $width - set image width in pixels
* @param int $height - set image height in pixels
* @param string $bgcolor - set background color, accepted is hex string
* @param string $color - set text color, accepted is hex string
* @return resource
*/
public function create (
$width,
$height,
string $bgcolor = '#fafafa',
string $color = '#cdcdcd',
string $text = null
) {
$image = imagecreatetruecolor($width, $height);
$image = $this->paintImage($image, $bgcolor);
$color = $this->painImageText($image, $color);
// Check whether there is globally set font size or not.
$fontSize = isset($this->fontSize) ? $this->fontSize : $this->setFontSize($width, $height);
$image = $this->typeOnImage($image, $fontSize, $color, $text);
return $image;
}
/**
* Write size or given text on image.
*
* @param resource $image - gd resource.
* @param int $fontSize - size of the text.
* @param int $color - allocated colour with imagecolorallocate.
* @param string $text - text that should be writteon image.
* @return resource
*/
public function typeOnImage($image, int $fontSize, int $color, string $text = null)
{
$width = imagesx($image);
$height = imagesy($image);
$text = isset($text) ? $text : "$width x $height";
$position = $this->getTextPosition($image, $fontSize, $text);
imagettftext(
$image,
$fontSize,
0,
$position['x'],
$position['y'],
$color,
$this->fontFamily,
$text
);
return $image;
}
public function getTextPosition($image, int $fontSize, string $text)
{
$textBox = imagettfbbox($fontSize, 0, $this->fontFamily, $text);
$textWidth = abs($textBox[2]) - abs($textBox[0]);
$textHeight = abs($textBox[5]) - abs($textBox[3]);
$imageWidth = imagesx($image);
$imageHeight = imagesy($image);
return [
'x' => ($imageWidth - $textWidth) / 2,
'y' => ($imageHeight + $textHeight) /2 ,
];
}
/**
* Set text colour.
*
* @param resource $image - gd resource.
* @param string $color - hex encoded colour.
* @return int
*/
public function painImageText($image, string $color): int
{
$textColor = $this->hexToRgb($color);
$textColor = imagecolorallocate(
$image,
$textColor['red'],
$textColor['green'],
$textColor['blue']
);
return $textColor;
}
/**
* Fill image with given colour.
*
* @param resource $image
* @param string $color
* @return resource
*/
public function paintImage($image, string $color)
{
$backgroundColor = $this->hexToRgb($color);
$backgroundColor = imagecolorallocate(
$image,
$backgroundColor['red'],
$backgroundColor['green'],
$backgroundColor['blue']
);
imagefill($image, 0, 0, $backgroundColor);
return $image;
}
/**
* Convert HEX string into RGB array.
*
* @param string $hexColor
* @return array
*/
public function hexToRgb(string $hexColor): array
{
$hexColor = str_replace('#', '', $hexColor);
$hexColor = strlen($hexColor) <= 3
? $hexColor[0].$hexColor[0].$hexColor[1].$hexColor[1].$hexColor[2].$hexColor[2]
: $hexColor;
return [
'red' => hexdec(substr($hexColor, 0, 2)),
'green' => hexdec(substr($hexColor, 2, 2)),
'blue' => hexdec(substr($hexColor, 4, 2))
];
}
/**
* Set font size based on image width and height.
*
* @param int $width
* @param int $height
* @return int
*/
public function setFontSize(int $width, int $height): int
{
return $width > $height ? $height / 10 : $width / 10;
}
}