From be099bb637c3b845a4d5037eeee15d2244120354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 27 Jul 2016 19:55:13 +0200 Subject: [PATCH] only convert pasted images to HQ jpg when it's at least 5% smaller --- app/controllers/uploads_controller.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index 5a718bd09d9..70119a21019 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -72,11 +72,16 @@ class UploadsController < ApplicationController # convert pasted images to HQ jpegs if filename == "blob.png" && SiteSetting.convert_pasted_images_to_hq_jpg - filename = "blob.jpg" - jpeg_path = "#{File.dirname(tempfile.path)}/#{filename}" - content_type = "image/jpeg" + jpeg_path = "#{File.dirname(tempfile.path)}/blob.jpg" `convert #{tempfile.path} -quality 95 #{jpeg_path}` - tempfile = File.open(jpeg_path) + # only change the format of the image when JPG is at least 5% smaller + if File.size(jpeg_path) < File.size(tempfile.path) * 0.95 + filename = "blob.jpg" + content_type = "image/jpeg" + tempfile = File.open(jpeg_path) + else + File.delete(jpeg_path) rescue nil + end end # allow users to upload large images that will be automatically reduced to allowed size