/** * */ public function generate() { $errorImage = PIMCORE_PATH . '/static6/img/filetype-not-supported.png'; $deferred = false; $generated = false; if (!$this->asset) { $this->filesystemPath = $errorImage; } elseif (!$this->filesystemPath) { $cs = $this->asset->getCustomSetting("image_thumbnail_time"); $im = $this->asset->getCustomSetting("image_thumbnail_asset"); if ($im || $this->imageAsset) { if ($this->imageAsset) { $im = $this->imageAsset; } else { $im = Model\Asset::getById($im); } if ($im instanceof Image) { $imageThumbnail = $im->getThumbnail($this->getConfig()); $this->filesystemPath = $imageThumbnail->getFileSystemPath(); } } if (!$this->filesystemPath) { $timeOffset = $this->timeOffset; if (!$this->timeOffset && $cs) { $timeOffset = $cs; } // fallback if (!$timeOffset) { $timeOffset = ceil($this->asset->getDuration() / 3); } $converter = \Pimcore\Video::getInstance(); $converter->load($this->asset->getFileSystemPath()); $path = PIMCORE_TEMPORARY_DIRECTORY . "/video-image-cache/video_" . $this->asset->getId() . "__thumbnail_" . $timeOffset . ".png"; if (!is_dir(dirname($path))) { File::mkdir(dirname($path)); } if (!is_file($path)) { $lockKey = "video_image_thumbnail_" . $this->asset->getId() . "_" . $timeOffset; Model\Tool\Lock::acquire($lockKey); // after we got the lock, check again if the image exists in the meantime - if not - generate it if (!is_file($path)) { $converter->saveImage($path, $timeOffset); $generated = true; } Model\Tool\Lock::release($lockKey); } if ($this->getConfig()) { $this->getConfig()->setFilenameSuffix("time-" . $timeOffset); try { $path = Image\Thumbnail\Processor::process($this->asset, $this->getConfig(), $path, $deferred, true, $generated); } catch (\Exception $e) { Logger::error("Couldn't create image-thumbnail of video " . $this->asset->getRealFullPath()); Logger::error($e); $path = $errorImage; } } $this->filesystemPath = $path; } \Pimcore::getEventManager()->trigger("asset.video.image-thumbnail", $this, ["deferred" => $deferred, "generated" => $generated]); } }
/** * @param $thumbnailName * @param int $page * @param bool $deferred $deferred deferred means that the image will be generated on-the-fly (details see below) * @return mixed|string */ public function getImageThumbnail($thumbnailName, $page = 1, $deferred = false) { // just 4 testing //$this->clearThumbnails(true); if (!\Pimcore\Document::isAvailable()) { \Logger::error("Couldn't create image-thumbnail of document " . $this->getFullPath() . " no document adapter is available"); return "/pimcore/static/img/filetype-not-supported.png"; } $thumbnail = Image\Thumbnail\Config::getByAutoDetect($thumbnailName); $thumbnail->setName("document_" . $thumbnail->getName() . "-" . $page); try { if (!$deferred) { $converter = \Pimcore\Document::getInstance(); $converter->load($this->getFileSystemPath()); $path = PIMCORE_TEMPORARY_DIRECTORY . "/document-image-cache/document_" . $this->getId() . "__thumbnail_" . $page . ".png"; if (!is_dir(dirname($path))) { \Pimcore\File::mkdir(dirname($path)); } $lockKey = "document-thumbnail-" . $this->getId() . "-" . $page; if (!is_file($path) && !Model\Tool\Lock::isLocked($lockKey)) { Model\Tool\Lock::lock($lockKey); $converter->saveImage($path, $page); Model\Tool\Lock::release($lockKey); } else { if (Model\Tool\Lock::isLocked($lockKey)) { return "/pimcore/static/img/please-wait.png"; } } } if ($thumbnail) { $path = Image\Thumbnail\Processor::process($this, $thumbnail, $path, $deferred); } return preg_replace("@^" . preg_quote(PIMCORE_DOCUMENT_ROOT) . "@", "", $path); } catch (\Exception $e) { \Logger::error("Couldn't create image-thumbnail of document " . $this->getFullPath()); \Logger::error($e); } return "/pimcore/static/img/filetype-not-supported.png"; }
/** * @param $thumbnailName * @param null $timeOffset * @param null $imageAsset * @return mixed|string * @throws \Exception */ public function getImageThumbnail($thumbnailName, $timeOffset = null, $imageAsset = null) { if (!\Pimcore\Video::isAvailable()) { \Logger::error("Couldn't create image-thumbnail of video " . $this->getFullPath() . " no video adapter is available"); return "/pimcore/static/img/filetype-not-supported.png"; } $cs = $this->getCustomSetting("image_thumbnail_time"); $im = $this->getCustomSetting("image_thumbnail_asset"); if ($im || $imageAsset) { if ($im) { $imageAsset = Model\Asset::getById($im); } if ($imageAsset instanceof Image) { return $imageAsset->getThumbnail($thumbnailName); } } if (!$timeOffset && $cs) { $timeOffset = $cs; } // fallback if (!$timeOffset) { $timeOffset = ceil($this->getDuration() / 3); } $converter = \Pimcore\Video::getInstance(); $converter->load($this->getFileSystemPath()); $path = PIMCORE_TEMPORARY_DIRECTORY . "/video-image-cache/video_" . $this->getId() . "__thumbnail_" . $timeOffset . ".png"; if (!is_dir(dirname($path))) { File::mkdir(dirname($path)); } if (!is_file($path)) { $lockKey = "video_image_thumbnail_" . $this->getId() . "_" . $timeOffset; Model\Tool\Lock::acquire($lockKey); // after we got the lock, check again if the image exists in the meantime - if not - generate it if (!is_file($path)) { $converter->saveImage($path, $timeOffset); } Model\Tool\Lock::release($lockKey); } $thumbnail = $this->getImageThumbnailConfig($thumbnailName); if ($thumbnail) { $thumbnail->setFilenameSuffix("time-" . $timeOffset); try { $path = Image\Thumbnail\Processor::process($this, $thumbnail, $path); } catch (\Exception $e) { \Logger::error("Couldn't create image-thumbnail of video " . $this->getFullPath()); \Logger::error($e); return "/pimcore/static/img/filetype-not-supported.png"; } } $path = preg_replace("@^" . preg_quote(PIMCORE_DOCUMENT_ROOT, "@") . "@", "", $path); return $path; }
/** * */ public function generate() { $errorImage = PIMCORE_PATH . '/static6/img/filetype-not-supported.png'; $generated = false; if (!$this->asset) { $this->filesystemPath = $errorImage; } elseif (!$this->filesystemPath) { $config = $this->getConfig(); $config->setName("document_" . $config->getName() . "-" . $this->page); try { $path = null; if (!$this->deferred) { $converter = \Pimcore\Document::getInstance(); $converter->load($this->asset->getFileSystemPath()); $path = PIMCORE_TEMPORARY_DIRECTORY . "/document-image-cache/document_" . $this->asset->getId() . "__thumbnail_" . $this->page . ".png"; if (!is_dir(dirname($path))) { \Pimcore\File::mkdir(dirname($path)); } $lockKey = "document-thumbnail-" . $this->asset->getId() . "-" . $this->page; if (!is_file($path) && !Model\Tool\Lock::isLocked($lockKey)) { Model\Tool\Lock::lock($lockKey); $converter->saveImage($path, $this->page); $generated = true; Model\Tool\Lock::release($lockKey); } elseif (Model\Tool\Lock::isLocked($lockKey)) { return "/pimcore/static6/img/please-wait.png"; } } if ($config) { $path = Image\Thumbnail\Processor::process($this->asset, $config, $path, $this->deferred, true, $generated); } $this->filesystemPath = $path; } catch (\Exception $e) { Logger::error("Couldn't create image-thumbnail of document " . $this->asset->getRealFullPath()); Logger::error($e); $this->filesystemPath = $errorImage; } \Pimcore::getEventManager()->trigger("asset.document.image-thumbnail", $this, ["deferred" => $this->deferred, "generated" => $generated]); } }
/** * Get the height of the generated thumbnail image in pixels. * @return string HTTP Mime Type of the generated thumbnail image. */ public function getMimeType() { if (!$this->mimetype) { // get target mime type without actually generating the thumbnail (deferred) $mapping = ["png" => "image/png", "jpg" => "image/jpeg", "jpeg" => "image/jpeg", "pjpeg" => "image/jpeg", "gif" => "image/gif", "tiff" => "image/tiff", "svg" => "image/svg+xml"]; $targetFormat = strtolower($this->getConfig()->getFormat()); $format = $targetFormat; $fileExt = \Pimcore\File::getFileExtension($this->getAsset()->getFilename()); if ($targetFormat == "source" || empty($targetFormat)) { $format = Thumbnail\Processor::getAllowedFormat($fileExt, ["jpeg", "gif", "png"], "png"); } elseif ($targetFormat == "print") { $format = Thumbnail\Processor::getAllowedFormat($fileExt, ["svg", "jpeg", "png", "tiff"], "png"); if (($format == "tiff" || $format == "svg") && \Pimcore\Tool::isFrontentRequestByAdmin()) { // return a webformat in admin -> tiff cannot be displayed in browser $format = "png"; } } if (array_key_exists($format, $mapping)) { $this->mimetype = $mapping[$format]; } else { // unknown $this->mimetype = "application/octet-stream"; } } return $this->mimetype; }