diff --git a/BoxImage.class.php b/BoxImage.class.php new file mode 100644 index 0000000..f83b6ee --- /dev/null +++ b/BoxImage.class.php @@ -0,0 +1,148 @@ +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); + } + +} \ No newline at end of file diff --git a/CharColorConverter.class.php b/CharColorConverter.class.php new file mode 100644 index 0000000..ee1665c --- /dev/null +++ b/CharColorConverter.class.php @@ -0,0 +1,50 @@ + 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; + } +} + +?> \ No newline at end of file diff --git a/File2Img.php b/File2Img.php index f411896..ff23fea 100644 --- a/File2Img.php +++ b/File2Img.php @@ -1,6 +1,9 @@ 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; + } + + } + +} +?> \ No newline at end of file diff --git a/Main.php b/Main.php new file mode 100644 index 0000000..cbb8177 --- /dev/null +++ b/Main.php @@ -0,0 +1,9 @@ +convertFile("test.jpg"); + +?> \ No newline at end of file diff --git a/val2rgb.php b/val2rgb.php deleted file mode 100644 index 4673548..0000000 --- a/val2rgb.php +++ /dev/null @@ -1,27 +0,0 @@ - 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 - -?> \ No newline at end of file