/** * @static * @param Asset_Image|Asset_Video|string * @param Asset_Image_Thumbnail_Config $config * @param string $path * @return string */ public static function process($asset, Asset_Image_Thumbnail_Config $config, $fileSystemPath = null) { $format = strtolower($config->getFormat()); if (!$fileSystemPath) { $fileSystemPath = $asset->getFileSystemPath(); } // 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($fileSystemPath)) { 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; }
/** * @param $config * @return Asset_Image_Thumbnail|bool|Thumbnail */ public function getThumbnailConfig($config) { if (is_string($config)) { try { $thumbnail = Asset_Image_Thumbnail_Config::getByName($config); } catch (Exception $e) { Logger::error("requested thumbnail " . $config . " is not defined"); return false; } } else { if (is_array($config)) { // check if it is a legacy config or a new one if (array_key_exists("items", $config)) { $thumbnail = Asset_Image_Thumbnail_Config::getByArrayConfig($config); } else { $thumbnail = Asset_Image_Thumbnail_Config::getByLegacyConfig($config); } } else { if ($config instanceof Asset_Image_Thumbnail_Config) { $thumbnail = $config; } } } return $thumbnail; }
public function thumbnailUpdateAction() { $pipe = Asset_Image_Thumbnail_Config::getByName($this->_getParam("name")); $data = Zend_Json::decode($this->_getParam("configuration")); $items = array(); foreach ($data as $key => $value) { $setter = "set" . ucfirst($key); if (method_exists($pipe, $setter)) { $pipe->{$setter}($value); } if (strpos($key, "item.") === 0) { $cleanKeyParts = explode(".", $key); $items[$cleanKeyParts[1]][$cleanKeyParts[2]] = $value; } } $pipe->resetItems(); foreach ($items as $item) { $type = $item["type"]; unset($item["type"]); $pipe->addItem($type, $item); } $pipe->save(); $this->deleteThumbnailTmpFiles($pipe); $this->_helper->json(array("success" => true)); }
/** * This is just for compatibility, this method will be removed with the next major release * @depricated * @static * @param $config * @return Asset_Image_Thumbnail_Config */ public static function getByLegacyConfig($config) { $pipe = new Asset_Image_Thumbnail_Config(); $hash = md5(Pimcore_Tool_Serialize::serialize($config)); $pipe->setName("auto_" . $hash); if ($config["format"]) { $pipe->setFormat($config["format"]); } if ($config["quality"]) { $pipe->setQuality($config["quality"]); } /*if ($config["cropPercent"]) { $pipe->addItem("cropPercent", array( "width" => $config["cropWidth"], "height" => $config["cropHeight"], "y" => $config["cropTop"], "x" => $config["cropLeft"] )); }*/ if ($config["cover"]) { $pipe->addItem("cover", array("width" => $config["width"], "height" => $config["height"], "positioning" => "center")); } else { if ($config["contain"]) { $pipe->addItem("contain", array("width" => $config["width"], "height" => $config["height"])); } else { if ($config["frame"]) { $pipe->addItem("frame", array("width" => $config["width"], "height" => $config["height"])); } else { if ($config["aspectratio"]) { if ($config["height"] > 0 && $config["width"] > 0) { $pipe->addItem("contain", array("width" => $config["width"], "height" => $config["height"])); } else { if ($config["height"] > 0) { $pipe->addItem("scaleByHeight", array("height" => $config["height"])); } else { $pipe->addItem("scaleByWidth", array("width" => $config["width"])); } } } else { if (empty($config["width"]) && !empty($config["height"])) { $pipe->addItem("scaleByHeight", array("height" => $config["height"])); } else { if (!empty($config["width"]) && empty($config["height"])) { $pipe->addItem("scaleByWidth", array("width" => $config["width"])); } else { $pipe->addItem("resize", array("width" => $config["width"], "height" => $config["height"])); } } } } } } return $pipe; }
echo $e->getMessage(); } // display help message if ($opts->getOption("help")) { echo $opts->getUsageMessage(); exit; } if ($opts->getOption("verbose")) { $writer = new Zend_Log_Writer_Stream('php://output'); $logger = new Zend_Log($writer); Logger::addLogger($logger); // set all priorities Logger::setPriorities(array(Zend_Log::DEBUG, Zend_Log::INFO, Zend_Log::NOTICE, Zend_Log::WARN, Zend_Log::ERR, Zend_Log::CRIT, Zend_Log::ALERT, Zend_Log::EMERG)); } // get all thumbnails $dir = Asset_Image_Thumbnail_Config::getWorkingDir(); $thumbnails = array(); $files = scandir($dir); foreach ($files as $file) { if (strpos($file, ".xml")) { $thumbnails[] = str_replace(".xml", "", $file); } } $allowedThumbs = array(); if ($opts->getOption("thumbnails")) { $allowedThumbs = explode(",", $opts->getOption("thumbnails")); } // get only images $conditions = array("type = 'image'"); if ($opts->getOption("parent")) { $parent = Asset::getById($opts->getOption("parent"));
<?php // rebuild thumbnails that have "cover" transformation in it $dir = Asset_Image_Thumbnail_Config::getWorkingDir(); $files = scandir($dir); foreach ($files as $file) { $found = false; if (strpos($file, ".xml")) { $name = str_replace(".xml", "", $file); $thumbnail = Asset_Image_Thumbnail_Config::getByName($name); foreach ($thumbnail->items as &$item) { if ($item["method"] == "cover") { $item["arguments"]["doNotScaleUp"] = "1"; $found = true; } } if ($found) { unset($thumbnail->filenameSuffix); $thumbnail->save(); } } }
public function thumbnailUpdateAction() { $pipe = Asset_Image_Thumbnail_Config::getByName($this->_getParam("name")); $settingsData = Zend_Json::decode($this->_getParam("settings")); $itemsData = Zend_Json::decode($this->_getParam("items")); foreach ($settingsData as $key => $value) { $setter = "set" . ucfirst($key); if (method_exists($pipe, $setter)) { $pipe->{$setter}($value); } } $pipe->resetItems(); foreach ($itemsData as $item) { $type = $item["type"]; unset($item["type"]); $pipe->addItem($type, $item); } $pipe->save(); $this->deleteThumbnailTmpFiles($pipe); $this->_helper->json(array("success" => true)); }
<?php $list = new Asset_Image_Thumbnail_List(); $list->load(); $thumbnails = array(); foreach ($list->getThumbnails() as $thumbnail) { $pipe = new Asset_Image_Thumbnail_Config(); $pipe->setName($thumbnail->getName()); $pipe->setDescription($thumbnail->getDescription()); $pipe->setFormat($thumbnail->getFormat()); $pipe->setQuality($thumbnail->getQuality()); if ($thumbnail->getCover()) { $pipe->addItem("cover", array("width" => $thumbnail->getWidth(), "height" => $thumbnail->getHeight(), "positioning" => "center")); } else { if ($thumbnail->getContain()) { $pipe->addItem("contain", array("width" => $thumbnail->getWidth(), "height" => $thumbnail->getHeight())); } else { if ($thumbnail->getAspectratio()) { if ($thumbnail->getHeight() > 0 && $thumbnail->getWidth() > 0) { $pipe->addItem("contain", array("width" => $thumbnail->getWidth(), "height" => $thumbnail->getHeight())); } else { if ($thumbnail->getHeight() > 0) { $pipe->addItem("scaleByHeight", array("height" => $thumbnail->getHeight())); } else { $pipe->addItem("scaleByWidth", array("width" => $thumbnail->getWidth())); } } } else { $pipe->addItem("resize", array("width" => $thumbnail->getWidth(), "height" => $thumbnail->getHeight())); } }