How to Write Text Over Image / Picture using PHP

Adding custom text over image is a good way to declare give a simple title information to that graphical image or picture without editing the image itself. PHP has made it very easy to add custom text over image or picture by using some basic functions from PHP GD.

Example

$title  = "How to Send HTML Form array in Codeigniter using Ajax";
$file_name = 'tutorial_'.date('YmdHis').'.jpg';

header('content-type: image/jpeg');

$image = imagecreatefrompng('front/img/smallimage.png');

$image_big = imagecreatefrompng('front/img/mainImagebig.png');

$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 61, 59, 54);

$white_big = imagecolorallocate($image_big, 255, 255, 255);
$black_big = imagecolorallocate($image_big, 61, 59, 54);

$font_path =  'front/fonts/Franchise-Bold-hinted.ttf';

$text = wordwrap($title, 25, "\n");
$text_big = wordwrap($title, 25, "\n");

imagettftext($image, 24, 0, 20, 40, $black, $font_path, $text);
imagettftext($image_big, 44, 0, 40, 80, $black_big, $font_path, $text_big);

imagejpeg($image, 'blogimage/' . $file_name, 100);
imagejpeg($image_big, 'blogimage/' . $file_name, 100);

imagedestroy($image);
imagedestroy($image_big);

Leave a Reply

Your email address will not be published. Required fields are marked *