[TASK] Fixed a bug in the movePointer() function.
This commit is contained in:
parent
61ea2b936e
commit
b74c814e3f
|
@ -36,12 +36,27 @@ class BoxImage {
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct($size) {
|
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
|
||||||
$this->height = ceil(sqrt($size));
|
switch($mode) {
|
||||||
$this->width = $this->height;
|
case "square":
|
||||||
|
$this->height = ceil(sqrt($size));
|
||||||
|
$this->width = $this->height;
|
||||||
|
break;
|
||||||
|
case "line":
|
||||||
|
$this->height = 1;
|
||||||
|
$this->width = $size;
|
||||||
|
break;
|
||||||
|
case "bar":
|
||||||
|
$this->height = $size;
|
||||||
|
$this->width = 1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Exception("Unknown mode given!");
|
||||||
|
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);
|
||||||
|
@ -98,6 +113,8 @@ class BoxImage {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function movePointer($offset) {
|
public function movePointer($offset) {
|
||||||
|
//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){
|
||||||
|
@ -125,14 +142,16 @@ class BoxImage {
|
||||||
//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";
|
||||||
} //end of function
|
} //end of function
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -29,7 +29,7 @@ class FileImageConverter {
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function convertFile($filename) {
|
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
|
||||||
|
@ -56,9 +56,9 @@ class FileImageConverter {
|
||||||
$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);
|
$this->image = new BoxImage($size, $mode);
|
||||||
|
|
||||||
echo "Image will be " . $this->image->getWidth() . "px x " . $this->image->getHeight() . "px.\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);
|
||||||
|
@ -92,9 +92,8 @@ class FileImageConverter {
|
||||||
echo "Adding file contents...\n";
|
echo "Adding file contents...\n";
|
||||||
$this->addDataToImage($content);
|
$this->addDataToImage($content);
|
||||||
|
|
||||||
//magic pixel
|
//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);
|
||||||
$this->image->movePointer(1);
|
|
||||||
|
|
||||||
//save image
|
//save image
|
||||||
echo "Saving image...";
|
echo "Saving image...";
|
||||||
|
|
4
Main.php
4
Main.php
|
@ -1,11 +1,13 @@
|
||||||
<?php
|
<?php
|
||||||
|
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];
|
||||||
|
|
||||||
$converter = new FileImageConverter();
|
$converter = new FileImageConverter();
|
||||||
$converter->convertFile($filename);
|
$converter->convertFile($filename, $mode);
|
||||||
|
|
||||||
?>
|
?>
|
Loading…
Reference in New Issue