How to draw text on an image using php

Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to draw text on an image using php

Post by Neo » Fri Apr 02, 2010 2:09 pm

Here is a nice and simple code to do this.

Code: Select all

<?php
// Set the content-type
header('Content-type: image/png');

// Create the image
$im = $im = ImageCreateFromjpeg("monitor.jpg");

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';

// Add some shadow to the text
imagettftext($im, 5, 0, 21, 51, $grey, $font, $text);

// Add the text
imagettftext($im, 5, 0, 19, 50, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
Post Reply

Return to “PHP & MySQL”