]> source.dussan.org Git - nextcloud-server.git/commitdiff
Use 90% JPEG quality for thumbnails and previews by default
authorJoas Schilling <coding@schilljs.com>
Wed, 22 Feb 2017 11:11:42 +0000 (12:11 +0100)
committerJoas Schilling <coding@schilljs.com>
Wed, 22 Feb 2017 13:53:15 +0000 (14:53 +0100)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/legacy/image.php
tests/lib/ImageTest.php

index 47f2a977e9cc153e4b4b52fa464c890c92095f7a..05367bbfde44b80e574b9f7f41c070f937152686 100644 (file)
@@ -54,6 +54,8 @@ class OC_Image implements \OCP\IImage {
        private $fileInfo;
        /** @var \OCP\ILogger */
        private $logger;
+       /** @var \OCP\IConfig */
+       private $config;
        /** @var array */
        private $exif;
 
@@ -79,12 +81,17 @@ class OC_Image implements \OCP\IImage {
         * @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by
         * an imagecreate* function.
         * @param \OCP\ILogger $logger
+        * @param \OCP\IConfig $config
         */
-       public function __construct($imageRef = null, $logger = null) {
+       public function __construct($imageRef = null, $logger = null, \OCP\IConfig $config = null) {
                $this->logger = $logger;
                if (is_null($logger)) {
                        $this->logger = \OC::$server->getLogger();
                }
+               $this->config = $config;
+               if ($config === null) {
+                       $this->config = \OC::$server->getConfig();
+               }
 
                if (\OC_Util::fileInfoLoaded()) {
                        $this->fileInfo = new finfo(FILEINFO_MIME_TYPE);
@@ -267,7 +274,7 @@ class OC_Image implements \OCP\IImage {
                                $retVal = imagegif($this->resource, $filePath);
                                break;
                        case IMAGETYPE_JPEG:
-                               $retVal = imagejpeg($this->resource, $filePath);
+                               $retVal = imagejpeg($this->resource, $filePath, $this->getJpegQuality());
                                break;
                        case IMAGETYPE_PNG:
                                $retVal = imagepng($this->resource, $filePath);
@@ -319,7 +326,12 @@ class OC_Image implements \OCP\IImage {
                                $res = imagepng($this->resource);
                                break;
                        case "image/jpeg":
-                               $res = imagejpeg($this->resource);
+                               $quality = $this->getJpegQuality();
+                               if ($quality !== null) {
+                                       $res = imagejpeg($this->resource, null, $quality);
+                               } else {
+                                       $res = imagejpeg($this->resource);
+                               }
                                break;
                        case "image/gif":
                                $res = imagegif($this->resource);
@@ -342,6 +354,17 @@ class OC_Image implements \OCP\IImage {
                return base64_encode($this->data());
        }
 
+       /**
+        * @return int|null
+        */
+       protected function getJpegQuality() {
+               $quality = $this->config->getAppValue('preview', 'jpeg_quality', 90);
+               if ($quality !== null) {
+                       $quality = min(100, max(10, (int) $quality));
+               }
+               return $quality;
+       }
+
        /**
         * (I'm open for suggestions on better method name ;)
         * Get the orientation based on EXIF data.
index 9176b8cf6c9299cd5ae334795a4cef0130800773..b7255ccdbd62950da8337d7c778294abe603df70 100644 (file)
@@ -9,6 +9,7 @@
 namespace Test;
 
 use OC;
+use OCP\IConfig;
 
 class ImageTest extends \Test\TestCase {
        public static function tearDownAfterClass() {
@@ -133,7 +134,12 @@ class ImageTest extends \Test\TestCase {
                $expected = ob_get_clean();
                $this->assertEquals($expected, $img->data());
 
-               $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.jpg');
+               $config = $this->createMock(IConfig::class);
+               $config->expects($this->once())
+                       ->method('getAppValue')
+                       ->with('preview', 'jpeg_quality', 90)
+                       ->willReturn(null);
+               $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.jpg', null, $config);
                $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
                ob_start();
                imagejpeg($raw);