Advertisement
Guest User

Image to HTML converter [PHP] - Scriptery

a guest
Dec 28th, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.04 KB | None | 0 0
  1. <?php
  2. define("MAX_WIDTH_IMG",1000);
  3. define("MAX_HEIGHT_IMG",1000);
  4.  
  5. if(isset($_GET['download'])) {
  6.     if(!file_exists($_GET['download'])) {
  7.         echo 'Error: The file not exist!';
  8.         exit;
  9.     }
  10.     if(stripos($_GET['download'],'http') !== false || stripos($_GET['download'],'../') !== false || stripos($_GET['download'],'.html') === false) {
  11.         echo 'Nope';
  12.         exit;
  13.     }
  14.     header("Cache-Control: private");
  15.     header("Content-Type: application/stream");
  16.     header("Content-Length: ".filesize($_GET['download']));
  17.     header("Content-Disposition: attachment; filename=".basename($_GET['download']));      
  18.     readfile($_GET['download']);
  19.     unlink($_GET['download']);
  20.     exit;
  21. }
  22. ?>
  23. <html>
  24.     <head>
  25.         <title>Image to HTML converter</title>
  26.         <style>
  27.             body { background-color:#eee; }
  28.             .but_down { background-color:green; padding: 4px 10px 4px 10px; color:white; margin-top:30px; margin-left:10px; border-radius:11px; text-decoration:none; }
  29.             *:focus { outline: none; }
  30.         </style>
  31.         <?php $raw_string = '
  32.         <style>
  33.             body { margin:0; }
  34.             .col { width:'.(isset($_POST['res_w']) ? $_POST['res_w'] : '1').'px; height:'.(isset($_POST['res_h']) ? $_POST['res_h'] : '1').'px; float:left; }
  35.             .row { clear: both; float: left; display: block; position: relative; }
  36.         </style>
  37.         ';
  38.         echo $raw_string;
  39.         $style_len = strlen($raw_string);
  40.         ?>
  41.     </head>
  42.     <body>
  43.         <?php
  44.         set_time_limit(0);
  45.         function getImage($filename) {
  46.             if (!file_exists($filename))
  47.                 throw new Exception('Error: The file "'.$filename.'" not exist!');
  48.             switch(strtolower(pathinfo($filename,PATHINFO_EXTENSION))) {
  49.                 case 'jpeg':
  50.                 case 'jpg':
  51.                     return imagecreatefromjpeg($filename);
  52.                 break;
  53.                 case 'png':
  54.                     return imagecreatefrompng($filename);
  55.                 break;
  56.                 case 'gif':
  57.                     return imagecreatefromgif($filename);
  58.                 case 'bmp':
  59.                     return imagecreatefrombmp($filename);
  60.                 break;
  61.                 case 'gd':
  62.                     return imagecreatefromgd($filename);
  63.                 break;
  64.                 case 'xpm':
  65.                     return imagecreatefromxpm($filename);
  66.                 break;
  67.                 case 'gd2':
  68.                     return imagecreatefromgd2($filename);
  69.                 break;
  70.                 case 'xbm':
  71.                     return imagecreatefromxbm($filename);
  72.                 break;
  73.                 case 'gif':
  74.                     return imagecreatefromgif($filename);
  75.                 break;
  76.                 case 'webp':
  77.                     return imagecreatefromwebp($filename);
  78.                 break;
  79.                 default:
  80.                     throw new Exception('Error: The file "'.$filename.'" is not valid format (valid formats: jpg, png or gif).');
  81.                 break;
  82.             }
  83.         }
  84.         if(isset($_FILES['image']) && strlen($_FILES['image']['name']) > 0 && isset($_POST['res_w']) && isset($_POST['res_d']) && isset($_POST['res_h'])) {
  85.             if(move_uploaded_file($_FILES['image']['tmp_name'], basename($_FILES['image']['name']))) {
  86.                 try {
  87.                     $img = getImage($_FILES['image']['name']);
  88.                     $maxx = imagesx($img); $maxy = imagesy($img);
  89.                     $file_s = filesize($_FILES['image']['name']);
  90.                     if($maxx > MAX_WIDTH_IMG || $maxy > MAX_HEIGHT_IMG) {
  91.                         echo "Error: You have exceeded the resolution limit! (MAX_WIDTH_IMG: ".MAX_WIDTH_IMG.", MAX_HEIGHT_IMG: ".MAX_HEIGHT_IMG;
  92.                         exit;
  93.                     }
  94.                     unlink($_FILES['image']['name']);
  95.                 } catch(Exception $err) {
  96.                     echo $err;
  97.                     exit;
  98.                 }
  99.             } else {
  100.                 echo 'Error: Image was not been uploaded!';
  101.             }  
  102.             $img = imagerotate($img , 90, 0);
  103.             $img = imagerotate($img , $_POST['res_d'], 0);
  104.             imagealphablending($img, false);
  105.             imagesavealpha($img, true);
  106.             $maxx = imagesx($img); $maxy = imagesy($img);
  107.             for($x=1; $x < $maxx; $x++) {
  108.                 for($y=1; $y < $maxy; $y++) {
  109.                     $RGB = imagecolorat($img, $x, $y);
  110.                     if((($RGB >> 16) & 0xFF) + (($RGB >> 8) & 0xFF) + ($RGB & 0xFF) > 0)
  111.                         $temp = ($y == 1 ? '<div class="row">' : '')."<div class='col' style='background-color:rgb(".(($RGB >> 16) & 0xFF).",".(($RGB >> 8) & 0xFF).",".($RGB & 0xFF).");'></div>".($y == 1 ? '</div>' : '');
  112.                     else
  113.                         $temp = ($y == 1 ? '<div class="row">' : '')."<div class='col' style='background-color:#00800000;'></div>".($y == 1 ? '</div>' : '');  
  114.                     echo $temp;
  115.                     $raw_string .= $temp;
  116.                 }
  117.             }
  118.             imagedestroy($img);
  119.             if(strlen($raw_string) > $style_len) {
  120.                 if(!is_dir("html-images")) {
  121.                     if(!mkdir('html-images', 0777, true)) {
  122.                         echo "Error: Can't create html-images folder!";
  123.                         exit;
  124.                     }
  125.                 }
  126.                 $imagehtml = fopen('./html-images/'.$_FILES['image']['name'].".html", "w");
  127.                 if(!fwrite($imagehtml, $raw_string)) {
  128.                     echo 'Error: no file write access!';
  129.                     exit;
  130.                 }
  131.                 fclose($imagehtml);
  132.                 ?>
  133.                 <br>
  134.                 <div class="row" style="margin-top: 10px;">
  135.                     <a style="margin-left:10px; margin-top:20px; color:green;">Size: <?php echo $file_s; ?> bytes | Size in html format: <?php echo filesize('./html-images/'.$_FILES['image']['name'].".html"); ?> bytes</a><br>
  136.                     <a style="margin-left:10px; margin-top:20px; color:green;">Number of pixels/divs: <?php echo $maxx*$maxy; ?> | render time: <a style='color:green;' id="rend_sec"></a></a><br><br>
  137.                     <a onclick="down_hid.style.display='none';" id="down_hid" traget="_blank" class="but_down" href="?download=<?php echo 'html-images/'.$_FILES['image']['name'].".html"; ?>">Download</a>
  138.                     <a class="but_down" style="margin-left:10px; margin-top:20px; color:white;" href="index.php">Back</a>
  139.                 <div>
  140.                 <?php
  141.             }
  142.         } else {
  143.             ?>
  144.             <form method="POST" enctype="multipart/form-data">
  145.                 <input class="but_down" type="file" name="image"><br>
  146.                 <input class="but_down" style="border:0; color:white;" name="res_w" value="1"> PX (WIDTH)<br>
  147.                 <input class="but_down" style="border:0; color:white;" name="res_h" value="1"> PX (HEIGHT)<br>
  148.                 <input class="but_down" style="border:0; color:white;" name="res_d" value="0"> &#176; degrees of image rotation<br>
  149.                 <input class="but_down" style="border:0;" type="submit" value="Upload Image" name="submit">
  150.             </form>
  151.             <?php
  152.         }
  153.         ?>
  154.         <script>
  155.             window.onload = function () {
  156.                var rendTime = window.performance.timing.domContentLoadedEventEnd-window.performance.timing.navigationStart;
  157.                rend_sec.innerHTML = rendTime/1000 + ' seconds';
  158.             }
  159.         </script>
  160.     </body>
  161. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement