/** * minify each file in a collection * @param $collection * @return mixed */ public function minify($collection) { $web = \Web::instance(); // check final path $public_path = $this->f3->get('ASSETS.minify.public_path'); if (!is_dir($public_path)) { mkdir($public_path, 0777, true); } $type = false; $inline_stack = array(); foreach ($collection as $i => &$asset) { $type = $asset['type']; if ($asset['origin'] == 'inline') { $slot = $asset['slot'] ?: 'default'; $inline_stack[$slot][] = $asset['data']; unset($collection[$i]); unset($asset); continue; } $path = $asset['path']; $exclude = $this->f3->get('ASSETS.minify.exclude'); // skip external files if ($asset['origin'] == 'external') { continue; } elseif (is_file($path) && ((!isset($asset['exclude']) || !in_array('minify', $this->f3->split($asset['exclude']))) && (empty($exclude) || !preg_match('/' . $exclude . '/i', $path)))) { // proceed $path_parts = pathinfo($path); $filename = $path_parts['filename'] . '.min.' . $type; if (!is_file($public_path . $filename) || filemtime($path) > filemtime($public_path . $filename)) { $min = $web->minify($path_parts['basename'], null, false, $path_parts['dirname'] . '/'); if ($type == 'css') { $min = $this->fixRelativePaths($min, $path_parts['dirname'] . '/'); } $this->f3->write($public_path . $filename, $min); } $asset['path'] = $public_path . $filename; } unset($asset); } if (!empty($inline_stack)) { foreach ($inline_stack as $slotGroup => $inlineData) { $data = implode($inlineData); if ($this->f3->get('ASSETS.minify.inline')) { // this is probably pretty slow $hash = $this->f3->hash($data); $filename = $hash . '.min.' . $type; if (!is_file($public_path . $filename)) { $this->f3->write($public_path . $filename, $data); $min = $web->minify($filename, null, false, $public_path); $this->f3->write($public_path . $filename, $min); } $collection[] = array('path' => $public_path . $filename, 'type' => $type, 'origin' => 'internal', 'slot' => $slotGroup); } else { $collection[] = array('data' => $data, 'type' => $type, 'origin' => 'inline', 'slot' => $slotGroup); } } } return $collection; }
/** * compute file cache name * @param $file * @return string */ protected function getCacheHash($file) { if (is_null($this->cacheHash)) { $fs_class = explode('\\', get_class($this->fs)); $this->cacheHash = $this->f3->hash($this->f3->stringify($file)) . '.' . strtolower(array_pop($fs_class)); } return $this->cacheHash; }
/** * @param \Base $f3 * @param array $params * @throws \Exception */ public function thumb($f3, $params) { $this->_useFileCache(); $cache = \Cache::instance(); // Ensure proper content-type for JPEG images if ($params["format"] == "jpg") { $params["format"] = "jpeg"; } // Output cached image if one exists $hash = $f3->hash($f3->get('VERB') . " " . $f3->get('URI')) . ".thm"; if ($cache->exists($hash, $data)) { header("Content-type: image/" . $params["format"]); echo $data; return; } $file = new \Model\Issue\File(); $file->load($params["id"]); if (!$file->id) { $f3->error(404); return; } $fg = 0x0; $bg = 0xffffff; // Generate thumbnail of image file if (substr($file->content_type, 0, 6) == "image/") { if (is_file($file->disk_filename)) { $img = new \Helper\Image($file->disk_filename); $hide_ext = true; } else { $protocol = isset($_SERVER["SERVER_PROTOCOL"]) ? $_SERVER["SERVER_PROTOCOL"] : "HTTP/1.0"; header($protocol . " 404 Not Found"); $img = new \Helper\Image("img/404.png"); } $img->resize($params["size"], $params["size"]); $fg = 0xffffff; $bg = 0x0; } elseif (substr($file->content_type, 0, 5) == "text/") { // Get first 2KB of file $fh = fopen($file->disk_filename, "r"); $str = fread($fh, 2048); fclose($fh); // Replace tabs with spaces $str = str_replace("\t", " ", $str); $img = new \Helper\Image(); $img->create($params["size"], $params["size"]); $img->fill(0xffffff); $img->text($str, round(0.05 * $params["size"]), 0, round(0.03 * $params["size"]), round(0.03 * $params["size"]), 0x777777); // Show file type icon if available if ($file->content_type == "text/csv" || $file->content_type == "text/tsv") { $icon = new \Image("img/mime/table.png"); $img->overlay($icon); } } elseif (extension_loaded("zip") && $file->content_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") { $zip = zip_open($file->disk_filename); while (($entry = zip_read($zip)) !== false) { if (preg_match("/word\\/media\\/image[0-9]+\\.(png|jpe?g|gif|bmp|dib)/i", zip_entry_name($entry))) { $idata = zip_entry_read($entry, zip_entry_filesize($entry)); $img = new \Helper\Image(); $img->load($idata); break; } } if (!isset($img)) { $img = new \Helper\Image("img/mime/base.png"); } $img->resize($params["size"], $params["size"]); } else { $img = new \Helper\Image("img/mime/base.png"); $img->resize($params["size"], $params["size"]); } // Render file extension over image if (empty($hide_ext)) { $ext = strtoupper(pathinfo($file->disk_filename, PATHINFO_EXTENSION)); $img->text($ext, $params["size"] * 0.125, 0, round(0.05 * $params["size"]), round(0.05 * $params["size"]), $bg); $img->text($ext, $params["size"] * 0.125, 0, round(0.05 * $params["size"]) - 1, round(0.05 * $params["size"]) - 1, $fg); } // Render and cache image $data = $img->dump($params["format"]); $cache->set($hash, $data, $f3->get("cache_expire.attachments")); // Output image header("Content-type: image/" . $params["format"]); echo $data; }