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…

You can see this script in action now: http://skycube.net/demos/image2html/

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

  6. Mike
    August 6th, 2010 at 15:52 | #6

    Hi, very usefull script, it´s possible change the RBG color to #HEX?

  7. August 10th, 2010 at 10:54 | #7

    @Mike
    Yes it’s possible, but probably it will slow down. Cause the output of image functions is rgb and you’ve to use a convert function like this:
    http://www.anyexample.com/programming/php/php_convert_rgb_from_to_html_hex_color.xml

    <?php
    function rgb2html($r, $g=-1, $b=-1)
    {
        if (is_array($r) && sizeof($r) == 3)
            list($r, $g, $b) = $r;
     
        $r = intval($r); $g = intval($g);
        $b = intval($b);
     
        $r = dechex($r<0?0:($r>255?255:$r));
        $g = dechex($g<0?0:($g>255?255:$g));
        $b = dechex($b<0?0:($b>255?255:$b));
     
        $color = (strlen($r) < 2?'0':'').$r;
        $color .= (strlen($g) < 2?'0':'').$g;
        $color .= (strlen($b) < 2?'0':'').$b;
        return '#'.$color;
    }
    ?>
  8. May 2nd, 2012 at 07:23 | #8

    How many are there in a book? http://ykyufuyak.blog.free.fr/ nude bbs kd pics I don’t care how many times its been posted, its awesome, and um why would you watch it a second time if you think it shouldn’t be reposted? lmmfao bunch of no count morons

  9. May 5th, 2012 at 01:28 | #9

    There’s a three month trial period http://ipiaiqutim.de.tl forum teen model you wouldnt notice someone getting head right up stairs…i love porn acting great stuff they should go on broadway…love how i just criticed porn

  10. May 5th, 2012 at 03:37 | #10

    Could I borrow your phone, please? http://temyjisayr.de.tl preteen asian girls Fucking LOVE the body on this blonde – and a super pussy and asshole to eat out. Wouldn’t mind fucking the guy either for that matter -

  11. May 5th, 2012 at 03:37 | #11

    I want to make a withdrawal http://eymunifobay.de.tl preteen underage child uh…her pussy look gross ikw its pussy pumping but i never liked it….but i can say the deepthroating got me horny so good job i guess…

  1. No trackbacks yet.