Remove image quality preprocessor

This commit is contained in:
Jure Grabnar 2019-06-10 15:10:07 +02:00
parent a5b88a4b50
commit c2d615ab48
2 changed files with 14 additions and 37 deletions

View file

@ -23,8 +23,7 @@ class Asset < ApplicationRecord
else
{
large: [Constants::LARGE_PIC_FORMAT, :jpg],
medium: [Constants::MEDIUM_PIC_FORMAT, :jpg],
original: { processors: [:image_quality_calculate] }
medium: [Constants::MEDIUM_PIC_FORMAT, :jpg]
}
end
},
@ -34,6 +33,7 @@ class Asset < ApplicationRecord
}
before_post_process :previewable?
before_save :extract_image_quality
# adds image processing in background job
process_in_background :file, processing_image_url: '/images/:style/processing.gif'
@ -200,6 +200,18 @@ class Asset < ApplicationRecord
end
end
def extract_image_quality
return unless ['image/jpeg', 'image/pjpeg'].include? file_content_type
tempfile = file.queued_for_write[:original]
unless tempfile.nil?
quality = Paperclip::Processor.new(tempfile).identify(" -format '%Q' #{tempfile.path}")
self.file_image_quality = quality.to_i
end
rescue StandardError => e
Rails.logger.info "There was an error extracting image quality - #{e}"
end
def previewable?
file.previewable_image? || file.previewable_document?
end

View file

@ -1,35 +0,0 @@
# frozen_string_literal: true
module Paperclip
class ImageQualityCalculate < Processor
def initialize(file, options = {}, attachment = nil)
super
end
def make
if @file && (['image/jpeg', 'image/pjpeg'].include? @file.content_type)
quality = Paperclip::Processor.new(@file).identify(" -format '%Q' #{@file.path}")
@attachment.instance.file_image_quality = quality.to_i
# Asset will be save after all processors finished
end
# We have to create a new temp file otherwise the postprocessing logic will
# delete the original file, leaving no files to postprocess for styles
current_format = File.extname(attachment.instance.file_file_name)
basename = File.basename(@file.path, current_format)
tempfile = Tempfile.new([basename, current_format].compact.join('.'))
begin
tempfile.write(File.read(@file.path))
tempfile.flush
tempfile
rescue StandardError => e
tempfile.close
tempfile.unlink
raise Paperclip::Error, "There was an error writing to tempfile - #{e}"
end
rescue StandardError => e
raise Paperclip::Error, "There was an error processing the image - #{e}"
end
end
end