Solutions for Go, MySQL, PHP, Linux and more

Image to html via php

Posted — Feb 14, 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:

.tableImage { padding: 0; margin: 0; border: 0; }
.tableImage TD { width: 1px; height: 1px; padding: 0; margin: 0; }

The php code:

$im = imagecreatefromjpeg($image);
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>";
	$html .= "</tr>";

Begin of the output table:

<table class="tableImage" cellpadding="0" cellspacing="0">

Include the generated image:

<?=$html ?>

End of output table:


Please note, that this could crash your web-server or browser, if you use to large images;

You can see this script in action now: