From 46ecdc3da1a307c6c558c89c8846cd6234545404 Mon Sep 17 00:00:00 2001 From: Ulysse Buonomo Date: Wed, 31 Aug 2022 19:55:57 +0200 Subject: [PATCH] Keep original image format when using MiniMagick unless explicited TODO --- lib/carrierwave/processing/mini_magick.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/carrierwave/processing/mini_magick.rb b/lib/carrierwave/processing/mini_magick.rb index f2a7b89cd..dd88eec35 100644 --- a/lib/carrierwave/processing/mini_magick.rb +++ b/lib/carrierwave/processing/mini_magick.rb @@ -289,14 +289,17 @@ def manipulate! # # [CarrierWave::ProcessingError] if processing failed. def minimagick!(block = nil) - builder = ImageProcessing::MiniMagick.source(current_path) - builder = yield(builder) + builder = ImageProcessing::MiniMagick.source(current_path).convert(content_type) + original_format = builder.options[:format] + builder = yield(builder) + new_format = builder.options[:format] result = builder.call result.close # backwards compatibility (we want to eventually move away from MiniMagick::Image) if block + # TODO: do we want to still support this, or remove it? image = ::MiniMagick::Image.new(result.path, result) image = block.call(image) result = image.instance_variable_get(:@tempfile) @@ -304,7 +307,7 @@ def minimagick!(block = nil) FileUtils.mv result.path, current_path - if File.extname(result.path) != File.extname(current_path) + if original_format != new_format move_to = current_path.chomp(File.extname(current_path)) + File.extname(result.path) file.content_type = Marcel::Magic.by_path(move_to).try(:type) file.move_to(move_to, permissions, directory_permissions)