0

I would like to create a script which is generating an image with one fix background image but display a randomly selected sentence on the image from a list which is previously given in the script.

So far I have a base and I've tried many variations for the random text, but none of them worked until this time.

Here is the script itself:

<?php

header('Content-type: image/png');
$text = 'The text which will be displayed';
$background_path = 'assets/bg_image.jpg';

$image = imagecreatetruecolor(1280, 720);
$background = imagecreatefromjpeg($background_path);
$color = imagecolorallocate($image, 255, 255, 255);

$title_sizes = imagettfbbox(24, 0, 'assets/roboto-medium.ttf', $text);
$title_height = $title_sizes[3];
$title_width = $title_sizes[4];

imagecopy($image, $background, 0, 0, 0, 0, 1280, 720);
imagettftext($image, 24, 0, (640 - $title_width / 2), (360 - $title_height / 2), $color, 'assets/roboto-medium.ttf', $text);

imagepng($image);

imagedestroy($image);
imagedestroy($background);

?>

So the thing is that the line "$text" allows only one sentence to be displayed. I would like to have multiple lines (various sentences) instead which will be displayed on the given background image randomly upon refresh.

Can you please help me to solve this problem?

Thank you very much in advance!

Regards, Adam

  • Doesnt' a simple `"...string end" . PHP_EOL . "new string start"` or even a `
    ` work?
    – Loek Jul 06 '18 at 13:19
  • Hello, I've tried these, they just put a line break in the text. – Adam Vetesi Jul 06 '18 at 13:22
  • Does this work then? https://stackoverflow.com/questions/6166261/using-the-imagettftext-function-with-multiple-lines-anyone-done-this-before – Loek Jul 06 '18 at 13:24
  • Unfortunately, no... Line breaks are OK with this, a simple
    works with them. My problem is, that I need something like: $text = 'Sentence one' $text2 = 'Sentence two' ...etc and to make the script to select one randomly to print it on the image file. I've checked already StackOverflow and tried many thins with $array, but none of them worked. Maybe I did something wrong... :/
    – Adam Vetesi Jul 06 '18 at 13:28

0 Answers0