tryingtolearn Posted March 31, 2007 Share Posted March 31, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/45079-solved-creating-preview-images-dynamically/ Share on other sites More sharing options...
tryingtolearn Posted March 31, 2007 Author Share Posted March 31, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/45079-solved-creating-preview-images-dynamically/#findComment-218860 Share on other sites More sharing options...
tryingtolearn Posted April 1, 2007 Author Share Posted April 1, 2007 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?? Quote Link to comment https://forums.phpfreaks.com/topic/45079-solved-creating-preview-images-dynamically/#findComment-219023 Share on other sites More sharing options...
MadTechie Posted April 1, 2007 Share Posted April 1, 2007 the script won't work on a loop as you are creating then destroying each png, you need to create only 1 png and send the header only once or save the file each time. Quote Link to comment https://forums.phpfreaks.com/topic/45079-solved-creating-preview-images-dynamically/#findComment-219028 Share on other sites More sharing options...
tryingtolearn Posted April 1, 2007 Author Share Posted April 1, 2007 When you say create only 1 png you are saying I will only get the 1 font? Or is there a different way to get all of them for a preview??? Quote Link to comment https://forums.phpfreaks.com/topic/45079-solved-creating-preview-images-dynamically/#findComment-219031 Share on other sites More sharing options...
MadTechie Posted April 1, 2007 Share Posted April 1, 2007 i am saying you are using a routine that by design is for creating a single png, so you need to either just create that 1 file per font, or only use the imagettftext in the loop Quote Link to comment https://forums.phpfreaks.com/topic/45079-solved-creating-preview-images-dynamically/#findComment-219040 Share on other sites More sharing options...
tryingtolearn Posted April 1, 2007 Author Share Posted April 1, 2007 Thanks ??? Quote Link to comment https://forums.phpfreaks.com/topic/45079-solved-creating-preview-images-dynamically/#findComment-219045 Share on other sites More sharing options...
tryingtolearn Posted April 1, 2007 Author Share Posted April 1, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/45079-solved-creating-preview-images-dynamically/#findComment-219057 Share on other sites More sharing options...
MadTechie Posted April 1, 2007 Share Posted April 1, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/45079-solved-creating-preview-images-dynamically/#findComment-219216 Share on other sites More sharing options...
tryingtolearn Posted April 1, 2007 Author Share Posted April 1, 2007 Thanks, I appreciate the advice/help. Quote Link to comment https://forums.phpfreaks.com/topic/45079-solved-creating-preview-images-dynamically/#findComment-219401 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.