/** * Load the image from file. * * @param T_File_Path $path file path */ function __construct($path) { $this->mime = $path->getMime(); if (!$path->exists()) { throw new T_Exception_Image($path->__toString() . ' not exists'); } $pathstr = $path->__toString(); list($this->width, $this->height, $type) = @getimagesize($pathstr); switch ($this->mime->getType()) { case T_Mime::JPEG: if ($type !== IMAGETYPE_JPEG) { throw new T_Exception_Image($pathstr . ' is not a JPEG'); } $this->handle = @imagecreatefromjpeg($pathstr); break; case T_Mime::PNG: if ($type !== IMAGETYPE_PNG) { throw new T_Exception_Image($pathstr . ' is not a PNG'); } $this->handle = @imagecreatefrompng($pathstr); break; case T_Mime::GIF: if ($type !== IMAGETYPE_GIF) { throw new T_Exception_Image($pathstr . ' is not a GIF'); } $this->handle = @imagecreatefromgif($pathstr); break; default: $msg = 'Illegal image MIME type ' . $mime->__toString(); throw new T_Exception_Image($msg); } if ($this->handle === false) { throw new T_Exception_Image('Error loading ' . $pathstr); } }
/** * Outputs the file to the output buffer. * * @return T_View fluent interface */ function toBuffer() { $ok = @readfile(parent::__toString()); if ($ok === false) { throw new T_Exception_File(parent::__toString(), 'could not be rendered to buffer'); } return $this; }
function testToBufferOutputsFileContentsToBuffer() { mkdir(T_CACHE_DIR . 'test'); $path = new T_File_Path(T_CACHE_DIR . 'test', 'common', 'css'); $file = new T_File_Unlocked($path, 'wb'); $file->write('somecontent'); $file->close(); $view = new T_File_View(T_CACHE_DIR . 'test', 'common', 'css'); $expect = file_get_contents($path->__toString()); ob_start(); $test = $view->toBuffer(); $this->assertSame($view, $test, 'fluent interface'); $test = ob_get_clean(); $this->assertSame($expect, $test); }
/** * Save to file. * * @param string $dir directory * @param string $filename filename * @return T_File_Path filepath to file (normalized extension) */ function toFile($dir, $filename) { $path = new T_File_Path($dir, $filename, $this->getMime()->getExt()); if ($path->exists()) { /* atomic swap-based replace performed so no possibility of mid-write read of partial image by another process */ $tmp = new T_File_Path($dir, uniqid(rand(), true), $this->getMime()->getExt()); $this->export($tmp->__toString()); $tmp->rename($path); } else { $this->export($path->__toString()); } return $path; }
/** * Compiles a template file using a filter. * * @param mixed filter * @return string compiled template filename */ function _compileUsingFilter($filter) { $dir = new T_File_Dir(T_CACHE_DIR . 'template'); $cache = new T_File_Path($dir->__toString(), md5($this->_tpl), 'php'); if (!$cache->exists() || $cache->exists() && $cache->getLastModified() < filemtime($this->_tpl)) { $data = @file_get_contents($this->_tpl); if ($data === false) { $msg = "Error reading template file {$this->_tpl}"; throw new RuntimeException(); } $data = _transform($data, $filter); $swap = new T_File_Swap($cache, 'wb'); $swap->write($data); $swap->close(); } return $cache->__toString(); }
/** * Copy file to another location. * * @param T_File_Path $path filepath for file to copy to */ function copyTo(T_File_Path $path) { $ok = @copy($this->getPath(), $path->__toString()); if (!$ok) { $msg = 'copy to ' . $path->__toString() . ' failed'; throw new T_Exception_File($this->getPath(), $msg); } return $this; }
/** * Rename the current file to a new filename. * * @param T_File_Path $path new filepath for file */ function rename(T_File_Path $path) { move_uploaded_file($this->path, $path->__toString()); }