Advertisement
Guest User

Generate HTML code from IMAGE

a guest
Sep 26th, 2015
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. <?PHP
  2. set_time_limit( 3600 );
  3.  
  4. $pathtoImage = 'img2.png'; /* Can be ANY PNG image! */
  5. /* Due to the large output size, only a PART of the image should be converted to HTML code */
  6. $startWidth = 250;
  7. $startHeight = 250;
  8. $width = 101; /* (startWidth + width) < actualimageWidth */
  9. $height = 101; /* (startHeight + height) < actualimageHeight */
  10.  
  11. /* Don't edit below this line */
  12. $im = @imagecreatefrompng( $pathtoImage );
  13.  
  14. unlink( 'image1.html' );
  15. unlink( 'image2.html' );
  16. unlink( 'image3.html' );
  17. $fid1 = fopen( 'image1.html', 'w+');
  18. $fid2 = fopen( 'image2.html', 'w+');
  19. $fid3 = fopen( 'image3.html', 'w+');
  20. fwrite( $fid1, '<!doctype html><html><head><meta charset="utf-8"><title></title></head><body>' );
  21. fwrite( $fid2, '<!doctype html><html><head><meta charset="utf-8"><title></title></head><body>' );
  22. fwrite( $fid3, '<!doctype html><html><head><meta charset="utf-8"><title></title></head><body>' );
  23.  
  24. $i = 0;
  25. if( ($startWidth + $width) > imagesx( $im ) ) echo 'Please lower the number for "$startWidth + $width"<br />';
  26. if( ($startHeight + $height) > imagesx( $im ) ) echo 'Please lower the number for "$startWidth + $width"<br />';
  27. fwrite( $fid1, '<style type="text/css">.i{width:' . ($width-1) . 'px;height:' . ($height-1) . 'px;}' );
  28. fwrite( $fid2, '<style type="text/css">.i{width:' . ($width-1) . 'px;height:' . ($height-1) . 'px;}' );
  29. fwrite( $fid3, '<style type="text/css">.i{width:' . ($width-1) . 'px;height:' . ($height-1) . 'px;}' );
  30. for( $x = $startWidth; $x < ($startWidth+$width)-1; $x++ ) {
  31. for( $y = $startHeight; $y < ($startHeight+$height)-1; $y++ ) {
  32. $p = imagecolorat( $im, $x, $y );
  33. $a = imagecolorsforindex( $im, $p );
  34. $a = dechex( $a['red'] ) . dechex( $a['green'] ) . dechex( $a['blue'] );
  35. fwrite( $fid1, '#a' . $i++ . '{background-color:#' . $a . ';float:left;height:1px;width:1px;}' );
  36. fwrite( $fid2, '#a' . $i . '{background-color:#' . $a . ';}' );
  37.  
  38. if( !isset( $b[$a] ) )
  39. $b[$a] = 1;
  40. else
  41. $b[$a]++;
  42. } }
  43.  
  44. $i = 0;
  45. foreach( $b AS $colour => $times ) {
  46. if( $times == 1 ) {
  47. $b[ $colour ] = 'id="a' . $i . '" class="d"';
  48. fwrite( $fid3, '#a' . $i++ . '{background-color:#' . $colour . ';}' );
  49. }else{
  50. $b[ $colour ] = 'class="a' . $i . ' d"';
  51. fwrite( $fid3, '.a' . $i++ . '{background-color:#' . $colour . ';}' );
  52. } }
  53.  
  54. $i = 0;
  55. $html1 = '<div class="i">';
  56. $html2 = '<div class="i">';
  57. $html3 = '<div class="i">';
  58. for( $x = $startWidth; $x < ($startWidth+$width)-1; $x++ ) {
  59. for( $y = $startHeight; $y < ($startHeight+$height)-1; $y++ ) {
  60. $p = imagecolorat( $im, $x, $y );
  61. $a = imagecolorsforindex( $im, $p );
  62. $a = dechex( $a['red'] ) . dechex( $a['green'] ) . dechex( $a['blue'] );
  63.  
  64. $html1 .= '<div id="a' . $i . '" class="d"></div>';
  65. $html2 .= '<div id="a' . $i++ . '"></div>';
  66. $html3 .= '<div ' . $b[$a] . '></div>';
  67. }
  68. }
  69.  
  70. fwrite( $fid2, ".d{float:left;height:1px;width:1px;}");
  71. fwrite( $fid3, ".d{float:left;height:1px;width:1px;}");
  72. fwrite( $fid1, '</style>' . $html1 . '</div></body></html>' );
  73. fwrite( $fid2, '</style>' . $html2 . '</div></body></html>' );
  74. fwrite( $fid3, '</style>' . $html3 . '</div></body></html>' );
  75.  
  76. fclose( $fid1 );
  77. fclose( $fid2 );
  78. fclose( $fid3 );
  79. imagedestroy($im);
  80.  
  81. echo '<a href="./image1.html">image1.html</a></br>';
  82. echo '<a href="./image1.html">image2.html</a></br>';
  83. echo '<a href="./image1.html">image3.html</a></br>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement