Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define("MAX_WIDTH_IMG",1000);
- define("MAX_HEIGHT_IMG",1000);
- if(isset($_GET['download'])) {
- if(!file_exists($_GET['download'])) {
- echo 'Error: The file not exist!';
- exit;
- }
- if(stripos($_GET['download'],'http') !== false || stripos($_GET['download'],'../') !== false || stripos($_GET['download'],'.html') === false) {
- echo 'Nope';
- exit;
- }
- header("Cache-Control: private");
- header("Content-Type: application/stream");
- header("Content-Length: ".filesize($_GET['download']));
- header("Content-Disposition: attachment; filename=".basename($_GET['download']));
- readfile($_GET['download']);
- unlink($_GET['download']);
- exit;
- }
- ?>
- <html>
- <head>
- <title>Image to HTML converter</title>
- <style>
- body { background-color:#eee; }
- .but_down { background-color:green; padding: 4px 10px 4px 10px; color:white; margin-top:30px; margin-left:10px; border-radius:11px; text-decoration:none; }
- *:focus { outline: none; }
- </style>
- <?php $raw_string = '
- <style>
- body { margin:0; }
- .col { width:'.(isset($_POST['res_w']) ? $_POST['res_w'] : '1').'px; height:'.(isset($_POST['res_h']) ? $_POST['res_h'] : '1').'px; float:left; }
- .row { clear: both; float: left; display: block; position: relative; }
- </style>
- ';
- echo $raw_string;
- $style_len = strlen($raw_string);
- ?>
- </head>
- <body>
- <?php
- set_time_limit(0);
- function getImage($filename) {
- if (!file_exists($filename))
- throw new Exception('Error: The file "'.$filename.'" not exist!');
- switch(strtolower(pathinfo($filename,PATHINFO_EXTENSION))) {
- case 'jpeg':
- case 'jpg':
- return imagecreatefromjpeg($filename);
- break;
- case 'png':
- return imagecreatefrompng($filename);
- break;
- case 'gif':
- return imagecreatefromgif($filename);
- case 'bmp':
- return imagecreatefrombmp($filename);
- break;
- case 'gd':
- return imagecreatefromgd($filename);
- break;
- case 'xpm':
- return imagecreatefromxpm($filename);
- break;
- case 'gd2':
- return imagecreatefromgd2($filename);
- break;
- case 'xbm':
- return imagecreatefromxbm($filename);
- break;
- case 'gif':
- return imagecreatefromgif($filename);
- break;
- case 'webp':
- return imagecreatefromwebp($filename);
- break;
- default:
- throw new Exception('Error: The file "'.$filename.'" is not valid format (valid formats: jpg, png or gif).');
- break;
- }
- }
- if(isset($_FILES['image']) && strlen($_FILES['image']['name']) > 0 && isset($_POST['res_w']) && isset($_POST['res_d']) && isset($_POST['res_h'])) {
- if(move_uploaded_file($_FILES['image']['tmp_name'], basename($_FILES['image']['name']))) {
- try {
- $img = getImage($_FILES['image']['name']);
- $maxx = imagesx($img); $maxy = imagesy($img);
- $file_s = filesize($_FILES['image']['name']);
- if($maxx > MAX_WIDTH_IMG || $maxy > MAX_HEIGHT_IMG) {
- echo "Error: You have exceeded the resolution limit! (MAX_WIDTH_IMG: ".MAX_WIDTH_IMG.", MAX_HEIGHT_IMG: ".MAX_HEIGHT_IMG;
- exit;
- }
- unlink($_FILES['image']['name']);
- } catch(Exception $err) {
- echo $err;
- exit;
- }
- } else {
- echo 'Error: Image was not been uploaded!';
- }
- $img = imagerotate($img , 90, 0);
- $img = imagerotate($img , $_POST['res_d'], 0);
- imagealphablending($img, false);
- imagesavealpha($img, true);
- $maxx = imagesx($img); $maxy = imagesy($img);
- for($x=1; $x < $maxx; $x++) {
- for($y=1; $y < $maxy; $y++) {
- $RGB = imagecolorat($img, $x, $y);
- if((($RGB >> 16) & 0xFF) + (($RGB >> 8) & 0xFF) + ($RGB & 0xFF) > 0)
- $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>' : '');
- else
- $temp = ($y == 1 ? '<div class="row">' : '')."<div class='col' style='background-color:#00800000;'></div>".($y == 1 ? '</div>' : '');
- echo $temp;
- $raw_string .= $temp;
- }
- }
- imagedestroy($img);
- if(strlen($raw_string) > $style_len) {
- if(!is_dir("html-images")) {
- if(!mkdir('html-images', 0777, true)) {
- echo "Error: Can't create html-images folder!";
- exit;
- }
- }
- $imagehtml = fopen('./html-images/'.$_FILES['image']['name'].".html", "w");
- if(!fwrite($imagehtml, $raw_string)) {
- echo 'Error: no file write access!';
- exit;
- }
- fclose($imagehtml);
- ?>
- <br>
- <div class="row" style="margin-top: 10px;">
- <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>
- <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>
- <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>
- <a class="but_down" style="margin-left:10px; margin-top:20px; color:white;" href="index.php">Back</a>
- <div>
- <?php
- }
- } else {
- ?>
- <form method="POST" enctype="multipart/form-data">
- <input class="but_down" type="file" name="image"><br>
- <input class="but_down" style="border:0; color:white;" name="res_w" value="1"> PX (WIDTH)<br>
- <input class="but_down" style="border:0; color:white;" name="res_h" value="1"> PX (HEIGHT)<br>
- <input class="but_down" style="border:0; color:white;" name="res_d" value="0"> ° degrees of image rotation<br>
- <input class="but_down" style="border:0;" type="submit" value="Upload Image" name="submit">
- </form>
- <?php
- }
- ?>
- <script>
- window.onload = function () {
- var rendTime = window.performance.timing.domContentLoadedEventEnd-window.performance.timing.navigationStart;
- rend_sec.innerHTML = rendTime/1000 + ' seconds';
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement