[TASK] Wrote classes and implemented about 50% of the whole idea.
This commit is contained in:
parent
a5c6439d3d
commit
bc5b7aeabd
|
@ -0,0 +1,148 @@
|
|||
<?php
|
||||
/**
|
||||
* This class will create a square png according to a given size and fill it with pixels.
|
||||
* @author JPT
|
||||
*/
|
||||
class BoxImage {
|
||||
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
protected $image;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $width;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $height;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $pointer_x;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $pointer_y;
|
||||
|
||||
/**
|
||||
* Basic constructor.
|
||||
* @param int $size
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct($size) {
|
||||
if($size < 1) throw new Exception("This cannot be done!");
|
||||
|
||||
//determine width+height
|
||||
$this->height = ceil(sqrt($size));
|
||||
$this->width = $this->height;
|
||||
|
||||
//prepare image for PNG with transparency using the calculated size.
|
||||
$this->image = imagecreatetruecolor($this->width, $this->height);
|
||||
|
||||
//set the alpha stuff
|
||||
imagesavealpha($this->image, TRUE);
|
||||
imagealphablending($this->image, FALSE);
|
||||
|
||||
//initialize pointers.
|
||||
$this->resetPointer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets pointer.
|
||||
* @return void
|
||||
*/
|
||||
public function resetPointer() {
|
||||
$this->pointer_x = 0;
|
||||
$this->pointer_y = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills the image with the given color.
|
||||
* @param int $r 0 - 255
|
||||
* @param int $g 0 - 255
|
||||
* @param int $b 0 - 255
|
||||
* @param int $a 127 - 0
|
||||
* @return void
|
||||
*/
|
||||
public function fillImage($r, $g, $b, $a) {
|
||||
$color = imagecolorallocatealpha($this->image, $r, $g, $b, $a);
|
||||
imagefill($this->image, 0, 0, $color);
|
||||
imagecolordeallocate($this->image, $color);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the current pixel to the given color, moves the "pointer" of the image.
|
||||
* @param int $r 0 - 255
|
||||
* @param int $g 0 - 255
|
||||
* @param int $b 0 - 255
|
||||
* @param int $a 127 - 0
|
||||
* @return void
|
||||
*/
|
||||
public function setCurrentPixel($r, $g, $b, $a) {
|
||||
$color = imagecolorallocatealpha($this->image, $r, $g, $b, $a);
|
||||
imagesetpixel($this->image, $this->pointer_x, $this->pointer_y, $color);
|
||||
imagecolordeallocate($this->image, $color);
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the x and y pointers of the image according to the offset.
|
||||
* @param int $offset
|
||||
* @throws Exception
|
||||
* @return void
|
||||
*/
|
||||
public function movePointer($offset) {
|
||||
if($offset === 0) return;
|
||||
if($offset > 0) $step = 1;
|
||||
if($offset < 0){
|
||||
$step = -1;
|
||||
$offset = $offset * -1;
|
||||
}
|
||||
|
||||
if(!isset($step)) throw new Exception("What the fuck?");
|
||||
|
||||
for($i = $offset; $i > 0; $i--) {
|
||||
//if we go back
|
||||
if($step === -1) {
|
||||
//decrease x-pointer
|
||||
$this->pointer_x--;
|
||||
//handle y-jumps
|
||||
if($this->pointer_x === 0) {
|
||||
$this->pointer_x = $width;
|
||||
$this->pointer_y--;
|
||||
//out of range?
|
||||
if($this->pointer_y < 0) throw new Exception("Out of range! (width)");
|
||||
}
|
||||
|
||||
//if we go forward
|
||||
} elseif($step === 1) {
|
||||
//increase x-pointer
|
||||
$this->pointer_x++;
|
||||
//handle y-jumps
|
||||
if($this->pointer_x === $this->width) {
|
||||
$this->pointer_x = 0;
|
||||
$this->pointer_y++;
|
||||
//out of range?
|
||||
if($this->pointer_y > $this->height) throw new Exception("Out of range! (height)");
|
||||
}
|
||||
} //end of if
|
||||
} //end of for
|
||||
} //end of function
|
||||
|
||||
/**
|
||||
* Writes the PNG to a file.
|
||||
* @param string $filename
|
||||
* @param int $compression 0-9
|
||||
* @return boolean
|
||||
*/
|
||||
public function writeToFile($filename, $compression) {
|
||||
return imagepng($this->image, $filename, $compression);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
/**
|
||||
* This class contains some library-functions for converting a string to a color [depending on certain modes].
|
||||
* @author JPT
|
||||
*/
|
||||
class CharColorConverter {
|
||||
|
||||
/**
|
||||
* Returns an array with RGB for the corresponding index.
|
||||
* @param int $index Index from 0 to 1530. (Everything above will result the same)
|
||||
* @return array
|
||||
* TODO: Function to reverse this one.
|
||||
* NOTICE: I will use 0 - 1275 for human readability;
|
||||
*/
|
||||
protected function index2rgb($index = 0) {
|
||||
$r = 255;
|
||||
$g = 0;
|
||||
$b = 0;
|
||||
|
||||
//the elseif fixes a glitch.
|
||||
for($i = 0; $i < $index; $i++) {
|
||||
if($r < 255 && $g === 0 && $b === 255) $r++;
|
||||
elseif($r === 255 && $g < 255 && $b === 0) $g++;
|
||||
elseif($r === 0 && $g === 255 && $b < 255) $b++;
|
||||
elseif($r > 0 && $g === 255 && $b === 0) $r--;
|
||||
elseif($r === 0 && $g > 0 && $b === 255) $g--;
|
||||
elseif($r === 255 && $g === 0 && $b > 0) $b--;
|
||||
|
||||
//DEBUG
|
||||
//$k = $i + 1;
|
||||
//echo "I:$k\t$r,$g,$b\r\n";
|
||||
}
|
||||
|
||||
return array($r, $g, $b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a single char to a string.
|
||||
* @param string $string
|
||||
* @return array RGB
|
||||
*/
|
||||
public function charToColor($string) {
|
||||
if(strlen($string) === 0) throw new Exception("Empty string given!");
|
||||
$index = ord($string) * 5;
|
||||
$color = $this->index2rgb($index);
|
||||
return $color;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,6 +1,9 @@
|
|||
<?php
|
||||
require "val2rgb.php";
|
||||
|
||||
$file = $argv[1];
|
||||
if(file_exists($file) === FALSE) die("Nein\n");
|
||||
if(file_exists($file) === FALSE) die("Error: file does not exist!\n");
|
||||
|
||||
$data = file_get_contents($file);
|
||||
$size = strlen($data);
|
||||
$foobar = array();
|
||||
|
|
|
@ -0,0 +1,104 @@
|
|||
<?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);
|
||||
|
||||
$new_filename = "result.png";
|
||||
|
||||
$header = "";
|
||||
for($i = 0;$i < 255;$i += 5) {
|
||||
$header .= chr($i);
|
||||
}
|
||||
$header .= chr(0);
|
||||
$header .= $filesize;
|
||||
$header .= chr(0);
|
||||
$header .= $md5;
|
||||
|
||||
//header done, now read the file content
|
||||
$content = file_get_contents($filename);
|
||||
|
||||
//get size for the image
|
||||
$size = strlen($header) + 1 + $filesize;
|
||||
|
||||
//create and prepare image
|
||||
$this->image = new BoxImage($size);
|
||||
$this->image->fillImage(0,0,0,127);
|
||||
|
||||
//header first
|
||||
$this->addDataToImage($header);
|
||||
|
||||
//magic pixel
|
||||
$this->image->setCurrentPixel(0,0,0,127);
|
||||
$this->image->movePointer(1);
|
||||
|
||||
//content
|
||||
$this->addDataToImage($content);
|
||||
|
||||
//save image
|
||||
$this->image->writeToFile($new_filename, 0);
|
||||
|
||||
//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);
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
//TODO: handle weird shit?
|
||||
echo "Caught Exception:\n";
|
||||
echo $e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
require_once "CharColorConverter.class.php";
|
||||
require_once "BoxImage.class.php";
|
||||
require_once "FileImageConverter.class.php";
|
||||
|
||||
$converter = new FileImageConverter();
|
||||
$converter->convertFile("test.jpg");
|
||||
|
||||
?>
|
27
val2rgb.php
27
val2rgb.php
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
$index = $argv[1];
|
||||
|
||||
$r = 255;
|
||||
$g = 0;
|
||||
$b = 0;
|
||||
for($i = 0; $i < $index; $i++) {
|
||||
|
||||
echo "I:$i\tR:$r G:$g B:$b \n";
|
||||
|
||||
if($g === 0 && $b === 255 && $r < 255) $r++;
|
||||
if($b === 0 && $g === 255 && $r > 0) $r--;
|
||||
|
||||
if($b === 0 && $r === 255 && $g < 255) $g++;
|
||||
if($r === 0 && $b === 255 && $g > 0) $g--;
|
||||
|
||||
if($r === 0 && $g === 255 && $b < 255) $b++;
|
||||
if($g === 0 && $r === 255 && $b > 0) $b--;
|
||||
|
||||
}
|
||||
echo "\n";
|
||||
print_r(array($i, $r, $g, $b));
|
||||
|
||||
// 0 - 1527
|
||||
|
||||
?>
|
Loading…
Reference in New Issue