Giter Site home page Giter Site logo

img_uploader's Introduction

導入

  1. ImageMagickのインストール
$ sudo apt-get update
$ sudo apt-get install imagemagick

#Homebrewでインストールする場合
$ brew install imagemagick
  1. gemの追加し、インストール
gem 'carrierwave' # 画像をアップロードするためのgem
gem 'mini_magick' #アップロードした画像ファイルのサイズを変更するためのgem
  1. 対象のモデルにimageカラムをtextデータ型で持たせる
  2. アップローダーを作成
$ rails g uploader Image

[app/uploaders/image_uploader.rb]が作成される

  1. 対象のモデルに以下を追記し、imageカラムと、ImageUploaderを紐付け
mount_uploader :image, ImageUploader
  1. ストロングパラメータにカラムを追加
params.require(:model).permit(:image, :image_cache)
  1. フォーム例
<div class="field">
  <%= form.label :image %>
  <%= form.file_field :image %>
</div>
  1. 画像表示
<%= image_tag @feed.image.url %>

画像のリサイズ

https://qiita.com/nekotanku/items/5da43600f35eada64eac [app/uploaders/image_uploader.rb]

class ImageUploader < CarrierWave::Uploader::Base
# minimagicが使用できるよう以下のコメントアウトを外す
  include CarrierWave::MiniMagick 

# 縦横比を維持して、全てリサイズ
  process resize_to_limit: [1200, 900]
# 縦横比を維持して、一方のみサイズ制限
  process :resize_to_limit => [500, nil]

# 余白の塗りつぶし
  process resize_to_limit: [300, 200, "#ffffff", "Center"]

# 切り抜き(全て同じサイズにリサイズされる)
  process resize_to_fill: [100, 100, "Center"]

# 指定したもののみ、縦横比を維持してリサイズ
  version :thumb do
    process resize_to_limit: [480, 360]
  end

cssで画像を円形にトリミング

width:50px;
height:50px;
object-fit:cover;
border-radius:50%;

画像をseedデータで投入する

参考:https://sweets-engineer.com/carrierwaveseed/

  1. 導入したい画像をpublic配下のimagesフォルダなどに格納
  2. seedデータを記述する

Food.create!(
   name: 'りんご',
   category: '果物',
   calorie: 120,
   image:File.open("./public/images/ファイル名.jpg"),
)

img_uploader's People

Contributors

kei-kamiguchi avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.