/** * @static * @param Asset_Image_Thumbnail_Config $config * @return string */ public static function process(Asset_Image $asset, Asset_Image_Thumbnail_Config $config) { $format = strtolower($config->getFormat()); // simple detection for source type if SOURCE is selected if ($format == "source") { $typeMapping = array("gif" => "gif", "jpeg" => "jpeg", "jpg" => "jpeg", "png" => "png"); $fileExt = Pimcore_File::getFileExtension($asset->getFilename()); if ($typeMapping[$fileExt]) { $format = $typeMapping[$fileExt]; } else { // use PNG if source doesn't have a valid mapping $format = "png"; } } $filename = "thumb_" . $asset->getId() . "__" . $config->getName() . "." . $format; $fsPath = PIMCORE_TEMPORARY_DIRECTORY . "/" . $filename; $path = str_replace(PIMCORE_DOCUMENT_ROOT, "", $fsPath); // check for existing and still valid thumbnail if (is_file($fsPath) and filemtime($fsPath) > $asset->getModificationDate()) { return $path; } // transform image $image = Asset_Image::getImageTransformInstance(); if (!$image->load($asset->getFileSystemPath())) { return "/pimcore/static/img/image-not-supported.png"; } $transformations = $config->getItems(); if (is_array($transformations) && count($transformations) > 0) { foreach ($transformations as $transformation) { if (!empty($transformation)) { $arguments = array(); $mapping = self::$argumentMapping[$transformation["method"]]; if (is_array($transformation["arguments"])) { foreach ($transformation["arguments"] as $key => $value) { $position = array_search($key, $mapping); if ($position !== false) { $arguments[$position] = $value; } } } ksort($arguments); if (count($mapping) == count($arguments)) { call_user_func_array(array($image, $transformation["method"]), $arguments); } else { $message = "Image Transform failed: cannot call method `" . $transformation["method"] . "´ with arguments `" . implode(",", $arguments) . "´ because there are too few arguments"; Logger::error($message); } } } } $image->save($fsPath, $format, $config->getQuality()); return $path; }
/** * @see Document_Tag_Interface::frontend * @return string */ public function frontend() { if ($this->image instanceof Asset) { $thumbnailInUse = false; if ($this->options["thumbnail"]) { // create a thumbnail first $thumbConfig = $this->image->getThumbnailConfig($this->options["thumbnail"]); if ($this->cropPercent) { $thumbConfig->addItemAt(0, "cropPercent", array("width" => $this->cropWidth, "height" => $this->cropHeight, "y" => $this->cropTop, "x" => $this->cropLeft)); $hash = md5(Pimcore_Tool_Serialize::serialize($thumbConfig->getItems())); $thumbConfig->setName("auto_" . $hash); } if ($imagePath = $this->image->getThumbnail($thumbConfig)) { list($width, $height) = getimagesize(PIMCORE_DOCUMENT_ROOT . $imagePath); $thumbnailInUse = true; } } if (!$thumbnailInUse) { $imagePath = $this->image->getPath() . $this->image->getFilename(); // width & height $options = $this->getOptions(); if ($options["width"]) { $width = $options["width"]; } if ($options["height"]) { $height = $options["height"]; } } // 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"); $htmlEscapeAttributes = array("alt", "align", "border", "height", "hspace", "longdesc", "usemap", "vspace", "width", "class", "dir", "id", "lang", "title"); $defaultAttributes = array("alt" => $this->alt, "title" => $this->alt, "height" => $height, "width" => $width); if (!is_array($this->options)) { $this->options = array(); } $customAttributes = array(); if (array_key_exists("attributes", $this->options) && is_array($this->options["attributes"])) { $customAttributes = $this->options["attributes"]; } $availableAttribs = array_merge($defaultAttributes, $customAttributes, $this->options); foreach ($availableAttribs as $key => $value) { if ((is_string($value) || is_numeric($value)) && (in_array($key, $allowedAttributes) || array_key_exists($key, $customAttributes))) { if (in_array($key, $htmlEscapeAttributes)) { $value = htmlspecialchars($value); } $attribs[] = $key . '="' . $value . '"'; } } return '<img src="' . $imagePath . '" ' . implode(" ", $attribs) . ' />'; } }