Font embedding using PHP

Hi friends, today am going to explain you how to embed the custom font using PHP. In most of the design we need to use special fonts to look our design great, but bringing same look in HTM is a big task. Font embedding is the main thing in CSS designing. Here I’m explaining font embedding using PHP.

Embed custom font  using PHP

In this method of font embedding we should upload our font file (.ttf) to server. Using PHP we are creating a dynamic image based on your input parameters.

Download FileDemo

// Get the width from URL
$im = imagecreatetruecolor($_GET['width'], 26);
//else default width for image
$im = imagecreatetruecolor(196, 26);

//Image background transparency
imagealphablending($im, false);
imagesavealpha($im, true);

// Create color of the text
$transparent = imagecolorallocatealpha($im, 255,255,255, 127);
$white = imagecolorallocate($im, $_GET['r'], $_GET['g'], $_GET['b']);
$grey = imagecolorallocate($im, 127, 127, 127);
$black = imagecolorallocate($im, 46, 167, 247);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
imagefilledrectangle($im, 0, 0, 399, 29, $transparent);

// Path to your font file
$font = 'css/BrieLight.ttf';

// Get the text to create as image

// Create the image from text
imagettftext($im, 18, 0, 0, 20, $white, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()

Hope that you will like this tutorial.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>