[PHP] Afbeelding generator, afbeelding invoegen
Hoi,
Ik heb een script om afbeeldingen te genereren:
Er wordt een achtergrond ingeladen, maar nu wil ik ook graag een statische Google Maps-afbeelding (png). Inladen (bijvoorbeeld staticmap.png op de server). Nu is mijn vraag, hoe moet ik dat doen via write_multiline_text (de functie die hierboven is gemaakt)?
Ik heb al gegoogled, maar ik kom er niet uit. Kan iemand mij helpen misschien?
Ik heb een script om afbeeldingen te genereren:
Code (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
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
<?php
function write_multiline_text($image, $font_size, $color, $font, $text, $start_x, $start_y, $max_width) {
//split the string
//build new string word for word
//check everytime you add a word if string still fits
//otherwise, remove last word, post current string and start fresh on a new line
$words = explode(" ", $text);
$string = "";
$tmp_string = "";
for($i = 0; $i < count($words); $i++) {
$tmp_string .= $words[$i]." ";
//check size of string
$dim = imagettfbbox($font_size, 0, $font, $tmp_string);
if($dim[4] < ($max_width - $start_x)) {
$string = $tmp_string;
$curr_width = $dim[4];
} else {
$i--;
$tmp_string = "";
$start_xx = $start_x + round(($max_width - $curr_width - $start_x) / 2);
imagettftext($image, $font_size, 0, $start_xx, $start_y, $color, $font, $string);
$string = "";
$start_y += abs($dim[5]) * 2;
$curr_width = 0;
}
}
$start_xx = $start_x + round(($max_width - $dim[4] - $start_x) / 2);
imagettftext($image, $font_size, 0, $start_xx, $start_y, $color, $font, $string);
}
// Create a 300x300 image
$im = imagecreatetruecolor(300, 300);
$im = imagecreatefrompng('background.png');
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
// Set the background to be white
imagefilledrectangle($im, 1, 1, 298, 298, $white);
// Path to our font file
$font = 'c:/windows/fonts/arial.ttf';
$text = "This is a very ";
$text .= "long long long long long long long long long long long long long long long long ";
$text .= "long long long long long long long long long long long long long long long long ";
$text .= "line of text";
write_multiline_text($im, 12, $black, $font, $text, 10, 22, 298);
// Output to browser
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
function write_multiline_text($image, $font_size, $color, $font, $text, $start_x, $start_y, $max_width) {
//split the string
//build new string word for word
//check everytime you add a word if string still fits
//otherwise, remove last word, post current string and start fresh on a new line
$words = explode(" ", $text);
$string = "";
$tmp_string = "";
for($i = 0; $i < count($words); $i++) {
$tmp_string .= $words[$i]." ";
//check size of string
$dim = imagettfbbox($font_size, 0, $font, $tmp_string);
if($dim[4] < ($max_width - $start_x)) {
$string = $tmp_string;
$curr_width = $dim[4];
} else {
$i--;
$tmp_string = "";
$start_xx = $start_x + round(($max_width - $curr_width - $start_x) / 2);
imagettftext($image, $font_size, 0, $start_xx, $start_y, $color, $font, $string);
$string = "";
$start_y += abs($dim[5]) * 2;
$curr_width = 0;
}
}
$start_xx = $start_x + round(($max_width - $dim[4] - $start_x) / 2);
imagettftext($image, $font_size, 0, $start_xx, $start_y, $color, $font, $string);
}
// Create a 300x300 image
$im = imagecreatetruecolor(300, 300);
$im = imagecreatefrompng('background.png');
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
// Set the background to be white
imagefilledrectangle($im, 1, 1, 298, 298, $white);
// Path to our font file
$font = 'c:/windows/fonts/arial.ttf';
$text = "This is a very ";
$text .= "long long long long long long long long long long long long long long long long ";
$text .= "long long long long long long long long long long long long long long long long ";
$text .= "line of text";
write_multiline_text($im, 12, $black, $font, $text, 10, 22, 298);
// Output to browser
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
Er wordt een achtergrond ingeladen, maar nu wil ik ook graag een statische Google Maps-afbeelding (png). Inladen (bijvoorbeeld staticmap.png op de server). Nu is mijn vraag, hoe moet ik dat doen via write_multiline_text (de functie die hierboven is gemaakt)?
Ik heb al gegoogled, maar ik kom er niet uit. Kan iemand mij helpen misschien?
Kan je background.png niet vervangen door staticmap.png, want dat is toch je achtergrond?
Laat anders even zien wat je nu als afbeelding hebt, en wat je wilt.
Laat anders even zien wat je nu als afbeelding hebt, en wat je wilt.
Let erop dat als je een plaatje als achtergrond gebruik deze in regel 45 wit wordt gemaakt.
En regel 48 zal het niet doen als arial.ttf op een server staat.
Voor de rest is er zo te zien niets mis met de code.
Bij mij werkt het.
Toevoeging op 14/07/2021 21:02:32:
Gevonden :
https://stackoverflow.com/a/12028118
En regel 48 zal het niet doen als arial.ttf op een server staat.
Voor de rest is er zo te zien niets mis met de code.
Bij mij werkt het.
Toevoeging op 14/07/2021 21:02:32:
Gevonden :
https://stackoverflow.com/a/12028118
Gewijzigd op 14/07/2021 18:39:47 door Adoptive Solution




