vendredi 6 juillet 2018

Creating image with fix background and randomly displayed text

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




Aucun commentaire:

Enregistrer un commentaire