Thursday, February 28, 2008

Creating thumbs from textfiles with PHP and GD

Creating thumbs from textfiles with PHP and GD
$width = 80;
$height = 120;
$font = 3; // "3" is a default font in the GD library.


now the next step is getting the text files lines and how many characters from each line we can use.

$lines = file("test.txt");
$fontWidth = imagefontwidth($font);
$fontHeight = imagefontheight($font);
$maxCharsPerLine = ($width / $fontWidth) - 2;
$maxLines = ($height / $fontHeight) - 2;
$lineHeight = $fontHeight + 1;


Ok so we have that all done the next step is to get the image ready.

$image = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($image, 0, 0, 0);
$black = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $white);


Now we are ready to start adding text to do this will will just loop through our lines up to our maxLines limit and only use our maxCharsPerLine from each line. We will also replace tab characters with two spaces and remove any carriage return character

for($i = 0; $i < $maxLines; $i++) {
    $line = (strlen($lines[$i]) > $maxCharsPerLine) ? substr($lines[$i], 0, $maxCharsPerLine) : $lines[$i];
    $line = ereg_replace("\t", "  ", $line);
    $line = ereg_replace("\r", "", $line);
    
    imagestring($image, $font, 3, ($i * $lineHeight), $black);
}


We will then add a border to the image. Afterwards you can save your image and you should have a nice image with text from your text file on it.

imageline($image, 0, 0, $width, 0, $black);
imageline($image, ($width - 1), 0, ($width - 1), ($height - 1), $black);
imageline($image, ($width - 1), ($height - 1), 0, ($height - 1), $black);
imageline($image, 0, ($height - 1), 0, 0, $black);

imagepng($image, "test.png");
imagedestroy($image);


and for those of you who just want the function to use here it is.

function FileToThumb($file, $save_path, $font, $width, $height) {
    if(!is_numeric($font)) {
        $fontRes = imageloadfont($font);
    }
    else {
        $fontRes = $font;
    }

    $lines = file($file);
    $fontWidth = imagefontwidth($font);
    $fontHeight = imagefontheight($font);
    $maxCharsPerLine = ($width / $fontWidth) - 2;
    $maxLines = ($height / $fontHeight) - 2;
    $lineHeight = $fontHeight + 1;
 
    $image= imagecreatetruecolor($width, $height);
    $black = imagecolorallocate($image, 0, 0, 0);
    $white = imagecolorallocate($image, 255, 255, 255);
    imagefill($image, 0, 0, $white);

    for($i = 0; $i < $maxLines; $i++) {
        $line = (strlen($lines[$i]) > $maxCharsPerLine) ? substr($lines[$i], 0, $maxCharsPerLine) : $lines[$i];
        $line = ereg_replace("\t", "  ", $line);
        $line = ereg_replace("\r", "", $line);
         
        imagestring($image, $font, 3, ($i * $lineHeight), $line, $black);
    }

    imageline($image, 0, 0, $width, 0, $black);
    imageline($image, ($width - 1), 0, ($width - 1), ($height - 1), $black);
    imageline($image, ($width - 1), ($height - 1), 0, ($height - 1), $black);
    imageline($image, 0, ($height - 1), 0, 0, $black);    

    imagepng($image, $save_path);
    imagedestroy($image);
}


No comments:

Post a Comment