liidi Posted March 2, 2007 Share Posted March 2, 2007 Is there any easy way to make this convertion to be happend ??? Link to comment https://forums.phpfreaks.com/topic/40871-convert-html-tableor-array-to-image/ Share on other sites More sharing options...
redarrow Posted March 2, 2007 Share Posted March 2, 2007 Yep can be done but your need to use the gd libbary ok. Link to comment https://forums.phpfreaks.com/topic/40871-convert-html-tableor-array-to-image/#findComment-197910 Share on other sites More sharing options...
liidi Posted March 2, 2007 Author Share Posted March 2, 2007 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 https://forums.phpfreaks.com/topic/40871-convert-html-tableor-array-to-image/#findComment-197925 Share on other sites More sharing options...
redarrow Posted March 2, 2007 Share Posted March 2, 2007 http://uk2.php.net/gd Link to comment https://forums.phpfreaks.com/topic/40871-convert-html-tableor-array-to-image/#findComment-198018 Share on other sites More sharing options...
Barand Posted March 2, 2007 Share Posted March 2, 2007 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 Link to comment https://forums.phpfreaks.com/topic/40871-convert-html-tableor-array-to-image/#findComment-198202 Share on other sites More sharing options...
liidi Posted March 4, 2007 Author Share Posted March 4, 2007 Possible using gd? Yes Easy? No. Jeah, somehow I have noticed this So mayby it's time to start hard coding now Link to comment https://forums.phpfreaks.com/topic/40871-convert-html-tableor-array-to-image/#findComment-199031 Share on other sites More sharing options...
Barand Posted March 4, 2007 Share Posted March 4, 2007 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 https://forums.phpfreaks.com/topic/40871-convert-html-tableor-array-to-image/#findComment-199068 Share on other sites More sharing options...
liidi Posted March 4, 2007 Author Share Posted March 4, 2007 thx, Barand. I also working almost same kind of array only more information. I was surprised that I also get something to work before you show that, but mine was not so beautiful than yours Link to comment https://forums.phpfreaks.com/topic/40871-convert-html-tableor-array-to-image/#findComment-199264 Share on other sites More sharing options...
Barand Posted March 4, 2007 Share Posted March 4, 2007 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]' ) ); Link to comment https://forums.phpfreaks.com/topic/40871-convert-html-tableor-array-to-image/#findComment-199281 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.