Skip to content

Instantly share code, notes, and snippets.

@syou007
Last active September 29, 2017 08:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save syou007/2ce31e112f46901217675e0eeed1f57b to your computer and use it in GitHub Desktop.
Save syou007/2ce31e112f46901217675e0eeed1f57b to your computer and use it in GitHub Desktop.
Rails、carrierwaveの使い方。インストール方法 ref: http://qiita.com/syou007/items/98842179e83c03e5f882
# S3を使用しているかを判定するためのメソッド。(ローカル環境ではS3を使わないため)
def use_s3?
ENV['S3_ACCESS_KEY'] && ENV['S3_SECRET_KEY'] && ENV['S3_REGION'] && ENV['S3_BUCKET']
end
## CarrierWaveの設定
CarrierWave.configure do |config|
# S3の設定
if use_s3?
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => ENV['S3_ACCESS_KEY'],
:aws_secret_access_key => ENV['S3_SECRET_KEY'],
:region => ENV['S3_REGION'],
# :host => '必要なら設定する'
# :endpoint => '必要なら設定する'
}
# S3のバケットを指定。
config.fog_directory = ENV['S3_BUCKET']
# 一般公開させて無いS3の場合は以下の設定を行う。
config.fog_public = false
# 一般公開されていない場合は以下の設定をする事で60秒間有効なURLを発行してくれる。
config.fog_authenticated_url_expiration = 60
CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/
end
# public配下にキャッシュができると参照されてしまうので、予め変えておく。
config.cache_dir = "#{Rails.root}/tmp/uploads"
end
brew install imagemagick
# 画像保存
gem 'carrierwave'
# For Carrierwave(S3用)
gem 'fog'
# 画像加工用
gem 'rmagick'
= simple_form_for(@image) do |f|
-#アップロードする画像(確認画面を作ることもできるけど、本記事では割愛)
= f.input :image, as: :file
-#アップロードした画像を表示
= image_tag f.image
-#カラム名にremoveをつけてデータを送ると画像を消せる。
= f.input :remove_image, as: :boolean
= f.submit "画像アップ"
def post_image
Image.new(params[:image]).save
end
open(path, :allow_redirections => :safe) do |file|
# 一時的にテンポラリーファイルを作る必要があった。
temp_img_file = Tempfile.new('ファイル名')
temp_img_file.binmode
temp_img_file << file.read
temp_img_file.rewind
# UploadedFileもしくはCarrierWave::Uploader::Base形式にする必要がある。
uploaded_file = ActionDispatch::Http::UploadedFile.new(
filename: 'ファイル名',
type: file.content_type,
tempfile: temp_img_file)
image.image = uploaded_file
image.save
end
process :fix_rotate
# 画像の向きを調整する。
def fix_rotate
manipulate! do |img|
img = img.auto_orient
img = yield(img) if block_given?
img
end
end
class Images < ActiveRecord::Migration[5.0]
def change
create_table :images do |t|
# 管理したい画像
t.string :image
# etc...
t.timestamps
end
end
end
# 画像をアップロードする際のルール(別ファイルにしておけば汎用的に使える。)
class ImageUploader < CarrierWave::Uploader::Base
# S3を使用するか?
if use_s3?
storage :fog
else
storage :file
end
# S3やローカルの保存先。(以下の書式をそのまま書いておけば大抵問題無い)
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# 以下のようなことができる。
# リサイズしたり画像形式を変更するのに必要
include CarrierWave::RMagick
# 画像を100x100にリサイズする。
process :resize_to_fill => [100, 100]
# 保存形式をJPGにする
process :convert => 'jpg'
# サムネイルを生成する設定
# version :thumb do
# process :resize_to_fill => [40, 40, gravity = ::Magick::CenterGravity]
# end
# jpg,jpeg,gif,pngしか受け付けない
def extension_white_list
%w(jpg jpeg png)
end
# 拡張子が同じでないとPNGをJPGとかにコンバートできないので、ファイル名を変更
def filename
super.chomp(File.extname(super)) + '.jpg' if original_filename.present?
end
# 別のバケットにアクセスする場合は以下のようにする。
# https://blog.hello-world.jp.net/ruby/1449/
# def fog_directory
# config = YAML.load_file("#{Rails.root}/config/carrierwave.yml")[Rails.env]
# config['fog_another_directory']
# end
end
# 画像用モデル
class Image < ApplicationRecord
# ファイルアップロード処理
mount_uploader :image, ImageUploader
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment