Jump to content

Recommended Posts

I am trying to get an image of each font listed in a directory

 

If I try to get the list of fonts in the dir it works fine using this

<?php
    // read all .ttf file in the font directory
    if ($fd = opendir('./font')) {
        $files = array();
        while (($file = readdir($fd)) !== false) {
            if (substr($file, strlen($file) - 4) == '.ttf') {
                array_push($files, $file);
            }
        }
        closedir($fd);
    }


    
    // Sort the files and display
    sort($files);    
    foreach ($files as $file) {
        $title = Title($file);	
echo "$title<br>$file<br>\n";	
}
    
    // Function to get a readable title from the filename
    function Title($filename) {
        $title = substr($filename, 0, strlen($filename) - 4);
        $title = str_replace('-', ' ', $title);
        $title = ucwords($title);
        return $title;
    }



?> 

 

Is it possible to create an image preview using something like this:

<?php
header("Content-type: image/png");        
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'font/$file';
// Optional add some shadow to the text
imagettftext($im, 20, 0, 15, 25, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()	
imagepng($im);
imagedestroy($im);

?>

 

The problem I was getting was that it only creates an image on the first font file.

 

Any help would be appreciated.

This is what I have that gives me an image of the first file only

 

<?php
    header("Content-type: image/png");
    // read all .ttf file in the font directory
    if ($fd = opendir('./font')) {
        $files = array();
        while (($file = readdir($fd)) !== false) {
            if (substr($file, strlen($file) - 4) == '.ttf') {        
                array_push($files, $file);
            }

        }
        closedir($fd);
    }

    // Sort the files and display
    sort($files);    
    foreach ($files as $file) {
       // Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = "font/$file";
// Optional add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()	
imagepng($im);
imagedestroy($im);
$title = Title($file);
echo "$title<br>$file<br>\n";


}
    
    // Function to get a readable title from the filename
    function Title($filename) {
        $title = substr($filename, 0, strlen($filename) - 4);
        $title = str_replace('-', ' ', $title);
        $title = ucwords($title);
        return $title;
    }



?>

 

Any ideas?

No luck

 

Well maybe I can explain it better this way

 

If I use

<?php
$path = "./font";
$dir_handle = @opendir($path) or die("Unable to open $path");
while($file = readdir($dir_handle))
{
   if(substr($file, -4) == '.ttf') 
   {
      $length = strlen($file);
        $file2 = substr($file, 0, ($length-4));	
echo "$file2<br>";
   }
}

closedir($dir_handle);

?>

I get

arial

tahoma

tallpaul

vrinda

Thats all of them

 

But If I use

<?php
$path = "./font";
$dir_handle = @opendir($path) or die("Unable to open $path");
while($file = readdir($dir_handle))
{
   if(substr($file, -4) == '.ttf') 
   {
      $length = strlen($file);
        $file2 = substr($file, 0, ($length-4));	
$im = imagecreatetruecolor(400, 30);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = 'Testing...';
$font = 'font/'.$file2.'.ttf';
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
   }
}

closedir($dir_handle);

?>

 

I only get an image in Arial font and then it stops running the script - wont even echo anything else after that.

 

Any ideas??

Think I got it now

Is this what you were talking about?

 

The image code (fontgen.php)

<?php
header("Content-type: image/png");
$im = imagecreate(600, 40);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagettftext($im, 30, 0, 11, 31, $grey, $font, $text);
imagettftext($im, 30, 0, 10, 30, $black, $font, $text);
imagepng($im);
imagedestroy($im);
?>

 

Used to call the images

<?php
$path = "./font";
$dir_handle = @opendir($path) or die("Unable to open $path");
while($file = readdir($dir_handle))
{
   if(substr($file, -4) == '.ttf') 
   {
      $length = strlen($file);
        $file2 = substr($file, 0, ($length-4));	
echo "$file2<br>";
$font = 'font/'.$file2.'.ttf';
echo "<img src=\"fontgen.php?font=$font&text=ABCDEFG abcdefg 1234567890\"><br><br>";
   }
}

closedir($dir_handle);

?>

thats one option

 

but fontgen.php should be more like

<?php
header("Content-type: image/png");
$im = imagecreate(600, 40);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagettftext($im, 30, 0, 11, 31, $grey, $_GET['font'], $_GET['text']);
imagettftext($im, 30, 0, 10, 30, $black, $_GET['font'], $_GET['text']);
imagepng($im);
imagedestroy($im);
?>

 

Think I got it now

Is this what you were talking about?

 

The image code (fontgen.php)

<?php
header("Content-type: image/png");
$im = imagecreate(600, 40);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagettftext($im, 30, 0, 11, 31, $grey, $font, $text);
imagettftext($im, 30, 0, 10, 30, $black, $font, $text);
imagepng($im);
imagedestroy($im);
?>

 

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.