user1117871
user1117871

Reputation: 11

Adding inner shadow to text

I'm looking for the way to add inner shadow to text using PHP. I'm not looking for a solution, which includes HTML and/or CSS. Image must be generated using only PHP.

For examle: https://i.sstatic.net/H4ndF.png
From the text above ('Original') I want to create modify text, so it will look like text at the bottom ('Shadow').
Effect must be achieved using only GD library or Imagemagick, because I can't install new libraries to the server.

Upvotes: 1

Views: 1846

Answers (1)

hacksteak25
hacksteak25

Reputation: 2039

One way is to draw your text twice with different colors and a small offset.

Below is the sample code, mainly taken from the php manual, with some modifications to let the shadow appear. The resulting image is here.

Code:

<?php
// Create a 300x100 image
$im = imagecreatetruecolor(300, 100);
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$gray = imagecolorallocate($im, 0x55, 0x55, 0x55);
$gray2 = imagecolorallocate($im, 0xDD, 0xDD, 0xDD);

// Make the background red
imagefilledrectangle($im, 0, 0, 299, 99, $gray2);

// Path to our ttf font file
$font_file = './ariblk.ttf';

// the text without shadow
imagefttext($im, 40, 0, 10, 45, $white, $font_file, 'Original');

// the shadow for "Shadow"
imagefttext($im, 40, 0, 10, 89, $gray, $font_file, 'Shadow');

// and the word itself
imagefttext($im, 40, 0, 10, 90, $white, $font_file, 'Shadow');

// Output image to the browser
header('Content-Type: image/png');

imagepng($im);
imagedestroy($im);
?>

Upvotes: 1

Related Questions