Tweet
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.

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.
// Get the width from URL
if($_GET['width']!='')
{
$im = imagecreatetruecolor($_GET['width'], 26);
}else
{
//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
$text=$_GET['text'];
// Create the image from text
imagettftext($im, 18, 0, 0, 20, $white, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
Hope that you will like this tutorial.
