This will be a small tutorial on how to create preview images / thumbnails of textfiles using PHP and the built-in GD library.
Note: This tutorial requires that you have GD2+ with Freetype support.
Ok so lets get started. This is actually quite an easy task, the basic idea is that we read in the lines of the
textfile convert tabs to spaces, and then draw each line to the image file we have created, easy huh?
First things first lets create the function name and arguments.
function FileToThumb($textSource, $thumbDest, $width, $height, $useEntireFile) {
}
Ok we now have are function and are ready to start adding code, but first a brief overview of
what each argument is for.
$textSource - The textfile to read from.
$thumbDesc - The path to save the image, use null to display in browser.
$width - The width of the image.
$height - The height of the image.
$useEntireFile - Use the entire file for the preview or just the first few lines.
Now that we have got that out of the way, lets start adding are code.
Firstly we will want to check a couple of arguments, as well as checking for are
fontfile.
if(file_exists($textSource) && is_readable($textSource) && file_exists("arial.ttf")) {
if($useEntireFile)
$fontSize = 4;
else
$fontSize = 7;
Ok that will check that the text file and font file exists as well ad checking whether of not
the text file is readable.
Now we can load are text file, get the maximum number of lines to draw
and set up are image and colors.
$lines = file($textSource); $lineHeight = ($fontSize + 2); $maxLines = ($height / $lineHeight); $thumb = imagecreatetruecolor($width, $height); $black = imagecolorallocate($thumb, 0, 0, 0); $white = imagecolorallocate($thumb, 255, 255, 255); imagefill($thumb, 0, 0, $white);
Ok now all that is left is to draw the lines and save the image.
for($i = 1; $i < $maxLines + 1; $i++) {
$line = str_replace("\t", " ", $lines[$i]);
imagettftext($thumb, $fontSize, 0, 2, ($i * $lineHeight), $black, "arial.ttf", $line);
}
if(!imagepng($thumb, $thumbDest)) {
return false;
}
return true;
Ok and there it is, I used png because png is the better format for save images which contain alot of text.
But now you probaly want the whole thing, so here you go.
<?php
/*
FileToThumb - Creates a thumbnail preview of a textfile.
arg1 = textfile to create preview of.
arg2 = destination of thumbnail preview. (Use null to display to browser)
arg3 = width of thumbnail.
arg4 = height of thumbnail.
arg5 = true or false for using the entire file or the first few
lines for the thumbnail.
*/
function FileToThumb($textSource, $thumbDest, $width, $height, $useEntireFile) {
if(file_exists($textSource) && is_readable($textSource) && file_exists("arial.ttf")) {
if($useEntireFile)
$fontSize = 4;
else
$fontSize = 7;
$lines = file($textSource);
$lineHeight = ($fontSize + 2);
$maxLines = ($height / $lineHeight);
$thumb = imagecreatetruecolor($width, $height);
$black = imagecolorallocate($thumb, 0, 0, 0);
$white = imagecolorallocate($thumb, 255, 255, 255);
imagefill($thumb, 0, 0, $white);
for($i = 1; $i < $maxLines + 1; $i++) {
$line = str_replace("\t", " ", $lines[$i]);
imagettftext($thumb, $fontSize, 0, 2, ($i * $lineHeight), $black, "arial.ttf", $line);
}
if(!imagepng($thumb, $thumbDest)) {
return false;
}
return true;
}
}
?>
Here is a simple example of the output of the function using this statment.
FileToThumb("test.txt", "test.png", 80, 120, false);
There is your ready to use function for creating image previews of textfiles, enjoy.


0 Responses to “Create image preview of textfile with PHP and the GD library.”
Leave a Reply