imagefttext

(PHP 4 >= 4.1.0, PHP 5)

imagefttext -- Write text to the image using fonts using FreeType 2

Description

array imagefttext ( resource image, float size, float angle, int x, int y, int col, string font_file, string text [, array extrainfo] )

Внимание

К настоящему времени эта функция еще не была документирована; для ознакомления доступен только список аргументов.

Замечание: Эта функция нуждается в GD версии 2.0.1 или выше.

Замечание: Parameter extrainfo is optional since PHP 4.3.5.



imagefttext
20-Feb-2006 02:19
I am using php 5.1.2 on a winxp machine. I was  getting into the TrueType fonts and wanted to see which ones would look best incorporated into web images. So I created the following script that prints out samples of all the TrueType fonts found in my C:\Windows\Fonts directory. The script takes only one request parameter - 'fsize'. It stands for font-size and lets you see each font in any size you wish -- I limited it to values between 5 and 48. Hope this helps someone other than me :)

I apologize in advance if any of my code is not the prettiest-written php code even seen -- I have only been coding in php for the past week (I'm a perl-guy usually).

<?php
  
list($x, $y, $maxwidth) = array(0, 0, 0);

  
$fsize = (int)$_REQUEST['fsize'];
   if (
$fsize < 5 or $fsize > 48) $fsize = 8;

  
header("Content-type: image/jpeg");

  
// don't know how wide or tall the font samples will be.
   // create a huge image for now, we'll copy it smaller
   // later when we know how large the image needs to be.
  
$im = imagecreate(1000, 20000) or die('could not create!');
  
$clr_white = imagecolorallocate($im, 255, 255, 255);
  
$clr_black = imagecolorallocate($im, 0, 0, 0);

  
$font_path = "C:/Windows/Fonts/";
  
$dh = opendir($font_path);
   while ((
$file = readdir($dh)) !== FALSE) {
      
// we're only dealing with TTY fonts here.
      
if (substr(strtolower($file), -4) != '.ttf') continue;

      
$str = "Sample text for '$file'";
      
$bbox = imagettfbbox(
          
$fsize, 0, "{$font_path}{$file}", $str
      
);
      
$ww = $bbox[4] - $bbox[6];
      
$hh = $bbox[1] - $bbox[7];

      
imagettftext(
          
$im, $fsize, 0, $x, $y,
          
$clr_black, "{$font_path}{$file}", $str
      
);

      
$y += $hh + 20;
       if (
$ww > $maxwidth) $maxwidth = $ww;
   }

  
closedir($dh);

  
// ok, now we can chop off the extra space from the
   // 1000 x 20000 image.
  
$im2 = imagecreate($maxwidth + 20, $y);
  
imagecopyresized(
      
$im2, $im, 0, 0, 0, 0, $maxwidth + 20,
      
$y, $maxwidth + 20, $y
  
);
  
imagejpeg($im2);
  
imagedestroy($im);
  
imagedestroy($im2);
?>
vsazel at atlas dot cz
23-Nov-2005 09:16
If you want to get the best result in monochrome font rendering, change render_mode to FT_LOAD_RENDER. It's the last parameter of FT_Load_Glyph() function (in gdft.c).
dnf at seznam dot cz
23-Jun-2005 02:25
For negative image you must add one line after the $grayColor computation:

$grayColor =  ~ $grayColor & 0x7FFFFFF;
aidan at php dot net
29-May-2005 03:11
This function is very simular to imageffttext(), you may find the information provided on its manual page helpful:

http://php.net/imagettftext
MagicalTux at FF dot st
27-Jan-2005 02:20
When compiling PHP with FreeType 2 support, you'll probably have some problems if - for example - you use debian and didn't compile freetype2 yourself...
If configure fails after saying "If configure fails, try --with-xpm-dir..." you most likely have FreeType1 installed, but not freetype2 ...

Do this as root :
apt-get install libfreetype6-dev

It took me some time to find out that apt-get install freetype2 is actually installing freetype1 ...
kagaku at gmail dot com
07-Dec-2004 08:27
I found myself in need of an align right function and found one on the imagepstext manual page. I can't imagine I'm the only person who's needed to use this, so here's a slightly modified version that works with imagefttext:

<?
 
function align_right($string, $fontfile, $imgwidth, $fontsize){
  
$spacing = 0;
  
$line = array("linespacing" => $spacing);
   list(
$lx,$ly,$rx,$ry) = imageftbbox($fontsize,0,$fontfile,$string,$line);
  
$textwidth = $rx - $lx;
  
$imw = ($imgwidth-10-$textwidth);
   return
$imw;
  }
?>
eshenk at comcast dot net
24-Jan-2004 02:05
I wrote a bit of code to gather all the .ttf files in the directory with this script, and randomize them to write text on a header image for my site. The only catch is the font files have to be named 1.ttf, 2.ttf etc etc.

<?php

srand
((double)microtime()*1234567); // Start the random gizmo
$image = imagecreatefromjpeg(rand(1,exec('ls *.jpg | wc -l')) . ".jpg"); // Get a background
$font = rand(1,exec('ls *.ttf | wc -l')) . ".ttf"; // Get a font
$textcolor = imagecolorallocate($image,0,0,0); // Set text color

$text1 = "shenko.homedns.org"; // Here is our text

imagettftext($image, 50, 0, 20, 50, $textcolor, $font, $text1); // Write the text with a font

header("Content-type: image/jpeg"); // Its a JPEG
imagejpeg($image,'',90); // Zap it to the browser
imagedestroy($image); // Memory Freeupage

?>
sebastiand at gmx dot de
17-Sep-2003 02:07
After spending the evening with some work on automatically generated images, I had the idea to switch of anti-aliasing (looking, if some font would look better that way), which turned out not to be quite so easy.

Actually you have to use the negative of the desired color to switch of antialising. I include the corresponding line from my code (line split up):

// USE NEGATIVE OF DESIRED COLOR TO SWITCH OF ANTI-ALIASING
ImageFTText ($neuesBild,$fontsize,$fontangle,$TextPosX,$TextPosY,
-$custom_fg,$fonttype,$text,array());
jwilliam at kcr dot uky dot edu
21-Nov-2002 12:22
Thanks for the script!  I modified it to show several fonts that I was wanting to use.  I am using GD-2.0.7, FreeType-2.1.3(text rotation fix,among others), and PHP-4.2.3 and had to include the array information to get it to work.

Code change follows:
$fontfile="/usr/local/fonts/ttf/bookantbd.ttf";
// Waterfall of point sizes to see what Freetype 2's autohinting looks like:
//
for($i=4;$i<=12;$i++){
  ImageFtText($image,$i,0,10,(280+$i*14),$forecolor,$fontfile, bookantbd . $i . ". " . $string, array("linespacing" => 1.0));
}

John
ben at tNOSPAManjNOSPAMo dot cnospamordots dot om
08-Aug-2002 12:59
If you're interested in turning off FreeType hinting, search for the following line in the gd source (gdft.c):
     err = FT_Load_Glyph (face, glyph_index, FT_LOAD_DEFAULT);
and replace it with
     err = FT_Load_Glyph (face, glyph_index, FT_LOAD_NO_HINTING);

Recompile GD, and vola: beauteous antialiasing.

<imageftbboximagegammacorrect>
 Last updated: Tue, 15 Nov 2005