Jump to content


Photo

php image available fonts


  • Please log in to reply
5 replies to this topic

#1 ultrus

ultrus
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 13 September 2006 - 02:50 PM

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!  :)

<?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");

Twitter | Blog | Worlds to explore. Worlds to create.

#2 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 13 September 2006 - 02:59 PM

why do it in image form? why not just export the plain text? Do you mean the client's installed fonts or the installed images on the server in the GD library?

Seems like a lot of work for minimal reward.
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#3 ultrus

ultrus
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 13 September 2006 - 03:51 PM

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.
Twitter | Blog | Worlds to explore. Worlds to create.

#4 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 13 September 2006 - 03:58 PM

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.

#5 ultrus

ultrus
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 13 September 2006 - 10:47 PM

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!

<?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/");
?>

Twitter | Blog | Worlds to explore. Worlds to create.

#6 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 14 September 2006 - 06:17 AM

Hmm sounds rather complicated. I understand why you'd need to look at the server's fonts... makes more sense than the client's.
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users