-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
58 lines (41 loc) · 1.36 KB
/
index.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
<?php
//putenv('GDFONTPATH=' . realpath('.'));
header('Content-Type: image/png');
$image = imagecreatetruecolor(500, 500);
$white = imagecolorallocate($image, 255, 255, 255);
$src = imagecreatefrompng('forzly_bg.png');
imagecopymerge($image, $src, 0, 0, 0, 0, 500, 500, 100);
$font = './Ubuntu.ttf';
$fontSize = 20;
$inputText = (string) isset($_GET['t']) ? $_GET['t'] : 'Lorem ipsum';
$textArray = explode(' ', $inputText);
$bbox = imagettfbbox($fontSize, 0, $font, $inputText);
$startPosition = 0;
$offset = 1;
$lines = [];
for ($i = 0; $i < count($textArray); $i++)
{
$line = implode(' ', array_slice($textArray, $startPosition, $offset));
$bbox = imagettfbbox($fontSize, 0, $font, $line);
$lineWidth = ($bbox[0] + $bbox[4]);
if ($lineWidth >= 320 || $i == count($textArray) - 1)
{
$lines[] = $line;
$startPosition = $i + count($lines);
$offset = 1;
}
$offset++;
}
$heightModifier = 0;
$lineHeight = 10;
for ($i = 0; $i < count($lines); $i++)
{
$bbox = imagettfbbox($fontSize, 0, $font, $lines[$i]);
$lineWidth = ($bbox[0] + $bbox[4]);
$textHeight = count($lines) * $lineHeight;
$x = (imagesx($image) / 2) - $lineWidth / 2;
$y = (imagesy($image) / 2) - ($textHeight / 2) + 30 * $i;
imagettftext($image, $fontSize, 0, $x, $y, $white, $font, $lines[$i]);
}
imagepng($image);
imagedestroy($image);