0

Hi,
I have a PHP script that generates an image based on a font and text string to display custom fonts on my site. Overall the code works fine but there are a few little bugs;
The image is clearly not high enough to display the full font correctly, part of the lower letters (like y, g, q etc.) get cut off.
The code I'm using is

<?php
//Check if the file is in cache
if ( file_exists('cache/'.$_GET['data']) == true ) {
	$Last_mod = substr(date('r', filemtime('cache/'.$_GET['data'])), 0, -5).'GMT';
    $etag = md5($Last_mod);
	
	$NeedToSend = true;
	
	// Send the headers
    header("Last-Modified: $Last_mod");
    header("ETag: $etag");
	
	//And if the browser already has the file
	// See if the client has provided the required headers
	if ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && isset($_SERVER['HTTP_IF_NONE_MATCH']) ) {
		//Now check if they match our current cache status
		if ( $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $Last_mod && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag ) {
			$NeedToSend = false;
		}
	}
	
	if ( $NeedToSend == false ) {
		// Nothing has changed since their last request - serve a 304 and exit
		header('HTTP/1.0 304 Not Modified');
		exit;
	}

	header('Content-Type: image/jpeg');
	header('Cache-Status: saved');
	echo file_get_contents('cache/'.$_GET['data']);
	exit;
}

//Otherwise generate and save the image

//Add the slashes back in and de-serialize into data
$CoreData = json_decode(base64_decode(str_replace(':', '/', $_GET['data'])));

$FontLib = 'fonts/ttf/';//Path to the directory with the fonts in

//Count the number of new-lines and replace them
$Text = str_replace('|', "\n", $CoreData->text, $NumLines);
$NumLines = $NumLines+1;

//Load our font
$Font = $FontLib.$CoreData->font;

$Dimensions = imageftbbox($CoreData->size+1, 0, $Font, $Text);

//Now calculate the width/height of the text
$W = $Dimensions[4]-$Dimensions[0];
$H = $Dimensions[1]-$Dimensions[5]+($NumLines+5);

$Image = imagecreatetruecolor($W, $H);//Create an image the same size as the text

$FontColor = imagecolorallocate($Image, hexdec(substr($CoreData->color, 0, 2)), hexdec(substr($CoreData->color, 2, 2)), hexdec(substr($CoreData->color, 4, 2)));

$BGColor = imagecolorallocate($Image, hexdec(substr($CoreData->bgcolor, 0, 2)), hexdec(substr($CoreData->bgcolor, 2, 2)), hexdec(substr($CoreData->bgcolor, 4, 2)));

imagefill($Image, 0, 0, $BGColor);

$y = ($Dimensions[1]-$Dimensions[5])/$NumLines;

imagefttext($Image, $CoreData->size, 0, 0, $y, $FontColor, $Font, $Text);

header('Content-type: image/jpeg');
header('Cache-Status: generated');
imagejpeg($Image);
imagejpeg($Image, 'cache/'.$_GET['data'], 100);
?>

and the code to generate the links to the image is

<?php
//This file just generates the embed code, the actual JPG generator is located in the /media/fonts directory

function custom_font($FontName, $FontColor, $BackgroundColor, $Text, $Size, $ID = NULL) {//Colors are in HEX
	$URL = 'http://example.com/';
	$URL_Parts['font'] = $FontName;
	$URL_Parts['color'] = $FontColor;
	$URL_Parts['bgcolor'] = $BackgroundColor;
	$URL_Parts['text'] = $Text;
	$URL_Parts['size'] = $Size;
	
	//Base64 encode then replace the chars that fuck up URL rewrites, we add them back in later
	$URL .= str_replace(array('=', '/'), array('', ':') , base64_encode(json_encode($URL_Parts)));
	$URL .= '.jpg';
	
	return "<img alt=\"$Text\" border=\"0\" src=\"$URL\" id=\"$ID\" />";
}

if it helps I also have a .htaccess file that does the rewrites

RewriteEngine on
RewriteRule ^([0-9a-zA-Z:]+).jpg$ generate.php?data=$1 [QSA]

-Sam

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Nyight
0

Just add 3-4+ onto the height of the image?

$Image = imagecreatetruecolor($W, $H+3);//Create an image the same size as the text
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.