Name: Anonymous 2008-06-04 10:39
GO!
#!/usr/bin/env php
<?php
$opts = getopt("hf:o:");
if (isset($opts['h']) || !isset($opts['f'])) {
$usage = <<<EOD
Usage: $argv[0] [-h] -f file.bin [-o file.png]
EOD;
fwrite(STDERR, $usage);
die();
}
$f = @ file_get_contents($opts['f']);
if (!$f) {
fwrite(STDERR, "Invalid file.\n");
die();
}
$len = strlen($f);
$data = $len . "\0" . basename($opts['f']) . "\0" . $f;
$size = ceil(sqrt(strlen($data) / 3));
$img = imagecreatetruecolor($size, $size);
for ($a = 0; $a < $size; ++$a) {
for ($b = 0; $b < $size; ++$b) {
if (isset( $data[((($a * $size) + $b) * 3) ])) {
$R = ord($data[((($a * $size) + $b) * 3) ]);
} else {
$R = 255;
}
if (isset( $data[((($a * $size) + $b) * 3) + 1])) {
$G = ord($data[((($a * $size) + $b) * 3) + 1]);
} else {
$G = 255;
}
if (isset( $data[((($a * $size) + $b) * 3) + 2])) {
$B = ord($data[((($a * $size) + $b) * 3) + 2]);
} else {
$B = 255;
}
$c = imagecolorallocate($img, $R, $G, $B);
imagesetpixel($img, $b, $a, $c);
}
}
header ("Content-type: image/png");
if (!isset($opts['o']) || empty($opts['o'])) {
$opts['o'] = null;
} else {
echo "Writing image to ${opts['o']}.\n";
}
imagepng($img, $opts['o'], 9);
?>#!/usr/bin/env php
<?php
$opts = getopt("hf:o");
if (isset($opts['h']) || !isset($opts['f'])) {
$usage = <<<EOD
Usage: $argv[0] [-h] -f file.bin [-o]
o: Extracts file to original filename instead of stdout.
EOD;
fwrite(STDERR, $usage);
die();
}
$img = @ imagecreatefrompng($opts['f']);
if (!$img) {
fwrite(STDERR, "Invalid file.\n");
die();
}
$x = imagesx($img);
$y = imagesy($img);
$o = "";
for ($a = 0; $a !== $x; ++$a) {
for ($b = 0; $b !== $y; ++$b) {
$c = imagecolorat($img, $b, $a);
$o .= chr(($c >> 16) & 0xFF) . chr(($c >> 8 ) & 0xFF) . chr($c & 0xFF);
}
}
list($l, $name, $f) = explode("\0", $o, 3);
if (!is_numeric($l) || empty($name) || empty($f)) {
fwrite(STDERR, "Invalid file.");
die();
}
if (isset($opts['o'])) {
echo "Writing $l KiB to $name.\n";
file_put_contents($name, substr($f, 0, $l));
} else {
echo substr($f, 0, $l);
}
?>