data2image/BoxImage.class.php

148 lines
3.3 KiB
PHP
Raw Normal View History

<?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
2010-12-28 20:36:17 +01:00
$this->pointer_x -= 1;
//handle y-jumps
2010-12-28 20:36:17 +01:00
if($this->pointer_x < 0) {
$this->pointer_x = $width;
2010-12-28 20:36:17 +01:00
$this->pointer_y -= 1;
//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
2010-12-28 20:36:17 +01:00
$this->pointer_x += 1;
//handle y-jumps
2010-12-28 20:36:17 +01:00
if($this->pointer_x > $this->width) {
$this->pointer_x = 0;
2010-12-28 20:36:17 +01:00
$this->pointer_y += 1;
//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);
}
}