Home > Projects > Image to html via php

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…

Projects , ,

  1. February 24th, 2009 at 00:19 | #1

    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

  2. April 7th, 2009 at 16:29 | #2

    Crazy idea – never thought about it because I wasn´t aware of that it´s possible…. now I´m thinking about a usecase :)

  3. April 12th, 2009 at 17:17 | #3

    I looking forward to find one as well,… Beside, I try to optimize the script for faster loading.

  4. ayaz
    September 8th, 2009 at 17:01 | #4

    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?

  5. September 11th, 2009 at 12:23 | #5

    Using a screenshot program ;) Or print to a pdf file… may that works

  1. No trackbacks yet.