[TASK] Fix line endings to LF only.

This commit is contained in:
Jan Philipp Timme 2013-09-05 11:52:26 +02:00
parent 633b4443e7
commit 7f656fffd9
3 changed files with 342 additions and 342 deletions

View File

@ -1,183 +1,183 @@
<?php <?php
/** /**
* This class will create a square png according to a given size and fill it with pixels. * This class will create a square png according to a given size and fill it with pixels.
* @author JPT * @author JPT
*/ */
class BoxImage { class BoxImage {
/** /**
* @var resource * @var resource
*/ */
protected $image; protected $image;
/** /**
* @var int * @var int
*/ */
protected $width; protected $width;
/** /**
* @var int * @var int
*/ */
protected $height; protected $height;
/** /**
* @var int * @var int
*/ */
protected $pointer_x; protected $pointer_x;
/** /**
* @var int * @var int
*/ */
protected $pointer_y; protected $pointer_y;
/** /**
* Basic constructor. * Basic constructor.
* @param int $size * @param int $size
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function __construct($size, $mode) { public function __construct($size, $mode) {
if($size < 1) throw new Exception("This cannot be done!"); if($size < 1) throw new Exception("This cannot be done!");
//determine width+height //determine width+height
switch($mode) { switch($mode) {
case "square": case "square":
$this->height = ceil(sqrt($size)); $this->height = ceil(sqrt($size));
$this->width = $this->height; $this->width = $this->height;
break; break;
case "line": case "line":
$this->height = 1; $this->height = 1;
$this->width = $size; $this->width = $size;
break; break;
case "bar": case "bar":
$this->height = $size; $this->height = $size;
$this->width = 1; $this->width = 1;
break; break;
default: default:
throw new Exception("Unknown mode given!"); throw new Exception("Unknown mode given!");
break; break;
} }
//prepare image for PNG with transparency using the calculated size. //prepare image for PNG with transparency using the calculated size.
$this->image = imagecreatetruecolor($this->width, $this->height); $this->image = imagecreatetruecolor($this->width, $this->height);
//set the alpha stuff //set the alpha stuff
imagesavealpha($this->image, TRUE); imagesavealpha($this->image, TRUE);
imagealphablending($this->image, FALSE); imagealphablending($this->image, FALSE);
//initialize pointers. //initialize pointers.
$this->resetPointer(); $this->resetPointer();
} }
/** /**
* Resets pointer. * Resets pointer.
* @return void * @return void
*/ */
public function resetPointer() { public function resetPointer() {
$this->pointer_x = 0; $this->pointer_x = 0;
$this->pointer_y = 0; $this->pointer_y = 0;
} }
/** /**
* Fills the image with the given color. * Fills the image with the given color.
* @param int $r 0 - 255 * @param int $r 0 - 255
* @param int $g 0 - 255 * @param int $g 0 - 255
* @param int $b 0 - 255 * @param int $b 0 - 255
* @param int $a 127 - 0 * @param int $a 127 - 0
* @return void * @return void
*/ */
public function fillImage($r, $g, $b, $a) { public function fillImage($r, $g, $b, $a) {
$color = imagecolorallocatealpha($this->image, $r, $g, $b, $a); $color = imagecolorallocatealpha($this->image, $r, $g, $b, $a);
imagefill($this->image, 0, 0, $color); imagefill($this->image, 0, 0, $color);
imagecolordeallocate($this->image, $color); imagecolordeallocate($this->image, $color);
} }
/** /**
* Sets the current pixel to the given color, moves the "pointer" of the image. * Sets the current pixel to the given color, moves the "pointer" of the image.
* @param int $r 0 - 255 * @param int $r 0 - 255
* @param int $g 0 - 255 * @param int $g 0 - 255
* @param int $b 0 - 255 * @param int $b 0 - 255
* @param int $a 127 - 0 * @param int $a 127 - 0
* @return void * @return void
*/ */
public function setCurrentPixel($r, $g, $b, $a) { public function setCurrentPixel($r, $g, $b, $a) {
$color = imagecolorallocatealpha($this->image, $r, $g, $b, $a); $color = imagecolorallocatealpha($this->image, $r, $g, $b, $a);
imagesetpixel($this->image, $this->pointer_x, $this->pointer_y, $color); imagesetpixel($this->image, $this->pointer_x, $this->pointer_y, $color);
imagecolordeallocate($this->image, $color); imagecolordeallocate($this->image, $color);
} }
/** /**
* Moves the x and y pointers of the image according to the offset. * Moves the x and y pointers of the image according to the offset.
* @param int $offset * @param int $offset
* @throws Exception * @throws Exception
* @return void * @return void
*/ */
public function movePointer($offset) { public function movePointer($offset) {
//echo $this->pointer_x.",".$this->pointer_y." --> "; //echo $this->pointer_x.",".$this->pointer_y." --> ";
if($offset === 0) return; if($offset === 0) return;
if($offset > 0) $step = 1; if($offset > 0) $step = 1;
if($offset < 0){ if($offset < 0){
$step = -1; $step = -1;
$offset = $offset * -1; $offset = $offset * -1;
} }
if(!isset($step)) throw new Exception("What the fuck?"); if(!isset($step)) throw new Exception("What the fuck?");
for($i = $offset; $i > 0; $i--) { for($i = $offset; $i > 0; $i--) {
//if we go back //if we go back
if($step === -1) { if($step === -1) {
//decrease x-pointer //decrease x-pointer
$this->pointer_x -= 1; $this->pointer_x -= 1;
//handle y-jumps //handle y-jumps
if($this->pointer_x < 0) { if($this->pointer_x < 0) {
$this->pointer_x = $width; $this->pointer_x = $width;
$this->pointer_y -= 1; $this->pointer_y -= 1;
//out of range? //out of range?
if($this->pointer_y < 0) throw new Exception("Out of range! (width)"); if($this->pointer_y < 0) throw new Exception("Out of range! (width)");
} }
//if we go forward //if we go forward
} elseif($step === 1) { } elseif($step === 1) {
//increase x-pointer //increase x-pointer
$this->pointer_x += 1; $this->pointer_x += 1;
//handle y-jumps //handle y-jumps
if($this->pointer_x >= $this->width) { if($this->pointer_x >= $this->width) {
$this->pointer_x = 0; $this->pointer_x = 0;
$this->pointer_y += 1; $this->pointer_y += 1;
//out of range? //out of range?
if($this->pointer_y >= $this->height) throw new Exception("Out of range! (height)"); if($this->pointer_y >= $this->height) throw new Exception("Out of range! (height)");
} }
} //end of if } //end of if
} //end of for } //end of for
//echo $this->pointer_x.",".$this->pointer_y."\n"; //echo $this->pointer_x.",".$this->pointer_y."\n";
} //end of function } //end of function
/** /**
* Writes the PNG to a file. * Writes the PNG to a file.
* @param string $filename * @param string $filename
* @param int $compression 0-9 * @param int $compression 0-9
* @return boolean * @return boolean
*/ */
public function writeToFile($filename, $compression) { public function writeToFile($filename, $compression) {
return imagepng($this->image, $filename, $compression); return imagepng($this->image, $filename, $compression);
} }
/** /**
* Gets height. * Gets height.
* @return int * @return int
*/ */
public function getHeight() { public function getHeight() {
return $this->height; return $this->height;
} }
/** /**
* Gets width. * Gets width.
* @return int * @return int
*/ */
public function getWidth() { public function getWidth() {
return $this->width; return $this->width;
} }
} }

View File

@ -1,149 +1,149 @@
<?php <?php
/** /**
* This class will be able to convert a file into an image. * This class will be able to convert a file into an image.
* @author JPT * @author JPT
*/ */
class FileImageConverter { class FileImageConverter {
/** /**
* @var BoxImage * @var BoxImage
*/ */
protected $image; protected $image;
/** /**
* @var CharColorConverter * @var CharColorConverter
*/ */
protected $colorConverter; protected $colorConverter;
/** /**
* Default constructor * Default constructor
* @return void * @return void
*/ */
public function __construct() { public function __construct() {
echo "\nInitializing color translation table..."; echo "\nInitializing color translation table...";
$this->colorConverter = new CharColorConverter(); $this->colorConverter = new CharColorConverter();
echo "done.\n"; echo "done.\n";
} }
/** /**
* Processes the given file, returns the filename of the resulting PNG-File. * Processes the given file, returns the filename of the resulting PNG-File.
* @param string $filename * @param string $filename
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function convertFile($filename, $mode) { public function convertFile($filename, $mode) {
if(file_exists($filename) === FALSE) throw new Exception("Invalid filename given!"); if(file_exists($filename) === FALSE) throw new Exception("Invalid filename given!");
//get file data //get file data
$filesize = filesize($filename); $filesize = filesize($filename);
$md5 = md5_file($filename); $md5 = md5_file($filename);
echo "Processing '" . $filename . "' (" . $filesize . " Bytes) - MD5: " . $md5 . "\n"; echo "Processing '" . $filename . "' (" . $filesize . " Bytes) - MD5: " . $md5 . "\n";
$new_filename = basename($filename)."-converted.png"; $new_filename = basename($filename)."-converted.png";
echo "Filename for the result: '" . $new_filename . "'\n"; echo "Filename for the result: '" . $new_filename . "'\n";
//some gradient //some gradient
$fancy_header = ""; $fancy_header = "";
for($i = 0;$i < 255;$i += 5) { for($i = 0;$i < 255;$i += 5) {
$fancy_header .= chr($i); $fancy_header .= chr($i);
} }
//header done, now read the file content //header done, now read the file content
$content = file_get_contents($filename); $content = file_get_contents($filename);
//get final minimum size for the image //get final minimum size for the image
//(do not mix up strlen of $filesize and the actual $filesize) //(do not mix up strlen of $filesize and the actual $filesize)
$size = strlen($fancy_header) + 1 + strlen($filesize) + 1 + strlen($md5) + 1 + $filesize + 1; $size = strlen($fancy_header) + 1 + strlen($filesize) + 1 + strlen($md5) + 1 + $filesize + 1;
//create and prepare image //create and prepare image
$this->image = new BoxImage($size, $mode); $this->image = new BoxImage($size, $mode);
echo "Image will be " . $this->image->getWidth() . "px X " . $this->image->getHeight() . "px. (Mode: " . $mode . ")\n"; echo "Image will be " . $this->image->getWidth() . "px X " . $this->image->getHeight() . "px. (Mode: " . $mode . ")\n";
echo "Filling image..."; echo "Filling image...";
$this->image->fillImage(0,0,0,0); $this->image->fillImage(0,0,0,0);
echo "done.\n"; echo "done.\n";
//some fancy header //some fancy header
echo "Adding header...\n"; echo "Adding header...\n";
$this->addDataToImage($fancy_header); $this->addDataToImage($fancy_header);
//magic pixel //magic pixel
$this->image->setCurrentPixel(0,0,0,127); $this->image->setCurrentPixel(0,0,0,127);
$this->image->movePointer(1); $this->image->movePointer(1);
//add filesize //add filesize
echo "Adding filesize...\n"; echo "Adding filesize...\n";
$this->addDataToImage($filesize); $this->addDataToImage($filesize);
//magic pixel //magic pixel
$this->image->setCurrentPixel(0,0,0,127); $this->image->setCurrentPixel(0,0,0,127);
$this->image->movePointer(1); $this->image->movePointer(1);
//add md5 value of content //add md5 value of content
echo "Adding MD5-Hash...\n"; echo "Adding MD5-Hash...\n";
$this->addDataToImage($md5); $this->addDataToImage($md5);
//magic pixel //magic pixel
$this->image->setCurrentPixel(0,0,0,127); $this->image->setCurrentPixel(0,0,0,127);
$this->image->movePointer(1); $this->image->movePointer(1);
//content //content
echo "Adding file contents...\n"; echo "Adding file contents...\n";
$this->addDataToImage($content); $this->addDataToImage($content);
//magic pixel - last pixel for line and bar mode, do NOT move pointer! //magic pixel - last pixel for line and bar mode, do NOT move pointer!
$this->image->setCurrentPixel(0,0,0,127); $this->image->setCurrentPixel(0,0,0,127);
//save image //save image
echo "Saving image..."; echo "Saving image...";
$this->image->writeToFile($new_filename, 9); $this->image->writeToFile($new_filename, 9);
echo "done.\n"; echo "done.\n";
//clean up //clean up
unset($this->image); unset($this->image);
} }
/** /**
* Converts data and adds it to the image. * Converts data and adds it to the image.
* @param string $data * @param string $data
* @return void * @return void
*/ */
protected function addDataToImage($data) { protected function addDataToImage($data) {
try { try {
$strlen = strlen($data); $strlen = strlen($data);
for($i = 0;$i < $strlen; $i++) { for($i = 0;$i < $strlen; $i++) {
$char = substr($data, $i, 1); $char = substr($data, $i, 1);
//convert char to color //convert char to color
list($r, $g, $b) = $this->colorConverter->charToColor($char); list($r, $g, $b) = $this->colorConverter->charToColor($char);
//set the pixel //set the pixel
$this->image->setCurrentPixel($r, $g, $b, 0); $this->image->setCurrentPixel($r, $g, $b, 0);
//move the pointer //move the pointer
$this->image->movePointer(1); $this->image->movePointer(1);
$this->progressBar($i, $strlen); $this->progressBar($i, $strlen);
} }
} catch (Exception $e) { } catch (Exception $e) {
//TODO: handle weird shit? //TODO: handle weird shit?
echo "Caught Exception:\n"; echo "Caught Exception:\n";
echo $e; echo $e;
} }
echo "\rdone. \n\n"; echo "\rdone. \n\n";
} }
/** /**
* Displays a simple progressbar using \r * Displays a simple progressbar using \r
* @param int $x * @param int $x
* @param int $total * @param int $total
* @return void * @return void
*/ */
protected function progressBar($x, $total) { protected function progressBar($x, $total) {
$percent = round($x / $total * 100, 2); $percent = round($x / $total * 100, 2);
echo " \r"; echo " \r";
echo "Working... " . $percent . " %"; echo "Working... " . $percent . " %";
} }
} }
?> ?>

View File

@ -1,13 +1,13 @@
<?php <?php
error_reporting(E_ALL); error_reporting(E_ALL);
require_once "CharColorConverter.class.php"; require_once "CharColorConverter.class.php";
require_once "BoxImage.class.php"; require_once "BoxImage.class.php";
require_once "FileImageConverter.class.php"; require_once "FileImageConverter.class.php";
$filename = $argv[1]; $filename = $argv[1];
$mode = $argv[2]; $mode = $argv[2];
$converter = new FileImageConverter(); $converter = new FileImageConverter();
$converter->convertFile($filename, $mode); $converter->convertFile($filename, $mode);
?> ?>