liero.php

Liero level png thumbnail generator

Year

Tags

<?php

class liero
{
	protected static $palette;
	protected static $level;
	protected static $level_size = 1;

	public static function init()
	{
		self::$level_size = 504*350;
		self::$palette = self::load_palette('liero.lpl');

		self::$level = self::load_level('pokol.lev');
	}

	public static function image($width = NULL, $height = NULL)
	{
		header("Content-type: image/png");

		$img = imagecreatetruecolor(504, 350);
		imagealphablending($img, FALSE);
		$color = imagecolorallocatealpha($img, 0, 0, 0, 127);
		imagefill($img, 0, 0, $color);
		imagesavealpha($img, TRUE);

		for ($i = 0; $i < 504; $i++)
		{
			for ($j = 0; $j < 350; $j++)
			{
				$pos = ($j * 504) + $i;

				self::setpixel($img, $i, $j, self::$palette[self::$level[$pos]][0], self::$palette[self::$level[$pos]][1], self::$palette[self::$level[$pos]][2]);
			}
		}

		if ($width != NULL AND $height != NULL)
		{
			$x = imagesx($img);
			$y = imagesy($img);

			$new_img = imagecreatetruecolor($width, $height);
			imagealphablending($new_img, FALSE);
			$color = imagecolorallocatealpha($new_img, 0, 0, 0, 127);
			imagefill($new_img, 0, 0, $color);
			imagesavealpha($new_img, TRUE);

			imagecopyresampled($new_img, $img, 0, 0, 0, 0, $width, $height, $x, $y);
			imagedestroy($img);
			$img = $new_img;
		}

		imagepng($img);
	}

	public static function setpixel($img, $x, $y, $r, $g, $b)
	{
		$color = imagecolorallocate($img, $r, $g, $b);

		imagesetpixel($img, round($x),round($y), $color);
	}

	public static function load_exe_palette($path)
	{
		return self::load_palette($path, TRUE);
	}

	public static function load_palette($path, $exe_skip = FALSE)
	{
		$palette = array();

		if ( ! $fp = @fopen($path, 'rb'))
		{
			return FALSE;
		}

		if ($exe_skip)
		{
			fseek($fp, 132774);
		}

		for ($i = 0; $i < 256; $i++)
		{
			$rgb = array
			(
				fread($fp, 1),
				fread($fp, 1),
				fread($fp, 1)
			);

			for ($j = 0; $j < 3; $j++)
			{
				$rgb[$j] = unpack('C*', $rgb[$j]);
				$rgb[$j] = $rgb[$j][1];
			}

			$palette[] = $rgb;
		}

		fclose($fp);

		return $palette;
	}

	public static function load_level($path)
	{
		$level = array();

		if ( ! $fp = @fopen($path, 'rb'))
		{
			return FALSE;
		}

		$size = filesize($path);

		for ($i = 0; $i < self::$level_size; $i++)
		{
			$byte = fread($fp, 1);
			$byte = unpack('C*', $byte);
			$byte = $byte[1];

			$level[] = $byte;
		}

		fclose($fp);

		return $level;
	}
}

liero::init();
liero::image();

?>