示例#1
0
 /**
  * @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;
 }