Jump to content

convert html table(or array) to image


liidi

Recommended Posts

Yep can be done but your need to use the gd libbary ok.

thx, for the support  ;)

I have tried to find some tutorials/sambles about that drawing thing but I can't find them, is there?

 

GD -librady is not very common for me, so can you bush me to right direction?

 

 

Possible using gd? Yes

Easy? No.

 

Your browser contains a lot of code to calculate table layouts, cell widths and heights, text wrapping, text alignment, borders, padding etc etc etc. If you want to do it with a gd image then you'll have to do it all yourself

 

 

To get you started with a simple example

[pre]

+-----------------+---------+

|    Name        |  Score  |

+-----------------+---------+

| John            |  31004 |

| Mary            |  21789 |

| Rumpelstiltskin |    2345 |

+-----------------+---------+

[/pre]

 

Page to display the image

<?php
session_start();
$data = array (
            array (
                'name' => 'John',
                'score' => 31004
                ),
            array (
                'name' => 'Mary',
                'score' => 21789
                ),
            array (
                'name' => 'Rumpelstiltskin',
                'score' => 2345
                )
);
$_SESSION['imagedata'] = $data;
echo "<IMG src='mytableimage.php'>"

?>

 

:: mytableimage.php ::

<?php
session_start();
$data = $_SESSION['imagedata'];
/**
* get max widths for each column
*/
$cols = array() ;
$colvals = array();
$count = 0;
foreach ($data as $row ) { 
    foreach ($row as $field => $val) {
        if (!isset($cols[$field])) {
            $cols[$field] = strlen($val);
        }
        else {
            $cols[$field] = max($cols[$field], strlen($val));
        }
        $colvals[$count][] = $val;
    }
    $count++;
}
/**
* calculate text widths in pixels
*/
$pad = 4;
$colnames = array_keys($cols);
$colwidths = array_values($cols);
foreach ($colwidths as $k=>$v) {
    $colwidths[$k] = $v * imagefontwidth(3) + 2 * $pad;
}
/**
* calc image size and create
*/
$rowheight = imagefontheight(3) + 2 * $pad;
$numrows = count ($data) + 1;
$ih = $numrows * $rowheight;
$iw = array_sum ($colwidths);

$im = imagecreate($iw, $ih);
$bg = imagecolorallocate($im, 0xAA, 0xD0, 0xD0);
$bdr = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$tcol = imagecolorallocate($im, 0,0,0);

/**
* draw cell borders 
*/
$x = $y = 0;
for ($y=0; $y < $ih; $y += $rowheight) imageline($im, 0, $y, $iw, $y, $bdr);
for ($i=0, $n=count($colnames); $i <= $n; $i++) {
    imageline($im, $x, 0, $x, $ih, $bdr);
    $x += $colwidths[$i];
}

/**
* headings 
*/
foreach ($colnames as $col=>$text) {
    $x = textCenter($col, $text, $pad, 3, $colwidths);
    $y = $pad;
    imagestring($im, 3, $x, $y, $text, $tcol);
}
/**
* data
*/
foreach ($colvals as $row=>$items) {
    foreach ($items as $col=>$text) {
        $y = ($row+1) * $rowheight + $pad;
        $x = is_numeric($text) ? textRight($col,$text,$pad,3,$colwidths) : textLeft($col,$text,$pad,3,$colwidths);
        imagestring($im, 3, $x, $y, $text, $tcol);
    }
}
header ("content-type: image/png");
imagepng($im);
imagedestroy($im);

/******************************************
* text positioning functions
*******************************************/
function textLeft ($col, $text, $pad, $font, &$colwidths) {
     $x = array_sum(array_slice($colwidths, 0, $col)) + $pad;
     return $x;
}
function textRight ($col, $text, $pad, $font, &$colwidths) {
     $tw = strlen($text) * imagefontwidth($font);
     $x = array_sum(array_slice($colwidths, 0, $col+1)) - $pad - $tw;
     return $x;
}
function textCenter ($col, $text, $pad, $font, &$colwidths) {
     $tw = strlen($text) * imagefontwidth($font);
     $x1 = array_sum(array_slice($colwidths, 0, $col)) ;
     $x2 = array_sum(array_slice($colwidths, 0, $col+1)) ;
     return ($x1 + $x2 - $tw)/2;
}
?>

I tried to make mine flexible so if you add extra data to the arrays it should accept them. So if you change data to this, say, it still works

 

$data = array (
            array (
                'name' => 'John',
                'score' => 31004,
                'age' => 31,
                'email' => '[email protected]'
                ),
            array (
                'name' => 'Mary',
                'score' => 21789,
                'age' => 21,
                'email' => '[email protected]'
                ),
            array (
                'name' => 'Rumpelstiltskin',
                'score' => 2345,
                'age' => 101,
                'email' => '[email protected]'
                ),
            array (
                'name' => 'Sarah',
                'score' => 345,
                'age' => 25,
                'email' => '[email protected]'
                )
);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.