Search code examples
phpimagetext

php two equaL columns from text in text file to image


<?php
//include new data
//require_once 'grab.php';

$filename = "result.txt";

function convert($path) {
    $lines = file($path);
    $newpath = substr($path, 0, -3) . 'png';
    $im = imagecreate(800, 20 * count($lines));
    //background//
    $red = imagecolorallocate($im, 178, 34, 34);
    //background//
    $white = imagecolorallocate($im, 255, 255, 255);
    //background//
    //$white = imagecolorallocate($im, 255, 255, 255);
    
            
        foreach($lines as $i => $line) {
        //unknown-leftMargin-lineSpace//
        imagestring($im, 12, 50, 20 * $i, trim($line), $white);
        
    }
    imagepng($im, $newpath);
    imagedestroy($im);
}

//chdir('C:\TOP-20');
convert($filename);
echo "<center><img src='result.png' height='auto' width='auto' alt='error'><br>";
?>

**I can only produce a single column in an image how do i create two equal columns like 10 on teh left and 10 on the right. **


Solution

  • Like this ?

    
    <?php
    //
    //include new data
    //require_once 'grab.php';
    
    //$filename = "result.txt";
    $filename = "result.txt";
    
    function convert($path) {
        $lines = file($path);
    
        $width = 800;
        $n = count($lines);
        $nhalf = ceil(count($lines)/2.0);
        
        $newpath = substr($path, 0, -3) . 'png';
        $im = imagecreate($width, 20 * $nhalf);
        //background//
        $red = imagecolorallocate($im, 178, 34, 34);
        //background//
        $white = imagecolorallocate($im, 255, 255, 255);
        //background//
        //$white = imagecolorallocate($im, 255, 255, 255);
        
        $x = 50;
        $j = 0;
        
        foreach($lines as $i => $line) {
            //unknown-leftMargin-lineSpace//
            imagestring($im, 12, $x, 20 * $j, trim($line), $white);
            $j++;
            if($j == $nhalf) {
                $x = $width - 200;
                $j = 0;
            }
            
        }
        imagepng($im, $newpath);
        imagedestroy($im);
      
        return $newpath;
    }
    
    //chdir('C:\TOP-20');
    $newpath = convert($filename);
    echo "<center><img src='$newpath' height='auto' width='auto' alt='error'><br>";
    ?>
    

    It produces an image

    enter image description here