2010-12-28 20:26:46 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This class will be able to convert a file into an image.
|
|
|
|
* @author JPT
|
|
|
|
*/
|
|
|
|
class FileImageConverter {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var BoxImage
|
|
|
|
*/
|
|
|
|
protected $image;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var CharColorConverter
|
|
|
|
*/
|
|
|
|
protected $colorConverter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$this->colorConverter = new CharColorConverter();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes the given file, returns the filename of the resulting PNG-File.
|
|
|
|
* @param string $filename
|
|
|
|
* @return void
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function convertFile($filename) {
|
|
|
|
if(file_exists($filename) === FALSE) throw new Exception("Invalid filename given!");
|
|
|
|
|
|
|
|
//get file data
|
|
|
|
$filesize = filesize($filename);
|
|
|
|
$md5 = md5_file($filename);
|
|
|
|
|
2010-12-28 20:53:23 +01:00
|
|
|
$new_filename = $filename."-converted.png";
|
2010-12-28 20:26:46 +01:00
|
|
|
|
2010-12-28 20:51:36 +01:00
|
|
|
//some gradient
|
|
|
|
$fancy_header = "";
|
2010-12-28 20:26:46 +01:00
|
|
|
for($i = 0;$i < 255;$i += 5) {
|
2010-12-28 20:51:36 +01:00
|
|
|
$fancy_header .= chr($i);
|
2010-12-28 20:26:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//header done, now read the file content
|
|
|
|
$content = file_get_contents($filename);
|
|
|
|
|
2010-12-28 20:51:36 +01:00
|
|
|
//get final minimum size for the image
|
|
|
|
//(do not mix up strlen of $filesize and the actual $filesize)
|
|
|
|
$size = strlen($fancy_header) + 1 + strlen($filesize) + 1 + strlen($md5) + 1 + $filesize + 1;
|
2010-12-28 20:26:46 +01:00
|
|
|
|
|
|
|
//create and prepare image
|
|
|
|
$this->image = new BoxImage($size);
|
2010-12-28 20:51:36 +01:00
|
|
|
$this->image->fillImage(0,0,0,0);
|
2010-12-28 20:26:46 +01:00
|
|
|
|
2010-12-28 20:51:36 +01:00
|
|
|
//some fancy header
|
|
|
|
$this->addDataToImage($fancy_header);
|
|
|
|
|
|
|
|
//magic pixel
|
|
|
|
$this->image->setCurrentPixel(0,0,0,127);
|
|
|
|
$this->image->movePointer(1);
|
|
|
|
|
|
|
|
//add filesize
|
|
|
|
$this->addDataToImage($filesize);
|
|
|
|
|
|
|
|
//magic pixel
|
|
|
|
$this->image->setCurrentPixel(0,0,0,127);
|
|
|
|
$this->image->movePointer(1);
|
|
|
|
|
|
|
|
//add md5 value of content
|
|
|
|
$this->addDataToImage($md5);
|
2010-12-28 20:26:46 +01:00
|
|
|
|
|
|
|
//magic pixel
|
|
|
|
$this->image->setCurrentPixel(0,0,0,127);
|
|
|
|
$this->image->movePointer(1);
|
|
|
|
|
|
|
|
//content
|
|
|
|
$this->addDataToImage($content);
|
|
|
|
|
2010-12-28 20:51:36 +01:00
|
|
|
//magic pixel
|
|
|
|
$this->image->setCurrentPixel(0,0,0,127);
|
|
|
|
$this->image->movePointer(1);
|
|
|
|
|
2010-12-28 20:26:46 +01:00
|
|
|
//save image
|
2010-12-28 23:04:54 +01:00
|
|
|
$this->image->writeToFile($new_filename, 9);
|
2010-12-28 20:26:46 +01:00
|
|
|
|
|
|
|
//clean up
|
|
|
|
unset($this->image);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts data and adds it to the image.
|
|
|
|
* @param string $data
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function addDataToImage($data) {
|
|
|
|
try {
|
|
|
|
$strlen = strlen($data);
|
|
|
|
for($i = 0;$i < $strlen; $i++) {
|
|
|
|
$char = substr($data, $i, 1);
|
|
|
|
//convert char to color
|
|
|
|
list($r, $g, $b) = $this->colorConverter->charToColor($char);
|
|
|
|
//set the pixel
|
|
|
|
$this->image->setCurrentPixel($r, $g, $b, 0);
|
|
|
|
//move the pointer
|
|
|
|
$this->image->movePointer(1);
|
2010-12-28 20:41:27 +01:00
|
|
|
$this->progressBar($i, $strlen);
|
2010-12-28 20:26:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
//TODO: handle weird shit?
|
|
|
|
echo "Caught Exception:\n";
|
|
|
|
echo $e;
|
|
|
|
}
|
2010-12-28 20:51:36 +01:00
|
|
|
echo "\rDone. \n";
|
2010-12-28 20:41:27 +01:00
|
|
|
}
|
2010-12-28 20:26:46 +01:00
|
|
|
|
2010-12-28 20:41:27 +01:00
|
|
|
/**
|
|
|
|
* Displays a simple progressbar using \r
|
|
|
|
* @param int $x
|
|
|
|
* @param int $total
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function progressBar($x, $total) {
|
|
|
|
$percent = round($x / $total * 100, 2);
|
|
|
|
echo " \r";
|
|
|
|
echo "Working... " . $percent . " %";
|
2010-12-28 20:26:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
?>
|