Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $start = count($_FILES) === 0;
- $img_src;
- if (count($_FILES)) {
- $file = $_FILES["thefile"];
- extract($file);
- convert($name, $tmp_name, $size);
- }
- ?><!DOCTYPE html>
- <html>
- <head>
- <meta charsef="utf-8" />
- <title>Convert any file to an image!</title>
- </head>
- <body>
- <?php
- if ($start) {
- ?>
- <p>Select a file:</p>
- <form method="post" enctype="multipart/form-data">
- <input type="file" name="thefile" />
- <input type="submit" />
- </form>
- <?php
- }
- else echo "<img src='$img_src' />";
- ?>
- </body>
- </html><?php
- function convert($name, $f, $size) {
- global $img_src;
- $img_src = "$name.png";
- $bytes = intval($size/3) + 4;
- $root = sqrt($bytes);
- $h = 0;
- $w = 0;
- for ($h = intval($root); $h > 1; $h--) {
- $w = intval($bytes / $h);
- if ($w * $h === $bytes) break;
- }
- if ($w / $h > 10) {
- $w = intval($root * 1.5);
- $h = intval($bytes / $w) + 1;
- }
- $f = fopen($f, 'r');
- $im = imagecreatetruecolor($w, $h);
- $r = 0;
- $g = 0;
- $b = 0;
- $c = 0;
- for ($y = 0; $y < $h; $y++) {
- for ($x = 0; $x < $w; $x++) {
- if ($x === 0 && $y === 0) {
- $r = intval($size / 65536);
- $g = intval($size / 256) % 256;
- $b = $size % 256;
- }
- else {
- $r = ord(fread($f, 1));
- $g = ord(fread($f, 1));
- $b = ord(fread($f, 1));
- }
- $c = imagecolorallocate($im, $r, $g, $b);
- imagesetpixel($im, $x, $y, $c);
- }
- }
- imagepng($im, $img_src);
- imagedestroy($im);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement