From 0a6ef6e351d6593ccb0bc0abfdccf8509b9db9d7 Mon Sep 17 00:00:00 2001 From: zwim <36999612+zwim@users.noreply.github.com> Date: Sat, 5 Dec 2020 23:54:06 +0100 Subject: [PATCH] CoverImage plugin: allow saving covers as JPG (#6924) --- plugins/coverimage.koplugin/main.lua | 31 +++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/plugins/coverimage.koplugin/main.lua b/plugins/coverimage.koplugin/main.lua index 099f73f3d..4ab92d801 100644 --- a/plugins/coverimage.koplugin/main.lua +++ b/plugins/coverimage.koplugin/main.lua @@ -44,9 +44,10 @@ local CoverImage = WidgetContainer:new{ } function CoverImage:init() - self.cover_image_path = G_reader_settings:readSetting("cover_image_path") or "cover.png" + self.cover_image_path = G_reader_settings:readSetting("cover_image_path") or "cover.jpg" self.cover_image_format = G_reader_settings:readSetting("cover_image_format") or "auto" self.cover_image_extension = getExtension(self.cover_image_path) + self.cover_image_quality = G_reader_settings:readSetting("cover_image_quality") or 75 self.cover_image_background = G_reader_settings:readSetting("cover_image_background") or "black" self.cover_image_fallback_path = G_reader_settings:readSetting("cover_image_fallback_path") or "cover_fallback.png" self.enabled = G_reader_settings:isTrue("cover_image_enabled") @@ -88,7 +89,12 @@ function CoverImage:createCoverImage(doc_settings) if self.cover_image_background == "none" or scale_factor == 1 then local act_format = self.cover_image_format == "auto" and self.cover_image_extension or self.cover_image_format - cover_image:writeToFile(self.cover_image_path, act_format) + if not cover_image:writeToFile(self.cover_image_path, act_format, self.cover_image_quality) then + UIManager:show(InfoMessage:new{ + text = T(_"Error writing file\n") .. self.cover_image_path, + show_icon = true, + }) + end cover_image:free() return end @@ -113,7 +119,12 @@ function CoverImage:createCoverImage(doc_settings) cover_image:free() local act_format = self.cover_image_format == "auto" and self.cover_image_extension or self.cover_image_format - image:writeToFile(self.cover_image_path, act_format) + if not image:writeToFile(self.cover_image_path, act_format, self.cover_image_quality) then + UIManager:show(InfoMessage:new{ + text = T(_"Error writing file\n") .. self.cover_image_path, + show_icon = true, + }) + end image:free() logger.dbg("CoverImage: image written to " .. self.cover_image_path) @@ -320,6 +331,20 @@ function CoverImage:addToMainMenu(menu_items) end end, }, + { + text = _("JPG file format"), + checked_func = function() + return self.cover_image_format == "jpg" + end, + callback = function() + local old_cover_image_format = self.cover_image_format + self.cover_image_format = "jpg" + G_reader_settings:saveSetting("cover_image_format", self.cover_image_format) + if self.enabled and old_cover_image_format ~= self.cover_image_format then + self:createCoverImage(self.ui.doc_settings) + end + end, + }, { text = _("PNG file format"), checked_func = function()