Output Text on Image using PHP imagettfbbox
Easily create an image with text written on it using PHP imagettfbbox. This function requires both the GD library and the FreeType library. Replace variables with your own, PHP script will take care of rest.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
<?php
$image_width = 500; //image width
$image_height = 150; //image height
$font_color = array(0, 0, 0); //RGB color
$background_color = array(152, 183, 236); //RGB Color
$font_size = 10; //size of font
$text_rotation = 0; //text rotation value
$margin_left = 10; //margin left
$margin_top = 20; //margin top
$font = 'verdana.ttf'; //font path
$string = "This is the string\nwritten on the image.";
//create 300x300 pixel image
$new_image = imagecreatetruecolor($image_width, $image_height);
$font_color = imagecolorallocate($new_image, $font_color[0],$font_color[1],$font_color[2]);
$background_color = imagecolorallocate($new_image, $background_color[0],$background_color[1],$background_color[2]);
// Set background color
imagefilledrectangle($new_image, 0, 0, ($image_width-1), ($image_height-1),$background_color);
$Text = imagettfbbox(0, 0, $font, $string);
imagettftext($new_image, $font_size, $text_rotation, $margin_left, $margin_top, $font_color, $font, $string);
//output image
header('Content-Type: image/jpeg');
imagepng($new_image);
imagedestroy($new_image);
?>