data2image/FileImageConverter.class.php

149 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2013-09-05 11:52:26 +02: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() {
echo "\nInitializing color translation table...";
$this->colorConverter = new CharColorConverter();
echo "done.\n";
}
/**
* Processes the given file, returns the filename of the resulting PNG-File.
* @param string $filename
* @return void
* @throws Exception
*/
public function convertFile($filename, $mode) {
if(file_exists($filename) === FALSE) throw new Exception("Invalid filename given!");
//get file data
$filesize = filesize($filename);
$md5 = md5_file($filename);
echo "Processing '" . $filename . "' (" . $filesize . " Bytes) - MD5: " . $md5 . "\n";
$new_filename = basename($filename)."-converted.png";
echo "Filename for the result: '" . $new_filename . "'\n";
//some gradient
$fancy_header = "";
for($i = 0;$i < 255;$i += 5) {
$fancy_header .= chr($i);
}
//header done, now read the file content
$content = file_get_contents($filename);
//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;
//create and prepare image
$this->image = new BoxImage($size, $mode);
echo "Image will be " . $this->image->getWidth() . "px X " . $this->image->getHeight() . "px. (Mode: " . $mode . ")\n";
echo "Filling image...";
$this->image->fillImage(0,0,0,0);
echo "done.\n";
//some fancy header
echo "Adding header...\n";
$this->addDataToImage($fancy_header);
//magic pixel
$this->image->setCurrentPixel(0,0,0,127);
$this->image->movePointer(1);
//add filesize
echo "Adding filesize...\n";
$this->addDataToImage($filesize);
//magic pixel
$this->image->setCurrentPixel(0,0,0,127);
$this->image->movePointer(1);
//add md5 value of content
echo "Adding MD5-Hash...\n";
$this->addDataToImage($md5);
//magic pixel
$this->image->setCurrentPixel(0,0,0,127);
$this->image->movePointer(1);
//content
echo "Adding file contents...\n";
$this->addDataToImage($content);
//magic pixel - last pixel for line and bar mode, do NOT move pointer!
$this->image->setCurrentPixel(0,0,0,127);
//save image
echo "Saving image...";
$this->image->writeToFile($new_filename, 9);
echo "done.\n";
//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);
$this->progressBar($i, $strlen);
}
} catch (Exception $e) {
//TODO: handle weird shit?
echo "Caught Exception:\n";
echo $e;
}
echo "\rdone. \n\n";
}
/**
* 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 . " %";
}
}
?>