Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
17
Help us understand the problem. What is going on with this article?
@nifuramu

Paperclipで写真も動画も同じカラムを使いアップロードとサムネイルの作成をする

More than 5 years have passed since last update.

PaperclipPaperclip FFMPEGを使って写真も動画も同じカラムでアップロードとサムネイルの作成を行いたい場合の書き方です。

has_attached_filestylesprocessors を以下のような感じで書く。

has_attached_file :attachment,
  styles: lambda { |file|
    if file.instance.is_video?
      {
        medium:    { geometry: '240x240#', format: 'jpg', time: 0, auto_rotate: true },
        thumbnail: { geometry: '64x64#', format: 'jpg', time: 0, auto_rotate: true },
      }
    else
      { medium: '240x240#', thumbnail: '64x64#' }
    end
  },
  storage: :filesystem,
  path: ":attachment/:id/:style/:filename",
  processors: lambda { |file| file.is_video? ? [ :ffmpeg ] : [ :thumbnail ] }

def is_video?
  attachment.instance.attachment_content_type =~ %r(video) ? true : false
end
17
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
nifuramu
ひかりのかなたに きえさった

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
17
Help us understand the problem. What is going on with this article?