9951 explained code solutions for 126 technologies


php-gdHow to draw text with shadow


To simulate shadow, we just draw our text 2 times with a small shift in position:

<?php

$im = imagecreatetruecolor(400, 300);

$c_black = imageColorAllocate($im, 0,0,0);
$c_green = imageColorAllocate($im, 46,204,64);
$c_shadow = imageColorAllocate($im, 96,96,96);

$font = '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf';
imagettftext($im, 40, 0, 104, 104, $c_shadow, $font, 'Hi!');
imagettftext($im, 40, 0, 100, 100, $c_green, $font, 'Hi!');

imagePng($im, '/tmp/image.png');ctrl + c
imagecreatetruecolor

creates true color GD image object with specified width & height

imageColorAllocate

creates color object to later use in image

$font

path to ttf font to use

imagettftext

draw text with given ttf font

$c_green

text color

$c_shadow

text shadow color

104, 104

shadow coordinates are shifted by 4 pixels


How to draw text with shadow, php gd

Usage example

<?php

$im = imagecreatetruecolor(400, 300);

$c_black = imageColorAllocate($im, 0,0,0);
$c_green = imageColorAllocate($im, 46,204,64);
$c_shadow = imageColorAllocate($im, 96,96,96);

$font = '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf';
imagettftext($im, 40, 0, 104, 104, $c_shadow, $font, 'Hi!');
imagettftext($im, 40, 0, 100, 100, $c_green, $font, 'Hi!');

imagePng($im, '/tmp/image.png');