In this article I am going to demonstrate you how to put text on the image using php.
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
$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 300×300 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);
?>
First up we need to specify the dimensions of the image hence we declare them in the variables later we specify the RGB color and assign it to a variable now we get to the font size of the text to be displayed on the image and we also declare the size, margins ,font and Finally the message to be displayed
Now we create an image using the method in PHP and we set it with a background color Finally we redirect it with a header redirect.
No comments:
Post a Comment