Skip to content

Instantly share code, notes, and snippets.

@frankie-loves-jesus
Created December 4, 2014 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankie-loves-jesus/8a54e31d06c36b0e2499 to your computer and use it in GitHub Desktop.
Save frankie-loves-jesus/8a54e31d06c36b0e2499 to your computer and use it in GitHub Desktop.
class Photo < ActiveRecord::Base
ATTACHMENT_STYLES = lambda do |attachment|
# http://stackoverflow.com/a/23513608/69926
# https://github.com/joehilton/hpvideos/blob/master/app/models/attachment.rb
# if is_animated_gif?(attachment)
if attachment.instance.is_animated_gif?
{medium: {format: "flv"}, thumbnail: {format: "png"}}
else
{medium: "x300>", thumbnail: "50x50#"}
end
end
# ATTACHMENT_PROCESSORS = lambda { |attachment| is_animated_gif?(attachment) ? [:ffmpeg] : [:thumbnail] }
ATTACHMENT_PROCESSORS = lambda { |attachment| attachment.instance.is_animated_gif? ? [:ffmpeg] : [:thumbnail] }
belongs_to :post, class_name: "Forem::Post"
has_attached_file :attachment, styles: ATTACHMENT_STYLES, processors: ATTACHMENT_PROCESSORS
validates_attachment :attachment, presence: true, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
def self.is_animated_gif?(attachment)
# file = Magick::ImageList.new(attachment.instance_read(:file_name))
# Appears it's not yet saved, so try something different
# file = Magick::ImageList.new(attachment.queued_for_write[:original].path)
# true if attachment.instance_read(:content_type) =~ /gif/ && file.scene > 0
file = Magick::ImageList.new(attachment.instance.attachment_file_name)
true if attachment.instance.attachment_content_type =~ /gif/ && file.scene > 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment