943,022 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 806
  • PHP RSS
Sep 4th, 2010
0

PHP font image generator

Expand Post »
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 Syntax (Toggle Plain Text)
  1. <?php
  2. //Check if the file is in cache
  3. if ( file_exists('cache/'.$_GET['data']) == true ) {
  4. $Last_mod = substr(date('r', filemtime('cache/'.$_GET['data'])), 0, -5).'GMT';
  5. $etag = md5($Last_mod);
  6.  
  7. $NeedToSend = true;
  8.  
  9. // Send the headers
  10. header("Last-Modified: $Last_mod");
  11. header("ETag: $etag");
  12.  
  13. //And if the browser already has the file
  14. // See if the client has provided the required headers
  15. if ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && isset($_SERVER['HTTP_IF_NONE_MATCH']) ) {
  16. //Now check if they match our current cache status
  17. if ( $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $Last_mod && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag ) {
  18. $NeedToSend = false;
  19. }
  20. }
  21.  
  22. if ( $NeedToSend == false ) {
  23. // Nothing has changed since their last request - serve a 304 and exit
  24. header('HTTP/1.0 304 Not Modified');
  25. exit;
  26. }
  27.  
  28. header('Content-Type: image/jpeg');
  29. header('Cache-Status: saved');
  30. echo file_get_contents('cache/'.$_GET['data']);
  31. exit;
  32. }
  33.  
  34. //Otherwise generate and save the image
  35.  
  36. //Add the slashes back in and de-serialize into data
  37. $CoreData = json_decode(base64_decode(str_replace(':', '/', $_GET['data'])));
  38.  
  39. $FontLib = 'fonts/ttf/';//Path to the directory with the fonts in
  40.  
  41. //Count the number of new-lines and replace them
  42. $Text = str_replace('|', "\n", $CoreData->text, $NumLines);
  43. $NumLines = $NumLines+1;
  44.  
  45. //Load our font
  46. $Font = $FontLib.$CoreData->font;
  47.  
  48. $Dimensions = imageftbbox($CoreData->size+1, 0, $Font, $Text);
  49.  
  50. //Now calculate the width/height of the text
  51. $W = $Dimensions[4]-$Dimensions[0];
  52. $H = $Dimensions[1]-$Dimensions[5]+($NumLines+5);
  53.  
  54. $Image = imagecreatetruecolor($W, $H);//Create an image the same size as the text
  55.  
  56. $FontColor = imagecolorallocate($Image, hexdec(substr($CoreData->color, 0, 2)), hexdec(substr($CoreData->color, 2, 2)), hexdec(substr($CoreData->color, 4, 2)));
  57.  
  58. $BGColor = imagecolorallocate($Image, hexdec(substr($CoreData->bgcolor, 0, 2)), hexdec(substr($CoreData->bgcolor, 2, 2)), hexdec(substr($CoreData->bgcolor, 4, 2)));
  59.  
  60. imagefill($Image, 0, 0, $BGColor);
  61.  
  62. $y = ($Dimensions[1]-$Dimensions[5])/$NumLines;
  63.  
  64. imagefttext($Image, $CoreData->size, 0, 0, $y, $FontColor, $Font, $Text);
  65.  
  66. header('Content-type: image/jpeg');
  67. header('Cache-Status: generated');
  68. imagejpeg($Image);
  69. imagejpeg($Image, 'cache/'.$_GET['data'], 100);
  70. ?>
and the code to generate the links to the image is
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. //This file just generates the embed code, the actual JPG generator is located in the /media/fonts directory
  3.  
  4. function custom_font($FontName, $FontColor, $BackgroundColor, $Text, $Size, $ID = NULL) {//Colors are in HEX
  5. $URL = 'http://example.com/';
  6. $URL_Parts['font'] = $FontName;
  7. $URL_Parts['color'] = $FontColor;
  8. $URL_Parts['bgcolor'] = $BackgroundColor;
  9. $URL_Parts['text'] = $Text;
  10. $URL_Parts['size'] = $Size;
  11.  
  12. //Base64 encode then replace the chars that **** up URL rewrites, we add them back in later
  13. $URL .= str_replace(array('=', '/'), array('', ':') , base64_encode(json_encode($URL_Parts)));
  14. $URL .= '.jpg';
  15.  
  16. return "<img alt=\"$Text\" border=\"0\" src=\"$URL\" id=\"$ID\" />";
  17. }

if it helps I also have a .htaccess file that does the rewrites
PHP Syntax (Toggle Plain Text)
  1. RewriteEngine on
  2. RewriteRule ^([0-9a-zA-Z:]+).jpg$ generate.php?data=$1 [QSA]

-Sam
Reputation Points: 26
Solved Threads: 31
Posting Whiz
samarudge is offline Offline
354 posts
since May 2008
Sep 6th, 2010
0
Re: PHP font image generator
Just add 3-4+ onto the height of the image?
php Syntax (Toggle Plain Text)
  1. $Image = imagecreatetruecolor($W, $H+3);//Create an image the same size as the text
Reputation Points: 23
Solved Threads: 21
Junior Poster in Training
Nyight is offline Offline
99 posts
since Aug 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: how to display arrays
Next Thread in PHP Forum Timeline: PHP count multiple





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC