Location: PHPKode > scripts > Image to PHP Standalone Output > image-to-php-standalone-output/Packer.php
<?php
class Packer {
	protected $inputPath;
	protected $outputPath;
	protected $subdir;

	protected $images = array();
	
	protected $fileContent = "";

	public function __construct($inputPath = null,$outputPath = null,$subdir = false) {
		$this->inputPath = realpath($inputPath);
		$this->outputPath = realpath($outputPath);
		$this->subdir = $subdir;
	}

	public function save($className) {
		if (empty($this->images)) return false;
		$this->createFileContent($className);
		return file_put_contents($this->outputPath."/$className.php",$this->fileContent) > 0 ? true : false;
	}
	
	protected function createFileContent($className) {
		$this->appendFileContent("<?php");
		$this->appendFileContent("class $className {");
		
		$this->appendFileContent("protected \$files = array(",1);
		foreach ($this->images as $k=>$v) {
			$this->appendFileContent("\"$k\"=>array(",2);
			
			$this->appendFileContent("\"$v[0]\",",3);
			$this->appendFileContent("\"$v[1]\"",3);
			
			$this->appendFileContent("),",2);
		}
		$this->appendFileContent(");",1);
		
		$this->appendFileContent("");
		
		$this->appendFileContent("public function __get(\$name) {",1);
		$this->appendFileContent("if (headers_sent()) die();",2);
		$this->appendFileContent("if (isset(\$this->files[\$name])) {",2);
		$this->appendFileContent("header(\"Content-Type: \".\$this->files[\$name][0]);",3);
		$this->appendFileContent("echo base64_decode(\$this->files[\$name][1]);",3);
		$this->appendFileContent("die();",3);
		$this->appendFileContent("} else {",2);
		$this->appendFileContent("die();",3);
		$this->appendFileContent("}",2);
		$this->appendFileContent("}",1	);

		$this->appendFileContent("}");
		
		$this->appendFileContent("if (isset(\$_GET[\"i\"])) {");
		$this->appendFileContent("\$image = new $className();",1);
		$this->appendFileContent("\$image->\$_GET[\"i\"];",1);
		$this->appendFileContent("}");
	}
	
	protected function appendFileContent($line,$ident = 0,$nl = true) {
		$ident = str_repeat("\t",$ident);
		$this->fileContent .= $ident.$line.($nl ? "\n" : "");
	}
	
	public function parse() {
		$this->_scandir($this->inputPath);
		return empty($this->images) ? false : true;
	}

	protected function _scandir($path) {
		$list = scandir($path);
		foreach ($list as $v) {
			if ($v == '.' || $v == '..') continue;
			$v = "$path/$v";
			if ($this->subdir && is_dir($v)) {
				$this->_scandir($v);
			} else {
				$this->addImage($v);
			}
		}
	}

	protected function isImage($path) {
		return @getimagesize($path) === false ? false : true;
	}

	protected function addImage($path) {
		if (!$this->isImage($path)) return false;

		$data = getimagesize($path);
		$content = base64_encode(file_get_contents($path));
		$name = $this->getFileName($path);

		switch ($data[2]) {
			case IMAGETYPE_JPEG:
				$mimeType = "image/jpeg";
				break;

			case IMAGETYPE_GIF:
				$mimeType = "image/gif";
				break;

			case IMAGETYPE_PNG:
				$mimeType = "image/png";
				break;

			default:
				return false;
		}

		$this->images[$name] = array(
			$mimeType,
			$content
		);
	}

	protected function getFileName($path) {
		$name = preg_replace(array("'.+(\\\\|\/)([^\/]+)$'","'[^a-zA-Z0-9]'"),array("$2"," "),$path);
		$name = explode(" ",$name,2);
		$name[0][0] = strtolower($name[0][0]);
		$name[1] = ucwords($name[1]);
		$name = implode("",$name);
		return $name;
	}
}
Return current item: Image to PHP Standalone Output