我使用回形针来处理我项目中上传的所有文件。我的问题是,有些文件是图像和一些图像使用CMYK作为颜色空间,这是一个问题,在一些浏览器,但特别是在一些Android版本(例如Android 4.2不能处理jpg与CMYK颜色空间)
我通过在回形针中使用convert_options并删除CMYK并添加RGB来解决这个问题。我的代码是这样的:
has_attached_file :media,
:storage => :azure,
:styles => lambda { |a|
if a.instance.is_image?
{
:thumb => {geometry: "75x75#", convert_options: '-strip -colorspace RGB' },
:preview => {geometry: "300x300#",convert_options: '-strip -colorspace RGB' },
:original => {convert_options: '-strip -colorspace RGB'}
}
else ....
这很好,但是当运行convert_options时,会有颜色的改变。所以,我想运行convert_options只有当我检测到当前的色彩空间是CMYK,不运行它,如果它已经是RGB,它是不做任何已经RGB图像,只是改变原来的颜色。我在找这样的东西:
has_attached_file :media,
:storage => :azure,
:styles => lambda { |a|
if a.instance.is_image?
if a.instance.colorspace_is?("CMYK")
{
:thumb => {geometry: "75x75#", convert_options: '-strip -colorspace RGB' },
:preview => {geometry: "300x300#",convert_options: '-strip -colorspace RGB' },
:original => {convert_options: '-strip -colorspace RGB'}
else
{
:thumb => "75x75#",
:preview => "300x300#"}
}
end
else ....
这可能吗?
我找到解决方案了!
在lib/paperclip中我添加了colorspace。
内的代码module Paperclip
class Colorspace < Thumbnail
def transformation_command
scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
trans = []
trans << "-coalesce" if animated?
trans << "-auto-orient" if auto_orient
trans << "-resize" << %["#{scale}"] unless scale.nil? || scale.empty?
trans << "-crop" << %["#{crop}"] << "+repage" if crop
trans << '-layers "optimize"' if animated?
trans << "-strip -colorspace RGB" unless is_rgb?
trans
end
protected
def is_rgb?
colorspace = identify("-verbose %m :file | grep 'Colorspace'", :file => "#{@file.path}[0]").to_s.downcase.strip
colorspace && colorspace.include?("rgb")
rescue Cocaine::ExitStatusError => e
return false
rescue Cocaine::CommandNotFoundError => e
raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `identify` command. Please install ImageMagick.")
end
end
end
在我的模型中,我将处理器设置为colorspace而不是缩略图。伟大作品