/** * @param null $thumbnailName * @return mixed */ public function getThumbnail($thumbnailName = null) { if (!$this->getImage()) { return ""; } $crop = null; if (is_array($this->getCrop())) { $crop = $this->getCrop(); } $thumbConfig = $this->getImage()->getThumbnailConfig($thumbnailName); if (!$thumbConfig && $crop) { $thumbConfig = new Asset\Image\Thumbnail\Config(); } if ($crop) { $thumbConfig->addItemAt(0, "cropPercent", array("width" => $crop["cropWidth"], "height" => $crop["cropHeight"], "y" => $crop["cropTop"], "x" => $crop["cropLeft"])); $hash = md5(\Pimcore\Tool\Serialize::serialize($thumbConfig->getItems())); $thumbConfig->setName($thumbConfig->getName() . "_auto_" . $hash); } return $this->getImage()->getThumbnail($thumbConfig); }
public function thumbnailAddAction() { $this->checkPermission("thumbnails"); $alreadyExist = false; try { Asset\Image\Thumbnail\Config::getByName($this->getParam("name")); $alreadyExist = true; } catch (\Exception $e) { $alreadyExist = false; } if (!$alreadyExist) { $pipe = new Asset\Image\Thumbnail\Config(); $pipe->setName($this->getParam("name")); $pipe->save(); } $this->_helper->json(array("success" => !$alreadyExist, "id" => $pipe->getName())); }
public function thumbnailAddAction() { $this->checkPermission("thumbnails"); $success = false; $pipe = Asset\Image\Thumbnail\Config::getByName($this->getParam("name")); if (!$pipe) { $pipe = new Asset\Image\Thumbnail\Config(); $pipe->setName($this->getParam("name")); $pipe->save(); $success = true; } $this->_helper->json(["success" => $success, "id" => $pipe->getName()]); }
/** * @see Document\Tag\TagInterface::frontend * @return string */ public function frontend() { if (!is_array($this->options)) { $this->options = array(); } $image = $this->getImage(); if ($image instanceof Asset) { if (isset($this->options["thumbnail"]) && $this->options["thumbnail"] || $this->cropPercent) { // create a thumbnail first $autoName = false; $thumbConfig = $image->getThumbnailConfig($this->options["thumbnail"]); if (!$thumbConfig && $this->cropPercent) { $thumbConfig = new Asset\Image\Thumbnail\Config(); } if ($this->cropPercent) { $cropConfig = array("width" => $this->cropWidth, "height" => $this->cropHeight, "y" => $this->cropTop, "x" => $this->cropLeft); $thumbConfig->addItemAt(0, "cropPercent", $cropConfig); // also crop media query specific configs if ($thumbConfig->hasMedias()) { foreach ($thumbConfig->getMedias() as $mediaName => $mediaItems) { $thumbConfig->addItemAt(0, "cropPercent", $cropConfig, $mediaName); } } $autoName = true; } if ($this->options["highResolution"] && $this->options["highResolution"] > 1) { $thumbConfig->setHighResolution($this->options["highResolution"]); } // autogenerate a name for the thumbnail because it's different from the original if ($autoName) { $hash = md5(Serialize::serialize($thumbConfig->getItems())); $thumbConfig->setName($thumbConfig->getName() . "_auto_" . $hash); } $imagePath = $image->getThumbnail($thumbConfig); } else { $imagePath = $image->getFullPath(); } $altText = $this->alt; $titleText = $this->alt; if (empty($titleText)) { if ($this->getImage()->getMetadata("title")) { $titleText = $this->getImage()->getMetadata("title"); } } if (empty($altText)) { if ($this->getImage()->getMetadata("alt")) { $altText = $this->getImage()->getMetadata("alt"); } else { $altText = $titleText; } } // get copyright from asset if ($this->getImage()->getMetadata("copyright")) { if (!empty($altText)) { $altText .= " | "; } if (!empty($titleText)) { $titleText .= " | "; } $altText .= "© " . $this->getImage()->getMetadata("copyright"); $titleText .= "© " . $this->getImage()->getMetadata("copyright"); } $defaultAttributes = array("alt" => $altText); if (!empty($titleText)) { $defaultAttributes["title"] = $titleText; } // add attributes to image $allowedAttributes = array("alt", "align", "border", "height", "hspace", "ismap", "longdesc", "usemap", "vspace", "width", "class", "dir", "id", "lang", "style", "title", "xml:lang", "onmouseover", "onabort", "onclick", "ondblclick", "onmousedown", "onmousemove", "onmouseout", "onmouseup", "onkeydown", "onkeypress", "onkeyup", "itemprop", "itemscope", "itemtype", "disableWidthHeightAttributes"); $htmlEscapeAttributes = array("alt", "align", "border", "height", "hspace", "longdesc", "usemap", "vspace", "width", "class", "dir", "id", "lang", "title"); $customAttributes = array(); if (array_key_exists("attributes", $this->options) && is_array($this->options["attributes"])) { $customAttributes = $this->options["attributes"]; } $availableAttribs = array_merge($this->options, $defaultAttributes, $customAttributes); $attribs = []; $attribsRaw = []; foreach ($availableAttribs as $key => $value) { if ((is_string($value) || is_numeric($value) || is_bool($value)) && (in_array($key, $allowedAttributes) || array_key_exists($key, $customAttributes))) { $attribsRaw[$key] = $value; if (in_array($key, $htmlEscapeAttributes)) { $value = htmlspecialchars($value); } $attribs[] = $key . '="' . $value . '"'; } } if ($imagePath instanceof Asset\Image\Thumbnail) { // thumbnail's HTML is always generated by the thumbnail itself return $imagePath->getHTML($attribsRaw); } else { return '<img src="' . $imagePath . '" ' . implode(" ", $attribs) . ' />'; } } }
public function downloadImageThumbnailAction() { $image = Asset\Image::getById($this->getParam("id")); $config = null; if ($this->getParam("config")) { $config = \Zend_Json::decode($this->getParam("config")); } elseif ($this->getParam("type")) { $predefined = ["web" => ["resize_mode" => "scaleByWidth", "width" => 3500, "dpi" => 72, "format" => "JPEG", "quality" => 85], "print" => ["resize_mode" => "scaleByWidth", "width" => 6000, "dpi" => 300, "format" => "JPEG", "quality" => 95], "office" => ["resize_mode" => "scaleByWidth", "width" => 1190, "dpi" => 144, "format" => "JPEG", "quality" => 90]]; $config = $predefined[$this->getParam("type")]; } if ($config) { $thumbnailConfig = new Asset\Image\Thumbnail\Config(); $thumbnailConfig->setName("pimcore-download-" . $image->getId() . "-" . md5($this->getParam("config"))); if ($config["resize_mode"] == "scaleByWidth") { $thumbnailConfig->addItem("scaleByWidth", ["width" => $config["width"]]); } elseif ($config["resize_mode"] == "scaleByHeight") { $thumbnailConfig->addItem("scaleByHeight", ["height" => $config["height"]]); } else { $thumbnailConfig->addItem("resize", ["width" => $config["width"], "height" => $config["height"]]); } $thumbnailConfig->setQuality($config["quality"]); $thumbnailConfig->setFormat($config["format"]); if ($thumbnailConfig->getFormat() == "JPEG") { $thumbnailConfig->setPreserveMetaData(true); $thumbnailConfig->setPreserveColor(true); } $thumbnail = $image->getThumbnail($thumbnailConfig); $thumbnailFile = $thumbnail->getFileSystemPath(); $exiftool = \Pimcore\Tool\Console::getExecutable("exiftool"); if ($thumbnailConfig->getFormat() == "JPEG" && $exiftool && isset($config["dpi"]) && $config["dpi"]) { \Pimcore\Tool\Console::exec($exiftool . " -overwrite_original -xresolution=" . $config["dpi"] . " -yresolution=" . $config["dpi"] . " -resolutionunit=inches " . escapeshellarg($thumbnailFile)); } $downloadFilename = str_replace("." . File::getFileExtension($image->getFilename()), "." . $thumbnail->getFileExtension(), $image->getFilename()); $downloadFilename = strtolower($downloadFilename); header('Content-Disposition: attachment; filename="' . $downloadFilename . '"'); header("Content-Type: " . $thumbnail->getMimeType(), true); // we have to clear the stat cache here, otherwise filesize() would return the wrong value // the reason is that exiftool modifies the file's size, but PHP doesn't know anything about that clearstatcache(); header("Content-Length: " . filesize($thumbnailFile), true); $this->sendThumbnailCacheHeaders(); while (@ob_end_flush()) { } flush(); readfile($thumbnailFile); @unlink($thumbnailFile); exit; } }