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?

 

 

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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' => 'john.doe@gmail.com'
                ),
            array (
                'name' => 'Mary',
                'score' => 21789,
                'age' => 21,
                'email' => 'maryx@amazon.com'
                ),
            array (
                'name' => 'Rumpelstiltskin',
                'score' => 2345,
                'age' => 101,
                'email' => 'rumpel@gmail.com'
                ),
            array (
                'name' => 'Sarah',
                'score' => 345,
                'age' => 25,
                'email' => 'sarah123@aol.com'
                )
);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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