http://www.jeremybrand.com/Jeremy/Brand/Jeremy_Brand.html
Image Create for PHP.
Release 1.0.0
http://www.nirvani.net/software/image_create/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
INSTRUCTIONS FOR USE:
Installation:
1) Place a copy of this script on your PHP4 enabled
web server somewhere in the document root accessable
to the world.
2) Make a folder on your web server and copy some *.ttf (true
type font) files into this folder. This is typically _not_
located in the document root.
3) Choose a folder where Image Create for PHP can create a
cache file. On unix a folder would have perms of something
like 4757.
4) Download a copy of variables from URI from
http://www.nirvani.net/software/variables_from_uri/.
Re-name the file variables_from_uri.inc and install it
somewhere on your web server. Usually this is _not_
installed in the docroot.
Configuration:
1) Change FONT_PATH to be the folder where fonts are installed (step
2 above). The folder name _must_ end with a slash /.
2) Change FONT_CACHE_FILE_PATH to be the folder that will contain the
cache file (from step 3 above). Again the folder name should
end in /.
3) Choose a name for the cache file, or leave it anduse the default
name.
4) Change the include for variables_from_uri.inc to be the path
and filename of where you installed variables from URI (from
step 4 above).
5) Scroll down to 'HOSTS' and add your web server's host name
and any other host name that you want to be able to access
these images. Add the hosts as separate 'case' statements.
If you've followed directions and it still doesn't work, you
probably don't have either PHP4, the GD library or TTF support
in PHP. Find your nearest guru.
Example:
br, bg, bb -- background red, green and blue (in decimal).
fr, fg, fb -- foreground red, green and blue (in decimal).
t -- 0 or 1. Wether or not you want a transparent image.
fs -- font size (decimal).
fn -- font name (check your cache file for font names after
you have ran this script once).
s -- the string to turn into an image.
Notes:
If you add more true type fonts to your folder, simply
delete the cache file (where ever you defined it to be)
and it will be re-created with the new font listings.
**/
/********************************************************************/
/**** FONT_CACHE_FILE_PATH MUST BE WRITEABLE BY YOUR WEB SERVER ****/
/********************************************************************/
define('FONT_PATH', '/usr/local/fonts/');
define('FONT_CACHE_FILE_PATH', '/home/nobody/cache/');
define('FONT_CACHE_FILE', 'font_cache.php');
/********************************************************************/
/**** REQUIRES "variables_from_uri.inc" ****/
/**** AVAILABLE FROM http://www.nirvani.net/software/ ****/
/**** DOWNLOAD IT AND SAVE IT SOMEWHERE ON YOUR SERVER. ****/
/**** MAKE SURE THE FOLLOWING INCLUDE() HAS THE CORRECT PATH ****/
/********************************************************************/
include('/usr/local/includes/variables_from_uri.inc');
variables_from_uri('=','s,br,bg,bb,fr,fg,fb,fs,fn,t');
/********************************************************************/
/**** THIS WILL AUTOMATICALLY DEFINE THE FONTS THAT CAN BE USED ****/
/**** WITH THIS SCRIPT ****/
/********************************************************************/
if (!file_exists(FONT_CACHE_FILE_PATH . FONT_CACHE_FILE))
{
$fd = fopen(FONT_CACHE_FILE_PATH . FONT_CACHE_FILE, 'w');
if ($fd)
{
$buf = <<read())
{
if (eregi('\.ttf$',$entry))
{
$short_name = str_replace(' ','_',strtolower(eregi_replace('\.ttf$','',$entry)));
$buf .= <<close();
$buf .= <<
EOT;
fwrite($fd, $buf, strlen($buf));
fclose($fd);
}
}
include(FONT_CACHE_FILE_PATH . FONT_CACHE_FILE);
/********************************************************************/
/**** Add multiple cases for HOSTS allowed to view this ****/
/**** so nobody links to your images, taking up your CPU, ****/
/**** etc without you knowing ****/
/********************************************************************/
$url_array = parse_url($HTTP_REFERER);
switch (strtolower($url_array['host']))
{
/********************************************************************/
/**** ADD MORE 'CASE - BREAK' STATEMENTS HERE ****/
/**** FOR HOSTS YOU WANT TO ALLOW TO USE THIS SCRIPT ****/
/********************************************************************/
case 'www.yourserver.moc':
case 'yourserver.moc':
case 'www2.yourserver2.moc':
case 'www3.yourserver.moc':
break;
/********************************************************************/
/**** BLANK IS TO ALLOW SIMPLE VIEWING OF ONLY THIS IMAGE ****/
/********************************************************************/
case '':
break;
/********************************************************************/
/**** THE DEFAULT IS TO EXIT THE SCRIPT, NO BANDWITH OR ANY ****/
/**** MORE CPU FOR UNAUTHORIZED LINKING TO THIS SCRIPT... ****/
/********************************************************************/
default:
exit();
break;
}
/* SANITY CHECKING */
$error = 0;
if ($fs <1 || $fs > 150)
$error += 1;
if (($br <0 || $br > 255) || ($bg <0 || $bg > 255) || ($bb <0 || $bb > 255))
$error += 2;
if (($fr <0 || $fr > 255) || ($fg <0 || $fg > 255) || ($fb <0 || $fb > 255))
$error += 4;
if (!strlen($s))
$error += 8;
if (!strlen($fonts_array[$fn]))
$error += 16;
if ($error)
exit();
/* You may want to do something other than exit, but that was good */
/* enough for me. */
/* This function (tries) to get the width of the canvas you will need */
/* to draw your image based on the font and text */
function get_width($array)
{
$width = abs($array[2]) + abs($array[0]);
return $width*1.10;
}
/* This function (tries) to get the height of the canvas you will need */
/* to draw your image based on the font and text */
function get_height($array)
{
$height = abs($array[3]) + abs($array[5]);
return $height*1.30;
}
/* FINALY GET TO THE GOOD STUFF... BASICALLY EVERYTHING STARTS HERE */
/* Pick a time for the cache to start */
$ts = mktime(12,12,12,1,1,2001);
header('Content-Type: image/gif');
header('X-Font-Name: ' . $fonts_array[$fn]);
header('Connection: close');
header('Cache-Control: max-age=7776000');
header('Last-modified: ' . date('D, d M Y H:i:s',$ts) . ' GMT');
$array = imagettfbbox($fs, 0, FONT_PATH.$fonts_array[$fn], $s);
$im = imagecreate(get_width($array),get_height($array));
$bg = ImageColorAllocate($im, $br,$bg,$bb);
$fg = ImageColorAllocate($im, $fr,$fg,$fb);
if ((int)$t)
$trans = imagecolortransparent($im, $bg);
ImageTTFText($im, $fs, 0, abs($array[0])*2, abs($array[5]), $fg, FONT_PATH.$fonts_array[$fn], $s);
ImageGif($im);
ImageDestroy($im);
?>