Jump to content

php image available fonts


ultrus

Recommended Posts

Hello,
I recently started playing with the image creation in PHP. It's flipping awesome!

I'm curious about fonts. For a visual reference, I want to list all of the installed fonts, each list item in thier own font, in a series of images to display on a page. How do I do this? Below are my initial scripting thoughts on this process. I would much appriciate any help filling in the blanks. Thank you!  :)

[code]<?php

$numFonts = count("number of fonts"); //get the number of fonts somehow?
for($i=0; $i<$numFonts; $i++) {
  $fontName = "font name"; //get font name?
  //folowing referenced from php.net - may not be what's needed for this project
  $im = imagecreatefrompng("images/background.png");[/code]
Link to comment
Share on other sites

Hi gerkintrigg,
Thanks for the reply. This is the start of bigger projects. The fonts come from the server, not the client.

One use would be creating custom images/text using live weather data to overlap webcam images, making the webcam "box" blend in better with the website design.

Another application would be dynamic navigation menus in template systems.

Dynamic page title graphics could be generated, keeping the design consistent, and not worrying about whether or not the client has the appripriate font installed. One of my designs uses title graphics at an angle. php text generation could be useful for that sort of thing.

The image text could also be useful in cell phone gps systems. For example, I could have an image showing "Where is Ultrus", with stylized text saying "Ultrus is at work." over the map.

Perhaps an ecard system could use this. The card sender can choose what font to use when sending a jpg card via email, and preview the image afterwards.

Many possibilites. It's just hard for me not knowing what fonts are on my hosting service's server(s). Making a list would save me time, and give a visual reference.
Link to comment
Share on other sites

To make a list of the available fonts, you would need to know where they are located on the server. As far as I'm aware there isn't a PHP function to list them. Unless you know of a command within the Operating System to list them, you could then execute that command with the exec() function.

You can actually use your own True Type Fonts with GD, of which you can just create a directory in your webroot and throw your .ttf files in there and reference them in your script. You can use imagettftext() to select your fonts. If you were to go down this route, you could get a list off all your font files with the glob() function.
Link to comment
Share on other sites

SemiApocalyptic,
You rock! That helped me get on the right track. To show my appriciation, I have posted the working result below.

I created a folder caled "fonts" on the server, made it writable, and uploaded some ttf fonts from my linux machine. The script creates a catalog of all the fonts in the folder with a file name below each image. If you can think of fun ways to enhance this, I'd love to see suggestions.  Thanks for the great help!

[code]<?php
function showFonts($text, $dir) {
$fontInt = 1;
foreach (glob($dir . "*.ttf") as $filename) {
$im = @imagecreate(450, 70) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 0, 0, 0);
$font = $filename;
imagettftext($im, 30, 0, 10, 40, $text_color, $font, $text);
$imgFile = "fonts/font" . $fontInt . ".png";
imagepng($im, $imgFile);
imagedestroy($im);
echo "<p><img src=$imgFile><br />$filename</p>\n\n";
$fontInt++;
}
}

showFonts("This is way cool", "fonts/");
?> [/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.