diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2014-04-29 17:07:10 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2014-05-02 17:02:57 +0200 |
commit | 20893cc3b335049a144a53be24eaacb2e33a1480 (patch) | |
tree | c0795a4246bf6c38ec8b8ad9461e25b17ec9af74 /lib/private/preview.php | |
parent | 49d9631eee1616b9ae2846886c3d428236c5b81a (diff) | |
download | nextcloud-server-20893cc3b335049a144a53be24eaacb2e33a1480.tar.gz nextcloud-server-20893cc3b335049a144a53be24eaacb2e33a1480.zip |
Images on public sharing get downscaled to increase use experience - this will speed up loading time
- adding keep aspect to core/ajax/preview.php
- remove duplicate method Preview::show()
- no more hard coded mimetype of preview
- remove .png from the preview urls
- keep old route preview.png for backwards compatibility
- aspect preserving previews are now cached
Diffstat (limited to 'lib/private/preview.php')
-rwxr-xr-x | lib/private/preview.php | 60 |
1 files changed, 41 insertions, 19 deletions
diff --git a/lib/private/preview.php b/lib/private/preview.php index cdf22240382..361073a0916 100755 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -43,6 +43,7 @@ class Preview { private $maxY; private $scalingUp; private $mimeType; + private $keepAspect = false; //filemapper used for deleting previews // index is path, value is fileinfo @@ -267,6 +268,11 @@ class Preview { return $this; } + public function setKeepAspect($keepAspect) { + $this->keepAspect = $keepAspect; + return $this; + } + /** * @brief check if all parameters are valid * @return bool @@ -297,7 +303,7 @@ class Preview { if($fileInfo !== null && $fileInfo !== false) { $fileId = $fileInfo->getId(); - $previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/' . $this->getMaxX() . '-' . $this->getMaxY() . '.png'; + $previewPath = $this->buildCachePath($fileId); return $this->userView->unlink($previewPath); } return false; @@ -330,15 +336,12 @@ class Preview { if (is_null($fileId)) { return false; } - - $maxX = $this->getMaxX(); - $maxY = $this->getMaxY(); - $previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/'; + $preview = $this->buildCachePath($fileId); //does a preview with the wanted height and width already exist? - if ($this->userView->file_exists($previewPath . $maxX . '-' . $maxY . '.png')) { - return $previewPath . $maxX . '-' . $maxY . '.png'; + if ($this->userView->file_exists($preview)) { + return $preview; } return $this->isCachedBigger($fileId); @@ -355,6 +358,11 @@ class Preview { return false; } + // in order to not loose quality we better generate aspect preserving previews from the original file + if ($this->keepAspect) { + return false; + } + $maxX = $this->getMaxX(); //array for usable cached thumbnails @@ -466,12 +474,12 @@ class Preview { $fileId = $fileInfo->getId(); $cached = $this->isCached($fileId); - if ($cached) { $stream = $this->userView->fopen($cached, 'r'); $image = new \OC_Image(); $image->loadFromFileHandle($stream); $this->preview = $image->valid() ? $image : null; + $this->resizeAndCrop(); fclose($stream); } @@ -497,7 +505,7 @@ class Preview { $this->resizeAndCrop(); $previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/'; - $cachePath = $previewPath . $maxX . '-' . $maxY . '.png'; + $cachePath = $this->buildCachePath($fileId); if ($this->userView->is_dir($this->getThumbnailsFolder() . '/') === false) { $this->userView->mkdir($this->getThumbnailsFolder() . '/'); @@ -524,20 +532,12 @@ class Preview { * @brief show preview * @return void */ - public function showPreview() { + public function showPreview($mimeType = null) { \OCP\Response::enableCaching(3600 * 24); // 24 hours if (is_null($this->preview)) { $this->getPreview(); } - $this->preview->show('image/png'); - } - - /** - * @brief show preview - * @return void - */ - public function show() { - $this->showPreview(); + $this->preview->show($mimeType); } /** @@ -561,6 +561,11 @@ class Preview { $realX = (int)$image->width(); $realY = (int)$image->height(); + // compute $maxY using the aspect of the generated preview + if ($this->keepAspect) { + $y = $x / ($realX / $realY); + } + if ($x === $realX && $y === $realY) { $this->preview = $image; return; @@ -743,4 +748,21 @@ class Preview { } return false; } + + /** + * @param $fileId + * @return string + */ + private function buildCachePath($fileId) { + $maxX = $this->getMaxX(); + $maxY = $this->getMaxY(); + + $previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/'; + $preview = $previewPath . $maxX . '-' . $maxY . '.png'; + if ($this->keepAspect) { + $preview = $previewPath . $maxX . '-with-aspect.png'; + return $preview; + } + return $preview; + } } |