Search code examples
amazon-s3carrierwaveimage-uploadingminimagick

Carrierwave, Minimagick image upload to S3 not working


I want to save several version of images. So I'm following and customizing uploading-images-with-carrierwave-to-s3-on-rails, but my image parameters are not allowed by form, but I don't know why. My model

class Attachment < ActiveRecord::Base
    mount_uploader :img, S3uploaderUploader
end

My form looks like this.

<%= form_tag(img_upload_create_path, { multipart: true, method: "POST"}) do %>
  <div class="fileupload btn btn-default">                  
    <span>file open</span>
    <%= file_field_tag 'user_pic[]', multiple: true, accept:'image/png,image/gif,image/jpeg', class: "pictures btn btn-success" %>
  </div>
  <%= submit_tag "upload", :class => "btn btn-success btn-lg" %>
  </div>  
<% end %>

And my controller

class ImgUploadController < ApplicationController
  def create
    params[:user_pic].each do |pic|
      Attachment.create(
            img: pic
      )
    end
    redirect_to :back
  end 
end

My uploader,

class S3uploaderUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick      
  storage :fog

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
  def cache_dir
    '/tmp/cache/s3file'
  end
  def extension_white_list
    %w(jpg jpeg png gif)
  end
  #version1
  version :detailVertical do
    process :quality => 50
  end
  #version2
  version :detailHorizontal do
    process :quality => 50
  end
  #version3
  version :mainVertical, :from_version => :detailVertical do
    process :resize_to_fit => [240, 320]
    process :quality => 100
  end
  #version4
  version :mainHorizontal, :from_version => :detailHorizontal do
    process :resize_to_fit => [240, 180]
    process :quality => 100
  end
end

And my config/initializers/carrierwave.rb

module CarrierWave
  module MiniMagick
    def quality(percentage)
      manipulate! do |img|
        img.quality(percentage.to_s)
        img = yield(img) if block_given?
        img
      end
    end
  end
end

I added log here, when I submit images.

Processing by ImgUploadController#create as HTML
  Parameters: {"utf8"=>"✓", 

"authenticity_token"=>"c6z+PUcBk59+ig/SUMcRrkxwFbTcA
rKnZCMu0ag0Rmp3JQzvXLWxbQ2SF7f/7VazG8Iwcxy5CWQ+nREPVJ10Jw==",
"user_pic"=>[#<ActionDispatch::Http::UploadedFile:0x007facb8033df8 
@tempfile=#<Tempfile:/home/ubuntu/workspace/RackMultipart20160106-5826-109snyd.jpg>, @original_filename="yhouse7.jpg", 
@content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user_pic[]\"; filename=\"yhouse7.jpg\"\r\nContent-Type: image/jpeg\r\n">,
 #<ActionDispatch::Http::UploadedFile:0x007facb8033dd0 @tempfile=#
<Tempfile:/home/ubuntu/workspace/RackMultipart20160106-5826-d9ni1l.jpg>, @original_filename="yhouse7-2.jpg", @content_type="image/jpeg", 
@headers="Content-Disposition: form-data; name=\"user_pic[]\"; 
filename=\"yhouse7-2.jpg\"\r\nContent-Type: image/jpeg\r\n">], 
"commit"=>"upload"}

Solution

  • Ok, after a long trip to find solution, I did it. I didn't install imagemagick. I just downloaded

    gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'
    gem "fog-aws"
    gem 'mini_magick'
    

    those three gems. Please download imagemagick using

    sudo apt-get -y install imagemagick
    

    Before you start your work with Minimagick.