diff options
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/private/preview.php | 60 | ||||
-rw-r--r-- | lib/private/preview/image.php | 3 | ||||
-rw-r--r-- | lib/private/preview/mp3.php | 2 | ||||
-rw-r--r-- | lib/private/preview/pdf.php | 1 | ||||
-rw-r--r-- | lib/private/preview/provider.php | 1 | ||||
-rw-r--r-- | lib/private/preview/svg.php | 3 |
6 files changed, 48 insertions, 22 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; + } } diff --git a/lib/private/preview/image.php b/lib/private/preview/image.php index 84343df2608..59aaa27ef34 100644 --- a/lib/private/preview/image.php +++ b/lib/private/preview/image.php @@ -31,6 +31,7 @@ class Image extends Provider { return $image->valid() ? $image : false; } + } -\OC\Preview::registerProvider('OC\Preview\Image');
\ No newline at end of file +\OC\Preview::registerProvider('OC\Preview\Image'); diff --git a/lib/private/preview/mp3.php b/lib/private/preview/mp3.php index 3fc0ab0490c..21f160fd50f 100644 --- a/lib/private/preview/mp3.php +++ b/lib/private/preview/mp3.php @@ -47,4 +47,4 @@ class MP3 extends Provider { } -\OC\Preview::registerProvider('OC\Preview\MP3');
\ No newline at end of file +\OC\Preview::registerProvider('OC\Preview\MP3'); diff --git a/lib/private/preview/pdf.php b/lib/private/preview/pdf.php index 064a5a3b3d1..4b88b1a31e2 100644 --- a/lib/private/preview/pdf.php +++ b/lib/private/preview/pdf.php @@ -40,6 +40,7 @@ if (extension_loaded('imagick')) { //check if image object is valid return $image->valid() ? $image : false; } + } \OC\Preview::registerProvider('OC\Preview\PDF'); diff --git a/lib/private/preview/provider.php b/lib/private/preview/provider.php index 88337d64e4b..f769823f6e6 100644 --- a/lib/private/preview/provider.php +++ b/lib/private/preview/provider.php @@ -22,4 +22,5 @@ abstract class Provider { * OC_Image object of the preview */ abstract public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview); + } diff --git a/lib/private/preview/svg.php b/lib/private/preview/svg.php index 505122fddbf..82ef3cdebf6 100644 --- a/lib/private/preview/svg.php +++ b/lib/private/preview/svg.php @@ -45,8 +45,9 @@ if (extension_loaded('imagick')) { //check if image object is valid return $image->valid() ? $image : false; } + } \OC\Preview::registerProvider('OC\Preview\SVG'); } -}
\ No newline at end of file +} |