/** * Retrieves the preview from the cache and resizes it if necessary * * @param int $fileId fileId of the original image * @param string $cached the path to the cached preview */ private function getCachedPreview($fileId, $cached) { $stream = $this->userView->fopen($cached, 'r'); $this->preview = null; if ($stream) { $image = new \OC_Image(); $image->loadFromFileHandle($stream); $this->preview = $image->valid() ? $image : null; if (!is_null($this->preview)) { // Size of the preview we calculated $maxX = $this->previewWidth; $maxY = $this->previewHeight; // Size of the preview we retrieved from the cache $previewX = (int) $this->preview->width(); $previewY = (int) $this->preview->height(); // We don't have an exact match if ($previewX !== $maxX || $previewY !== $maxY) { $this->resizeAndStore($fileId); } } fclose($stream); } }
/** * return a preview of a file * @return \OC_Image */ public function getPreview() { if (!is_null($this->preview) && $this->preview->valid()) { return $this->preview; } $this->preview = null; $file = $this->getFile(); $maxX = $this->getMaxX(); $maxY = $this->getMaxY(); $scalingUp = $this->getScalingUp(); $fileInfo = $this->getFileInfo($file); if ($fileInfo === null || $fileInfo === false) { return new \OC_Image(); } $fileId = $fileInfo->getId(); $cached = $this->isCached($fileId); if ($cached) { $stream = $this->userView->fopen($cached, 'r'); $this->preview = null; if ($stream) { $image = new \OC_Image(); $image->loadFromFileHandle($stream); $this->preview = $image->valid() ? $image : null; $this->resizeAndCrop(); fclose($stream); } } if (is_null($this->preview)) { $preview = null; foreach (self::$providers as $supportedMimeType => $provider) { if (!preg_match($supportedMimeType, $this->mimeType)) { continue; } \OC_Log::write('core', 'Generating preview for "' . $file . '" with "' . get_class($provider) . '"', \OC_Log::DEBUG); /** @var $provider Provider */ $preview = $provider->getThumbnail($file, $maxX, $maxY, $scalingUp, $this->fileView); if (!$preview instanceof \OC_Image) { continue; } $this->preview = $preview; $this->resizeAndCrop(); $previewPath = $this->getPreviewPath($fileId); $cachePath = $this->buildCachePath($fileId); if ($this->userView->is_dir($this->getThumbnailsFolder() . '/') === false) { $this->userView->mkdir($this->getThumbnailsFolder() . '/'); } if ($this->userView->is_dir($previewPath) === false) { $this->userView->mkdir($previewPath); } $this->userView->file_put_contents($cachePath, $preview->data()); break; } } if (is_null($this->preview)) { $this->preview = new \OC_Image(); } return $this->preview; }
/** * Retrieves the preview from the cache and resizes it if necessary * * @param int $fileId fileId of the original image * @param string $cached the path to the cached preview */ private function getCachedPreview($fileId, $cached) { $stream = $this->userView->fopen($cached, 'r'); $this->preview = null; if ($stream) { $image = new \OC_Image(); $image->loadFromFileHandle($stream); $this->preview = $image->valid() ? $image : null; $maxX = (int) $this->getMaxX(); $maxY = (int) $this->getMaxY(); $previewX = (int) $this->preview->width(); $previewY = (int) $this->preview->height(); if ($previewX !== $maxX && $previewY !== $maxY) { $this->resizeAndStore($fileId); } fclose($stream); } }