/** * 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); } }
/** * Open file. * * The constructor calls the fopen function to open the file handle and * throws a T_Exception_File on failure. The input $mode is also checked * for portability (it should include the 'b' binary indicator so original * line endings are preserved). The input modes available are: * * 'rb' reading only. * 'wb' writing only, truncate or create file. * * @param T_File_Path $fname full path to file * @param string $mode open mode (including 'b' indicator) */ function __construct(T_File_Path $fname, $mode) { if (strcmp('wb', $mode) == 0) { $this->fswap = $fname; $fname = new T_File_Path($fname->getDirName(), uniqid(rand(), true), null); // ^ make a tmp file in same dir as target } parent::__construct($fname, $mode); }
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); }
/** * 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; }
/** * Specify path. * * @param string $web_root dir for web root * @param string $rel_dir rel dir from web root * @param string $file filename * @param string $ext extension */ function __construct($web_root, $rel_dir, $file, $ext) { // split relative path into path sub-sections $rel_dir = trim(trim($rel_dir, '/'), DIRECTORY_SEPARATOR); if (strcmp('/', DIRECTORY_SEPARATOR) !== 0) { $this->url_path = preg_split('#[' . preg_quote('/' . DIRECTORY_SEPARATOR) . ']#', $rel_dir); } else { $this->url_path = explode('/', $rel_dir); } // concatenate dirs and exe parent $dir = rtrim(rtrim($web_root, '/'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $rel_dir; parent::__construct($dir, $file, $ext); }
/** * 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()); }