Advertisement
Guest User

file2pic

a guest
Nov 28th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1. <?php
  2.  
  3. $start = count($_FILES) === 0;
  4.  
  5. $img_src;
  6.  
  7. if (count($_FILES)) {
  8.     $file = $_FILES["thefile"];
  9.     extract($file);
  10.     convert($name, $tmp_name, $size);
  11. }
  12.  
  13. ?><!DOCTYPE html>
  14. <html>
  15.     <head>
  16.         <meta charsef="utf-8" />
  17.         <title>Convert any file to an image!</title>
  18.     </head>
  19.     <body>
  20.     <?php
  21.         if ($start) {
  22.         ?>
  23.         <p>Select a file:</p>
  24.         <form method="post" enctype="multipart/form-data">
  25.             <input type="file" name="thefile" />
  26.             <input type="submit" />
  27.         </form>
  28.         <?php
  29.         }
  30.         else echo "<img src='$img_src' />";
  31.     ?>
  32.     </body>
  33. </html><?php
  34.  
  35. function convert($name, $f, $size) {
  36.     global $img_src;
  37.    
  38.     $img_src = "$name.png";
  39.  
  40.     $bytes = intval($size/3) + 4;
  41.     $root = sqrt($bytes);
  42.     $h = 0;
  43.     $w = 0;
  44.     for ($h = intval($root); $h > 1; $h--) {
  45.         $w = intval($bytes / $h);
  46.         if ($w * $h === $bytes) break;
  47.     }
  48.    
  49.     if ($w / $h > 10) {
  50.         $w = intval($root * 1.5);
  51.         $h = intval($bytes / $w) + 1;
  52.     }
  53.    
  54.    
  55.     $f = fopen($f, 'r');
  56.    
  57.     $im = imagecreatetruecolor($w, $h);
  58.    
  59.     $r = 0;
  60.     $g = 0;
  61.     $b = 0;
  62.     $c = 0;
  63.     for ($y = 0; $y < $h; $y++) {
  64.         for ($x = 0; $x < $w; $x++) {
  65.             if ($x === 0 && $y === 0) {
  66.                 $r = intval($size / 65536);
  67.                 $g = intval($size / 256) % 256;
  68.                 $b = $size % 256;
  69.             }
  70.             else {
  71.                 $r = ord(fread($f, 1));
  72.                 $g = ord(fread($f, 1));
  73.                 $b = ord(fread($f, 1));
  74.             }
  75.            
  76.             $c = imagecolorallocate($im, $r, $g, $b);
  77.             imagesetpixel($im, $x, $y, $c);
  78.         }
  79.     }
  80.    
  81.     imagepng($im, $img_src);
  82.     imagedestroy($im);
  83. }
  84. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement