public function write($filepath, $mimetype) { if (!$this->handle) { return false; } // 75 is about the default value of imagejpeg() so it seems like a reasonable default for us as well $vn_jpeg_quality = isset($this->properties["quality"]) && $this->properties["quality"] ? intval($this->properties["quality"]) : 75; if ($mimetype == "image/tilepic") { if ($this->properties["mimetype"] == "image/tilepic") { copy($this->filepath, $filepath); } else { $tp = new TilepicParser(); $tp->useLibrary(LIBRARY_GD); if (!($properties = $tp->encode($this->filepath, $filepath, array("tile_width" => $this->properties["tile_width"], "tile_height" => $this->properties["tile_height"], "layer_ratio" => $this->properties["layer_ratio"], "quality" => $vn_jpeg_quality, "antialiasing" => $this->properties["antialiasing"], "output_mimetype" => $this->properties["tile_mimetype"], "layers" => $this->properties["layers"])))) { $this->postError(1610, $tp->error, "WLPlugTilepic->write()"); return false; } } # update mimetype foreach ($properties as $k => $v) { $this->properties[$k] = $v; } $this->properties["mimetype"] = "image/tilepic"; $this->properties["typename"] = "Tilepic"; return $filepath; } else { # is mimetype valid? if (!($ext = $this->info["EXPORT"][$mimetype])) { # this plugin can't write this mimetype return false; } # get layer out of Tilepic if ($this->properties["mimetype"] == "image/tilepic") { if (!($h = $this->handle->getLayer($this->properties["output_layer"] ? $this->properties["output_layer"] : intval($this->properties["layers"] / 2.0), $mimetype))) { $this->postError(1610, $this->handle->error, "WLPlugTilepic->write()"); return false; } $this->handle = $h; } $vn_res = 0; switch ($mimetype) { case 'image/gif': $vn_res = imagegif($this->handle, $filepath . "." . $ext); $vs_typename = "GIF"; break; case 'image/jpeg': $vn_res = imagejpeg($this->handle, $filepath . "." . $ext, $vn_jpeg_quality); $vs_typename = "JPEG"; break; case 'image/png': $vn_res = imagepng($this->handle, $filepath . "." . $ext); $vs_typename = "PNG"; break; } # write the file if (!$vn_res) { # error $this->postError(1610, _t("Couldn't write image"), "WLPlugGD->write()"); return false; } # update mimetype $this->properties["mimetype"] = $mimetype; $this->properties["typename"] = $vs_typename; return $filepath . "." . $ext; } }
public function write($ps_filepath, $mimetype) { if (!$this->handle) { return false; } if (strpos($ps_filepath, ':') && caGetOSFamily() != OS_WIN32) { $this->postError(1610, _t("Filenames with colons (:) are not allowed"), "WLPlugGmagick->write()"); return false; } if ($mimetype == "image/tilepic") { if ($this->properties["mimetype"] == "image/tilepic") { copy($this->filepath, $ps_filepath); } else { $tp = new TilepicParser(); if (!($properties = $tp->encode($this->filepath, $ps_filepath, array("tile_width" => $this->properties["tile_width"], "tile_height" => $this->properties["tile_height"], "layer_ratio" => $this->properties["layer_ratio"], "quality" => $this->properties["quality"], "antialiasing" => $this->properties["antialiasing"], "output_mimetype" => $this->properties["tile_mimetype"], "layers" => $this->properties["layers"])))) { $this->postError(1610, $tp->error, "WLPlugTilepic->write()"); return false; } } # update mimetype foreach ($properties as $k => $v) { $this->properties[$k] = $v; } $this->properties["mimetype"] = "image/tilepic"; $this->properties["typename"] = "Tilepic"; return 1; } else { # is mimetype valid? if (!($ext = $this->info["EXPORT"][$mimetype])) { # this plugin can't write this mimetype return false; } $this->handle->setimageformat($this->magick_names[$mimetype]); # set quality if ($this->properties["quality"] && $this->properties["mimetype"] != "image/tiff") { $this->handle->setcompressionquality($this->properties["quality"]); } $this->handle->setimagebackgroundcolor(new GmagickPixel("#CC0000")); if ($this->properties['gamma']) { if (!$this->properties['reference-black']) { $this->properties['reference-black'] = 0; } if (!$this->properties['reference-white']) { $this->properties['reference-white'] = 65535; } $this->handle->levelimage($this->properties['reference-black'], $this->properties['gamma'], $this->properties['reference-white']); } $this->handle->stripimage(); // remove all lingering metadata # write the file try { if (!$this->handle->writeimage($ps_filepath . "." . $ext)) { $this->postError(1610, _t("Error writing file"), "WLPlugGmagick->write()"); return false; } if (!file_exists($ps_filepath . "." . $ext)) { if ($this->handle->getnumberimages() > 1) { if (file_exists($ps_filepath . "-1." . $ext)) { @rename($ps_filepath . "-0." . $ext, $ps_filepath . "." . $ext); $this->properties["mimetype"] = $mimetype; $this->properties["typename"] = $this->handle->getimageformat(); // get rid of other pages $i = 1; while (file_exists($vs_f = $ps_filepath . "-" . $i . "." . $ext)) { @unlink($vs_f); $i++; } return $ps_filepath . "." . $ext; } } $this->postError(1610, _t("Error writing file"), "WLPlugGmagick->write()"); return false; } } catch (Exception $e) { $this->postError(1610, _t("Error writing file: %1", $e->getMessage()), "WLPlugGmagick->write()"); return false; } # update mimetype $this->properties["mimetype"] = $mimetype; $this->properties["typename"] = $this->handle->getimageformat(); return $ps_filepath . "." . $ext; } }
public static function getTileQuickly($ps_filepath, $pn_tile_number, $pb_print_errors = true) { # --- Tile numbers start at 1, *NOT* 0 in parameter! if ($fh = @fopen($ps_filepath, 'r')) { # look for signature $sig = fread($fh, 4); if (preg_match("/TPC\n/", $sig)) { $buf = fread($fh, 4); $x = unpack("Nheader_size", $buf); if ($x['header_size'] <= 8) { if ($pb_print_errors) { print "Tilepic header length is invalid"; } return false; } # --- get tile offsets (start of each tile) if (!fseek($fh, $x['header_size'] + ($pn_tile_number - 1) * 4)) { $x = unpack("Noffset", fread($fh, 4)); $y = unpack("Noffset", fread($fh, 4)); $x["offset"] = TilepicParser::unpackLargeInt($x["offset"]); $y["offset"] = TilepicParser::unpackLargeInt($y["offset"]); $vn_len = $y["offset"] - $x["offset"]; if (!fseek($fh, $x["offset"])) { return fread($fh, $vn_len); return true; } else { if ($pb_print_errors) { print "File seek error while getting tile; tried to seek to " . $x["offset"] . " and read {$vn_len} bytes"; } return false; } } else { if ($pb_print_errors) { print "File seek error while getting tile offset"; } return false; } } else { if ($pb_print_errors) { print "File is not Tilepic format"; } return false; } } else { if ($pb_print_errors) { print "Couldn't open file {$ps_filepath}"; } return false; } }
public function write($filepath, $mimetype) { if (!$this->handle) { return false; } if (strpos($filepath, ':') && caGetOSFamily() != OS_WIN32) { $this->postError(1610, _t("Filenames with colons (:) are not allowed"), "WLPlugImageMagick->write()"); return false; } if ($mimetype == "image/tilepic") { if ($this->properties["mimetype"] == "image/tilepic") { copy($this->filepath, $filepath); } else { $tp = new TilepicParser(); if (!($properties = $tp->encode($this->filepath, $filepath, array("tile_width" => $this->properties["tile_width"], "tile_height" => $this->properties["tile_height"], "layer_ratio" => $this->properties["layer_ratio"], "quality" => $this->properties["quality"], "antialiasing" => $this->properties["antialiasing"], "output_mimetype" => $this->properties["tile_mimetype"], "layers" => $this->properties["layers"])))) { $this->postError(1610, $tp->error, "WLPlugTilepic->write()"); return false; } } # update mimetype foreach ($properties as $k => $v) { $this->properties[$k] = $v; } $this->properties["mimetype"] = "image/tilepic"; $this->properties["typename"] = "Tilepic"; return 1; } else { # is mimetype valid? if (!($ext = $this->info["EXPORT"][$mimetype])) { # this plugin can't write this mimetype return false; } if (!$this->_graphicsMagickWrite($this->handle, $filepath . "." . $ext, $mimetype, $this->properties["quality"])) { $this->postError(1610, _t("Could not write file %1", $filepath . "." . $ext), "WLPlugImageMagick->write()"); return false; } # update mimetype $this->properties["mimetype"] = $mimetype; $this->properties["typename"] = $this->magick_names[$mimetype]; return $filepath . "." . $ext; } }
* * This source code is free and modifiable under the terms of * GNU General Public License. (http://www.gnu.org/copyleft/gpl.html). See * the "license.txt" file for details, or visit the CollectiveAccess web site at * http://www.CollectiveAccess.org * * ---------------------------------------------------------------------- */ # Only works with JPEG tiles; we skip extracting tile mimetype from the Tilepic file # to save time. If you are using non-JPEG tiles (unlikely, right?) then change the # Content-type header below. require "../../setup.php"; require_once __CA_LIB_DIR__ . "/core/Parsers/TilepicParser.php"; require_once __CA_LIB_DIR__ . "/core/Configuration.php"; $vo_conf = Configuration::load(); $ps_filepath = $_REQUEST["p"]; $pn_tile = $_REQUEST["t"]; $ps_filepath = preg_replace("/^http[s]{0,1}:\\/\\/[^\\/]+/i", "", $ps_filepath); $ps_filepath = preg_replace("/\\.tpc\$/", "", $ps_filepath); $ps_filepath = str_replace($vo_conf->get('ca_media_url_root'), "", $ps_filepath); $ps_filepath = preg_replace("/[^A-Za-z0-9_\\-\\/]/", "", $ps_filepath); $vs_media_root = $vo_conf->get('ca_media_root_dir'); if (file_exists($vs_media_root . "/" . $ps_filepath . ".tpc")) { header("Content-type: image/jpeg"); $vs_output = TilepicParser::getTileQuickly($vs_media_root . "/" . $ps_filepath . ".tpc", $pn_tile); header("Content-Length: " . strlen($vs_output)); print $vs_output; exit; } else { die("Invalid file"); }