如果给定的 .gif 图像不是动画 gif,如何将 .gif 图像转换为 .jpeg?
How can I convert a .gif image to .jpeg if the given .gif image is not a animated gif?
我在 Rails 和 paper-clip
Gem 上使用 Ruby,我想将 .gif
图像转换为 .jpeg
如果 .gif
图片不是动画 gif
.
这是我的代码:
has_attached_file :image, styles: Proc.new { |file| file.instance.check_image_gif? ? {
:'960' => ["960>x960", :gif],
:'640' => ["640>x640", :gif],
:'320' => ["320>x320", :gif]
}:{
:'960' => ["960>x960", :jpg],
:'640' => ["640>x640", :jpg],
:'320' => ["320>x320", :jpg]
}
}
def check_image_gif?
# I want to check animation gif here.
image.instance.image_content_type =~ %r(gif) ? true : false
end
我自己解决了
def check_image_gif?
begin
file = image.instance.image.queued_for_write[:original]
img = Magick::Image.read(file.path)
animation = (img.size>1)
unless animation
image.instance.image_content_type = "image/jpeg"
end
rescue => e
end
image.instance.image_content_type =~ %r(gif) ? true : false
end
我添加了rmagick
Gem。
我在 Rails 和 paper-clip
Gem 上使用 Ruby,我想将 .gif
图像转换为 .jpeg
如果 .gif
图片不是动画 gif
.
这是我的代码:
has_attached_file :image, styles: Proc.new { |file| file.instance.check_image_gif? ? {
:'960' => ["960>x960", :gif],
:'640' => ["640>x640", :gif],
:'320' => ["320>x320", :gif]
}:{
:'960' => ["960>x960", :jpg],
:'640' => ["640>x640", :jpg],
:'320' => ["320>x320", :jpg]
}
}
def check_image_gif?
# I want to check animation gif here.
image.instance.image_content_type =~ %r(gif) ? true : false
end
我自己解决了
def check_image_gif?
begin
file = image.instance.image.queued_for_write[:original]
img = Magick::Image.read(file.path)
animation = (img.size>1)
unless animation
image.instance.image_content_type = "image/jpeg"
end
rescue => e
end
image.instance.image_content_type =~ %r(gif) ? true : false
end
我添加了rmagick
Gem。