Project

General

Profile

Feature #22995 ยป 0001-Replace-ImageMagick-shellouts-with-fastimage_resize.patch

Gregor Schmidt, 2016-06-06 12:57

View differences:

Gemfile
gem "actionpack-action_caching"
gem "actionpack-xml_parser"
gem "roadie-rails"
gem "mimemagic"
# Optional gems to create attachment thumbnails
platforms :mri do
group :thumbnails do
gem "local-fastimage_resize", "~> 3.1", require: "fastimage/resize"
end
end
# Request at least nokogiri 1.6.7.2 because of security advisories
gem "nokogiri", ">= 1.6.7.2"
# Request at least rails-html-sanitizer 1.0.3 because of security advisories
# Request at least rails-html-sanitizer 1.0.3 because of security advisories
gem "rails-html-sanitizer", ">= 1.0.3"
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
app/models/attachment.rb
end
def image?
!!(self.filename =~ /\.(bmp|gif|jpg|jpe|jpeg|png)$/i)
!!(self.filename =~ /\.(gif|jpg|jpe|jpeg|png)$/i)
end
def thumbnailable?
lib/redmine/thumbnail.rb
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'fileutils'
require 'mimemagic'
begin
require 'fastimage/resize' unless defined? FastImage
rescue LoadError
# :thumbnails group not available
end
module Redmine
module Thumbnail
extend Redmine::Utils::Shell
CONVERT_BIN = (Redmine::Configuration['imagemagick_convert_command'] || 'convert').freeze
# Generates a thumbnail for the source image to target
def self.generate(source, target, size)
# Nothing we can do without the libraries
return nil unless convert_available?
unless File.exists?(target)
# Make sure we only invoke Imagemagick if this is actually an image
unless File.open(source) {|f| MimeMagic.by_magic(f).try(:image?)}
return nil
end
directory = File.dirname(target)
unless File.exists?(directory)
FileUtils.mkdir_p directory
end
size_option = "#{size}x#{size}>"
cmd = "#{shell_quote CONVERT_BIN} #{shell_quote source} -thumbnail #{shell_quote size_option} #{shell_quote target}"
unless system(cmd)
logger.error("Creating thumbnail failed (#{$?}):\nCommand: #{cmd}")
return nil
end
# Nothing to do, thumb was already created
return target if File.exists?(target)
fastimage = FastImage.new(source)
# Cannot read dimensions for source - it's probably not an image
return nil if fastimage.size.nil?
directory = File.dirname(target)
FileUtils.mkdir_p(directory) unless File.exists?(directory)
if fastimage.size.max <= size
FileUtils.cp(source, target)
elsif fastimage.size.first > fastimage.size.last
fastimage.resize(size, 0, :outfile => target, :jpeg_quality => 90)
else
fastimage.resize(0, size, :outfile => target, :jpeg_quality => 90)
end
target
rescue FastImage::FastImageException
logger.error("Creating thumbnail failed - #{$!.class.name}: #{$!.message}")
nil
end
def self.convert_available?
return @convert_available if defined?(@convert_available)
@convert_available = system("#{shell_quote CONVERT_BIN} -version") rescue false
logger.warn("Imagemagick's convert binary (#{CONVERT_BIN}) not available") unless @convert_available
@convert_available
!!defined?(FastImage)
end
def self.logger
    (1-1/1)