/** * 单例模式 * * @return Smarty */ public static function init() { static $object = null; if ($object === null) { $object = new self(); } else { $object->clear(); } return $object; }
/** * Cleanup expired cache entries. */ private static function file_gc() { $dir = new DirectoryIterator(\Sledgehammer\TMP_DIR . 'Cache'); $files = []; foreach ($dir as $entry) { if ($entry->isFile()) { $files[] = $entry->getFilename(); } } shuffle($files); $files = array_slice($files, 0, ceil(count($files) / 10)); // Take 10% of the files $cache = new self('GC', 'file'); foreach ($files as $id) { $cache->_guid = $id; $cache->_file = fopen(\Sledgehammer\TMP_DIR . 'Cache/' . $id, 'r'); $hit = $cache->read($output); fclose($cache->_file); $cache->_file = null; if ($hit === false) { // Expired? $cache->clear(); } } }
/** * Given a template name, and a list of variable arrays, this method will * iterate through all the sets of variables, rendering the template with * each set, and concatenating them together. Could be used for looping * through a complex list, where each list element would be too much HTML * to cleanly place in the main template itself. * * @param string $template Path to template file. * @param array $varArray Array of associative arrays containing * template variables. * @return string All rendered templates concatenated together. */ public function partialLoop($template, array $varArray = array()) { $t = new self($template); $s = ''; foreach ($varArray as $index => $vars) { $t->clear(); $t->set($vars); $t->set('loopIndex', $index); $s .= $t->render(); } return $s; }
/** * Watermark * * You can place watermark at one of 9 zones * * -------------------------- * | 1 | 2 | 3 | * -------------------------- * | 4 | 5 | 6 | * -------------------------- * | 7 | 8 | 9 | * -------------------------- * * @param mixed $watermark * @param int $zone */ public function watermark($watermark = NULL, $zone = 9, $opacity = 100) { $watermark && file_exists($watermark) or $watermark = config('watermark', ENGINE . DS . 'Core.' . DS . 'images' . DS . 'watermark.png'); $this->prepare(); imagealphablending($this->destination, TRUE); // imagesavealpha($this->destination, TRUE); $watermark = new self($watermark); imagecopy($this->destination, $this->source, 0, 0, 0, 0, $this->info->width, $this->info->height); switch ($zone) { case 1: $top = 0; $left = 0; break; case 2: $top = 0; $left = ($this->info->width - $watermark->info->width) / 2; break; case 3: $top = 0; $left = $this->info->width - $watermark->info->width; break; case 4: $top = ($this->info->height - $watermark->info->height) / 2; $left = 0; break; case 5: $top = ($this->info->height - $watermark->info->height) / 2; $left = ($this->info->width - $watermark->info->width) / 2; break; case 6: $top = ($this->info->height - $watermark->info->height) / 2; $left = $this->info->width - $watermark->info->width; break; case 7: $top = $this->info->height - $watermark->info->height; $left = 0; break; case 8: $top = $this->info->height - $watermark->info->height; $left = ($this->info->width - $watermark->info->width) / 2; break; case 9: default: $top = $this->info->height - $watermark->info->height; $left = $this->info->width - $watermark->info->width; } imagecopymerge($this->destination, $watermark->source, $left, $top, 0, 0, $watermark->info->width, $watermark->info->height, $opacity); $watermark->clear(); return $this; }