From c4eb14f5cd1cf499b7d5eaf944b6c200c0d6cd10 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Fri, 29 Mar 2019 17:22:23 +0100 Subject: [PATCH] Preserve file type if image contains transparent pixels --- wcfsetup/install/files/js/WCF.Attachment.js | 7 +++-- .../WoltLabSuite/Core/{ => Image}/ExifUtil.js | 2 +- .../js/WoltLabSuite/Core/Image/ImageUtil.js | 29 +++++++++++++++++++ .../{ImageResizer.js => Image/Resizer.js} | 7 +++-- wcfsetup/install/lang/de.xml | 2 +- wcfsetup/install/lang/en.xml | 2 +- 6 files changed, 40 insertions(+), 9 deletions(-) rename wcfsetup/install/files/js/WoltLabSuite/Core/{ => Image}/ExifUtil.js (99%) create mode 100644 wcfsetup/install/files/js/WoltLabSuite/Core/Image/ImageUtil.js rename wcfsetup/install/files/js/WoltLabSuite/Core/{ImageResizer.js => Image/Resizer.js} (97%) diff --git a/wcfsetup/install/files/js/WCF.Attachment.js b/wcfsetup/install/files/js/WCF.Attachment.js index 8abb95b879..34cf0c4720 100644 --- a/wcfsetup/install/files/js/WCF.Attachment.js +++ b/wcfsetup/install/files/js/WCF.Attachment.js @@ -296,9 +296,10 @@ if (COMPILER_TARGET_DEFAULT) { require([ 'WoltLabSuite/Core/FileUtil', - 'WoltLabSuite/Core/ImageResizer', + 'WoltLabSuite/Core/Image/ImageUtil', + 'WoltLabSuite/Core/Image/Resizer', 'WoltLabSuite/Core/Ajax/Status' - ], (function (FileUtil, ImageResizer, AjaxStatus) { + ], (function (FileUtil, ImageUtil, ImageResizer, AjaxStatus) { AjaxStatus.show(); var files = []; @@ -360,7 +361,7 @@ if (COMPILER_TARGET_DEFAULT) { var fileType = this._options.autoScale.fileType; - if (this._options.autoScale.fileType === 'keep') { + if (this._options.autoScale.fileType === 'keep' || ImageUtil.containsTransparentPixels(resizedImage)) { fileType = file.type; } diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/ExifUtil.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Image/ExifUtil.js similarity index 99% rename from wcfsetup/install/files/js/WoltLabSuite/Core/ExifUtil.js rename to wcfsetup/install/files/js/WoltLabSuite/Core/Image/ExifUtil.js index 8db5931941..618446cc7d 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/ExifUtil.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Image/ExifUtil.js @@ -4,7 +4,7 @@ * @author Maximilian Mader * @copyright 2001-2018 WoltLab GmbH * @license GNU Lesser General Public License - * @module WoltLabSuite/Core/ExifUtil + * @module WoltLabSuite/Core/Image/ExifUtil */ define([], function() { "use strict"; diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Image/ImageUtil.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Image/ImageUtil.js new file mode 100644 index 0000000000..cd558c4be5 --- /dev/null +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Image/ImageUtil.js @@ -0,0 +1,29 @@ +/** + * Provides helper functions for Image metadata handling. + * + * @author Tim Duesterhus + * @copyright 2001-2019 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLabSuite/Core/Image/ImageUtil + */ +define([], function() { + "use strict"; + + return { + /** + * Returns whether the given canvas contains transparent pixels. + * + * @param image {Canvas} Canvas to check + * @returns {bool} + */ + containsTransparentPixels: function (canvas) { + var imageData = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height); + + for (var i = 3, max = imageData.data.length; i < max; i += 4) { + if (imageData.data[i] !== 255) return true; + } + + return false; + } + }; +}); diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/ImageResizer.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Image/Resizer.js similarity index 97% rename from wcfsetup/install/files/js/WoltLabSuite/Core/ImageResizer.js rename to wcfsetup/install/files/js/WoltLabSuite/Core/Image/Resizer.js index 3765407e3f..f427f6159e 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/ImageResizer.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Image/Resizer.js @@ -4,11 +4,11 @@ * @author Maximilian Mader * @copyright 2001-2018 WoltLab GmbH * @license GNU Lesser General Public License - * @module WoltLabSuite/Core/ImageResizer + * @module WoltLabSuite/Core/Image/Resizer */ define([ 'WoltLabSuite/Core/FileUtil', - 'WoltLabSuite/Core/ExifUtil', + 'WoltLabSuite/Core/Image/ExifUtil', 'Pica' ], function(FileUtil, ExifUtil, Pica) { "use strict"; @@ -194,7 +194,8 @@ define([ var options = { quality: resizeQuality, - cancelToken: cancelPromise + cancelToken: cancelPromise, + alpha: true }; return pica.resize(image, canvas, options); diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index a1421fcdc0..30d47dd5e7 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -1392,7 +1392,7 @@ Das Fehlerprotokoll enthält {$data[count]} neue Einträge. Die ersten drei, in - + diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 0ba9e5857a..3f3eb676e6 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -1369,7 +1369,7 @@ This protocol file contains {$data[count]} new entries. The first three error me - + -- 2.20.1