Image to html via php
February 14th, 2009
Did you ever thought about to show an image on a website using a table?
The following script analyze the image and does a print out using a standard html table.
The CSS code:
1 2 3 4 | <style type="text/css" media="screen"> .tableImage { padding: 0; margin: 0; border: 0; } .tableImage TD { width: 1px; height: 1px; padding: 0; margin: 0; } </style> |
The php code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php $image='sample.jpg'; $im = imagecreatefromjpeg($image); $info=getimagesize($image); $width=$info[0]; $height=$info[1]; for ($i=0; $i<$height; $i++){ $html .= "<tr>"; for ($j=0; $j<$width; $j++){ $color_index = imagecolorat($im, $j, $i); $color_tran = imagecolorsforindex($im, $color_index); $html .= "<td style=\"background-color: rgb($color_tran[red],$color_tran[green],$color_tran[blue]);\"></td>\n"; } $html .= "</tr>\n"; } ?> |
Begin of the output table:
1 | <table class="tableImage" cellpadding="0" cellspacing="0"> |
Include the generated image:
1 | <?=$html ?> |
End of output table:
1 | </table> |
Please note, that this could crash your web-server or browser, if you use to large images…
Hi!
I’m a beginner in php programming, but your script helped me so much (in an other way), that I must say thank you
Greg
Crazy idea – never thought about it because I wasn´t aware of that it´s possible…. now I´m thinking about a usecase
I looking forward to find one as well,… Beside, I try to optimize the script for faster loading.
Hello
How can we create image from html i.e. I have a DIV tag and i want to take snapshot of contents in that div…how can we acheive it?
Using a screenshot program
Or print to a pdf file… may that works