Paperclip - Save image dimensions to database from already uploaded images to S3 - ruby-on-rails

I have big troubles...
I have uploaded around 500 images to S3 via my Rails app in a couple of different sizes. Now I realize that I need to store those images dimensions to my database. My approach to do this is with this migration:
class AddImageDimensionsToImages < ActiveRecord::Migration
def change
# add image columns
add_column :images, :small_width, :integer
add_column :images, :small_height, :integer
add_column :images, :medium_width, :integer
add_column :images, :medium_height, :integer
add_column :images, :large_width, :integer
add_column :images, :large_height, :integer
add_column :images, :original_width, :integer
add_column :images, :original_height, :integer
# loop all images
Image.all.each do |image|
# get sizes
geo_small = Paperclip::Geometry.from_file(image.image.to_file(:small))
geo_medium = Paperclip::Geometry.from_file(image.image.to_file(:medium))
geo_large = Paperclip::Geometry.from_file(image.image.to_file(:large))
geo_original = Paperclip::Geometry.from_file(image.image.to_file(:original))
# set sizes
image.small_width = geo_small.width if geo_small
image.small_height = geo_small.height if geo_small
image.medium_width = geo_medium.width if geo_medium
image.medium_height = geo_medium.height if geo_medium
image.large_width = geo_large.width if geo_large
image.large_height = geo_large.height if geo_large
image.original_width = geo_original.width if geo_original
image.original_height = geo_original.height if geo_original
# save image
image.save
end
end
end
When I run this migration it goes like this:
...
-- add_column(:images, :small_width, :integer)
-> 2.0866s
-- add_column(:images, :small_height, :integer)
-> 0.0092s
-- add_column(:images, :medium_width, :integer)
-> 0.0073s
...
Command :: identify -format %wx%h '/app/tmp/HayUnFinal_1620120408-4-qpzyym.jpg[0]'
[AWS S3 200 0.106467] get_object(:bucket_name=>"aa",:key=>"original/34/HayUnFinal_16.jpg")
Command :: identify -format %wx%h '/app/tmp/HayUnFinal_1620120408-4-1vqo71y.jpg[0]'
[paperclip] Saving attachments.
[AWS S3 200 0.152898] get_object(:bucket_name=>"aa",:key=>"small/64/Portrait_2.jpg")
Command :: identify -format %wx%h '/app/tmp/Portrait_220120408-4-i1ohlr.jpg[0]'
[AWS S3 200 0.055378] get_object(:bucket_name=>"aa",:key=>"medium/64/Portrait_2.jpg")
Command :: identify -format %wx%h '/app/tmp/Portrait_220120408-4-viizzf.jpg[0]'
[AWS S3 200 0.079235] get_object(:bucket_name=>"aa",:key=>"large/64/Portrait_2.jpg")
Command :: identify -format %wx%h '/app/tmp/Portrait_220120408-4-1d2u00o.jpg[0]'
[AWS S3 200 0.066724] get_object(:bucket_name=>"aa",:key=>"original/64/Portrait_2.jpg")
Command :: identify -format %wx%h '/app/tmp/Portrait_220120408-4-jq9wmg.jpg[0]'
[paperclip] Saving attachments.
[AWS S3 200 0.104891] get_object(:bucket_name=>"aa",:key=>"small/58/Bansah_24.jpg")
Command :: identify -format %wx%h '/app/tmp/Bansah_2420120408-4-an2mpt.jpg[0]'
[AWS S3 200 0.064148] get_object(:bucket_name=>"aa",:key=>"medium/58/Bansah_24.jpg")
Command :: identify -format %wx%h '/app/tmp/Bansah_2420120408-4-tzmnup.jpg[0]'
[AWS S3 200 0.051090] get_object(:bucket_name=>"aa",:key=>"large/58/Bansah_24.jpg")
Command :: identify -format %wx%h '/app/tmp/Bansah_2420120408-4-1pa0rxh.jpg[0]'
[AWS S3 200 0.063531] get_object(:bucket_name=>"aa",:key=>"original/58/Bansah_24.jpg")
Command :: identify -format %wx%h '/app/tmp/Bansah_2420120408-4-kogk2g.jpg[0]'
[paperclip] Saving attachments.
rake aborted!
An error has occurred, this and all later migrations canceled:
can't convert nil into String
rake aborted! It seems to work for a couple of images, but then the migration fails. I can't really figure out where that nil comes from, and how I can prevent it. Can anyone help me with this?
Bonus question: Is this a retared way to approach the task of saving image dimensions of already uploaded images?
Thanks a lot in advance!

Some image rows in the database had null image values. A check like this made the trick:
Image.all.each do |image|
# check that we have an image
if image.image_file_size
...
end
end

Related

Rails 5, paperclip not validating the attachment?

I am getting this response:
Command :: file -b --mime "C:/Users/Ben/AppData/Local/Temp/5523c88dd347d1b7cc617f632b7efdb720171021-12076-b6btes.jpg"
[paperclip] Content Type Spoof: Filename bg.jpg (image/jpeg from Headers, ["image/jpeg"] from Extension), content type discovered from file command: . See documentation to allow this combination.
(0.0ms) BEGIN
Command :: file -b --mime "C:/Users/Ben/AppData/Local/Temp/5523c88dd347d1b7cc617f632b7efdb720171021-12076-1fvpv4a.jpg"
[paperclip] Content Type Spoof: Filename bg.jpg (image/jpeg from Headers, ["image/jpeg"] from Extension), content type discovered from file command: . See documentation to allow this combination.
(0.6ms) ROLLBACK
On Windows
And I have this in my model:
has_attached_file :image
# Validate content type
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
# Explicitly do not validate
do_not_validate_attachment_file_type :image
And the request just fails, I dont understand why is it happening?
Try:
config/initializers/paperclip.rb:
Paperclip.options[:command_path] = '/usr/bin/' # Path to ImageMagick

base 64 photo to paperclip rails image

I have a rails app and I am receiving a base64 encoded string that is an image from an ios app.
I am trying to convert it to a paperclip image.
So far I have:
module Api
module V1
class MyController < BaseController
def image_build
begin
token = request.headers["HTTP_TOKEN"]
#user = User.find_by(user_token: token)
#decoded_image = Base64.decode64(params[:image_data])
puts #decoded_image[0, 50]
#new_stand = Stand.create(:user_id => #user.id, :avatar => #decoded_image)
...
which outputs:
iVBORw0KGgoAAAANSUhEUgAAAPsAAACxCAYAAAACscusAAAAAX
�PNG
IHDR��ˬsRGB���
Completed 500 Internal Server Error in 90ms (Views: 3.1ms | ActiveRecord: 1.1ms)
How do I convert the base64 string to a proper image file in rails? I have paperclip running from this gem:
gem "paperclip", git: "git://github.com/thoughtbot/paperclip.git"
Notes
The images sent over can be any image from an ios phone so the type can change. I would like to support .gif, .png, .jpg, and jpeg.
In IOS I send over the information like this:
#IBOutlet var image: UIImage
#IBAction func Build(sender: AnyObject) {
let image_data = UIImagePNGRepresentation(image.image!)
self.service.createNewImage(notifier: notifier, image: image_data!)
}
then
let strBase64:String = image.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
let dataDictionary = ["image_data": strBase64]
self.post("image/build", data: dataDictionary).responseJSON
EDIT 1
I have started to attempt to use the paperclip io adapters but it still errors out. This is what I have so far:
#new_image = Stand.create(:user_id => #user.id)
encoded_picture = params[:image_data]
content_type = "image/jpg"
puts "HERE 1"
image = Paperclip.io_adapters.for("data:#{content_type};base64,#{encoded_picture}")
puts "HERE 2"
image.original_filename = "new_image.jpg"
puts "HERE 3"
#new_image.avatar = image
puts "HERE 4"
#new_image.cover_image_url = #new_image.avatar.url
puts "HERE 5"
#new_image.save
This code gives these errors:
HERE 1
HERE 2
HERE 3
Command :: file -b --mime '/tmp/082995477845923f853c5a48cc6e3cae20160712-26217-s1dngb.jpg'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/8d777f385d3dfec8815d20f7496026dc20160712-26217-1ht7ijm[0]' 2>/dev/null
Command :: identify -format %m '/tmp/8d777f385d3dfec8815d20f7496026dc20160712-26217-1ht7ijm[0]'
Command :: convert '/tmp/8d777f385d3dfec8815d20f7496026dc20160712-26217-1ht7ijm[0]' -auto-orient -resize "160x160>" '/tmp/4a98b13d8ce55141b173bb0ed5cb181220160712-26217-17sazsk'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/8d777f385d3dfec8815d20f7496026dc20160712-26217-1ht7ijm[0]' 2>/dev/null
Command :: identify -format %m '/tmp/8d777f385d3dfec8815d20f7496026dc20160712-26217-1ht7ijm[0]'
Command :: convert '/tmp/8d777f385d3dfec8815d20f7496026dc20160712-26217-1ht7ijm[0]' -auto-orient -resize "40x40>" '/tmp/4a98b13d8ce55141b173bb0ed5cb181220160712-26217-fhdb0w'
HERE 4
Completed 500 Internal Server Error in 654ms (Views: 2.5ms | Searchkick: 15.6ms | ActiveRecord: 7.3ms)
This seems to work, but I am still working on testing differing image types.
#new_image = Stand.create(:user_id => #user.id)
encoded_picture = params[:image_data]
content_type = "image/jpg"
image = Paperclip.io_adapters.for("data:#{content_type};base64,#{encoded_picture}")
image.original_filename = "new_image.jpg"
#new_image.avatar = image
#new_image.save
#new_image.cover_image_url = #new_image.avatar.url
#new_image.save
I have upvoted the previous answer because it helped me find other resources, but it does not contain an answer for my scenario.
A lot of useful information you can found in paperclip's repository, for your question i found this:
3.5.0:
Feature: Handle Base64-encoded data URIs as uploads
Paperclip::DataUriAdapter handles base64-encoded data.
Just works:
class Photo < ActiveRecord::Base
before_validation :set_image
has_attached_file :image
def set_image
StringIO.open(Base64.decode64(image_json)) do |str|
decorate_str(str)
str.original_filename = "THE_NAME_OF_FILE.jpg"
str.content_type = "image/jpeg"
self.image = data
end
end
def decorate_str(str)
str.class.class_eval { attr_accessor :original_filename, :content_type }
end
end
Similar way to solve your problem is here.
Paperclip's changelog

PaperClip gem Paperclip::Errors::NotIdentifiedByImageMagickError

There are numerous Stackoverflow questions regarding this, but after many hours, I've exhausted every angle I could think of, and still get the following error when I try to upload an image using the PaperClip gem (in tandem with PaperCrop, for cropping the image).
Paperclip::Errors::NotIdentifiedByImageMagickError - Paperclip::Errors::NotIdentifiedByImageMagickError:
paperclip (4.3.6) lib/paperclip/geometry_detector_factory.rb:10:in `make'
paperclip (4.3.6) lib/paperclip/geometry.rb:26:in `from_file'
papercrop (0.3.0) lib/papercrop/model_extension.rb:95:in `image_geometry'
papercrop (0.3.0) lib/papercrop/helpers.rb:45:in `cropbox'
app/views/users/_crop_photo_modal.html.haml:12:in `block in _app_views_users__crop_photo_modal_html_haml___1410212035753490924_70365684359680'
Here is what prints in the logs just prior to the error:
[AWS S3 200 0.554305 0 retries] head_object(:bucket_name=>"XXX-development-bucket-us",:key=>"profiles/profile_images/1/original/main_sized_small.png")
[AWS S3 200 0.049585 0 retries] head_object(:bucket_name=>"XXX-development-bucket-us",:key=>"profiles/profile_images/1/thumb/main_sized_small.png")
[AWS S3 200 0.053507 0 retries] head_object(:bucket_name=>"XXX-development-bucket-us",:key=>"profiles/profile_images/1/medium/main_sized_small.png")
[AWS S3 200 0.048024 0 retries] head_object(:bucket_name=>"XXX-development-bucket-us",:key=>"profiles/profile_images/1/large/main_sized_small.png")
Command :: PATH=/usr/local/bin/:$PATH; file -b --mime '/tmp/3aa49ec6bfc910647fa1c5a013e48eef20160601-82031-t5fq8a.png'
Command :: PATH=/usr/local/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' '/tmp/3aa49ec6bfc910647fa1c5a013e48eef20160601-82031-1kh6s2w.png[0]' 2>/dev/null
Command :: PATH=/usr/local/bin/:$PATH; identify -format %m '/tmp/3aa49ec6bfc910647fa1c5a013e48eef20160601-82031-1kh6s2w.png[0]'
Command :: PATH=/usr/local/bin/:$PATH; convert '/tmp/3aa49ec6bfc910647fa1c5a013e48eef20160601-82031-1kh6s2w.png[0]' -auto-orient -resize "50x" -crop "50x50+0+0" +repage '/tmp/96994f01b3cacc5ddb9ed35b539d8c0420160601-82031-1n0cfvq'
Command :: PATH=/usr/local/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' '/tmp/3aa49ec6bfc910647fa1c5a013e48eef20160601-82031-1kh6s2w.png[0]' 2>/dev/null
Command :: PATH=/usr/local/bin/:$PATH; identify -format %m '/tmp/3aa49ec6bfc910647fa1c5a013e48eef20160601-82031-1kh6s2w.png[0]'
Command :: PATH=/usr/local/bin/:$PATH; convert '/tmp/3aa49ec6bfc910647fa1c5a013e48eef20160601-82031-1kh6s2w.png[0]' -auto-orient -resize "100x" -crop "100x100+0+0" +repage '/tmp/96994f01b3cacc5ddb9ed35b539d8c0420160601-82031-1fnck50'
Command :: PATH=/usr/local/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' '/tmp/3aa49ec6bfc910647fa1c5a013e48eef20160601-82031-1kh6s2w.png[0]' 2>/dev/null
Command :: PATH=/usr/local/bin/:$PATH; identify -format %m '/tmp/3aa49ec6bfc910647fa1c5a013e48eef20160601-82031-1kh6s2w.png[0]'
Command :: PATH=/usr/local/bin/:$PATH; convert '/tmp/3aa49ec6bfc910647fa1c5a013e48eef20160601-82031-1kh6s2w.png[0]' -auto-orient -resize "220x" -crop "220x220+0+0" +repage '/tmp/96994f01b3cacc5ddb9ed35b539d8c0420160601-82031-e3r9iy'
Command :: PATH=/usr/local/bin/:$PATH; file -b --mime '/tmp/3aa49ec6bfc910647fa1c5a013e48eef20160601-82031-ub7nk7.png'
(0.4ms) ROLLBACK
Command :: PATH=/usr/local/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' 'https://XXX-development-bucket-us.s3.amazonaws.com/profiles/profile_images/1/original/blake.png?1464811715[0]' 2>/dev/null
My environment:
paperclip v4.3.6
papercrop v0.3.0
aws-sdk v1.66.0
Rails 4.0.0
Ruby 2.0.0p643
Amazon S3 for file storage
OS-X Yosemite 10.10.5 (same error occurs on Heroku Linux server)
I have tried the following:
Use aws-sdk version 2 or higher.
Uninstall all imagemagick installs (I had 2 originally), and reinstall imagemagick with brew install imagemagick
Downgrade PaperClip to 4.1.1
Include :s3_credentials and :s3_permissions directly in the model, as follows:
has_attached_file :profile_image,
:styles => { :thumb => "50x50#", :medium => "100x100#", :large => "220x220#" },
:storage => :s3,
:s3_credentials => {:bucket => "XXX-development-bucket-us", :access_key_id => "XXX", :secret_access_key => "XXX"},
:s3_permissions => "public-read",
:default_url => '/images/:attachment/missing_:style.jpg'
validates_attachment :profile_image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png"] }
crop_attached_file :profile_image, :aspect => "1:1"
I thought it might be an s3 permissions problem, but the permissions on our development bucket should allow all access to the user identified by the keys.
I've also added...
Paperclip.options[:command_path] = "/usr/local/bin"
...to environments/development.rb.
I've verified ImageMagick is working on the commandline.
Any ideas what else to look at?
Yes, there are many exiting answers for this with different suggestions. But I tried out every possible solution for a day and arrived at the solution which fixed for me.
Ubuntu 12.04,Paperclip ~> '4.2'
Initially I was having ImageMagick 6.6.9-7 2016-06-01 Q16, updating this to ImageMagick 7.0.3-1 Q16 x86_64 2016-09-27 fixed it.
Note: Try installing the Imagemagick manually using wget http://www.imagemagick.org/download/ImageMagick.tar.gz

Store thumb image to specific directory in paperclip gem

Here is the code
has_attached_file :image,
:path => ":rails_root/public/images/:id/:filename",
:url => "/images/:id/:filename",
:styles => { :small => "300x168>", :large => "1000x560>" }
Here is my console log
Command :: PATH=/opt/imagemagick-6.9/bin:$PATH; file -b --mime '/tmp/2251fc5821941d6bd28b2ee3cb25bf7620150617-13080-1gg7ekt.png'
Command :: PATH=/opt/imagemagick-6.9/bin:$PATH; identify -format '%wx%h,%[exif:orientation]' '/tmp/2251fc5821941d6bd28b2ee3cb25bf7620150617-13080-197v1bm.png[0]' 2>/dev/null
Command :: PATH=/opt/imagemagick-6.9/bin:$PATH; identify -format %m '/tmp/2251fc5821941d6bd28b2ee3cb25bf7620150617-13080-197v1bm.png[0]'
Command :: PATH=/opt/imagemagick-6.9/bin:$PATH; convert '/tmp/2251fc5821941d6bd28b2ee3cb25bf7620150617-13080-197v1bm.png[0]' -auto-orient -resize "300x168>" '/tmp/2251fc5821941d6bd28b2ee3cb25bf7620150617-13080-197v1bm20150617-13080-8q41x9'
Command :: PATH=/opt/imagemagick-6.9/bin:$PATH; file -b --mime '/tmp/2251fc5821941d6bd28b2ee3cb25bf7620150617-13080-197v1bm20150617-13080-8q41x9'
Command :: PATH=/opt/imagemagick-6.9/bin:$PATH; identify -format '%wx%h,%[exif:orientation]' '/tmp/2251fc5821941d6bd28b2ee3cb25bf7620150617-13080-197v1bm.png[0]' 2>/dev/null
Command :: PATH=/opt/imagemagick-6.9/bin:$PATH; identify -format %m '/tmp/2251fc5821941d6bd28b2ee3cb25bf7620150617-13080-197v1bm.png[0]'
Command :: PATH=/opt/imagemagick-6.9/bin:$PATH; convert '/tmp/2251fc5821941d6bd28b2ee3cb25bf7620150617-13080-197v1bm.png[0]' -auto-orient -resize "1000x560>" '/tmp/2251fc5821941d6bd28b2ee3cb25bf7620150617-13080-197v1bm20150617-13080-q57vxf'
Command :: PATH=/opt/imagemagick-6.9/bin:$PATH; file -b --mime '/tmp/2251fc5821941d6bd28b2ee3cb25bf7620150617-13080-197v1bm20150617-13080-q57vxf'
Command :: PATH=/opt/imagemagick-6.9/bin:$PATH; file -b --mime '/tmp/2251fc5821941d6bd28b2ee3cb25bf7620150617-13080-1o3we23.png'
I need to store small,large and original image to my project public directory but it store only the original
You can use :styles of paperclip gem in your model. Like,
has_attached_file :photo,
:styles => {
:thumb=> "100x100#",
:small => "150x150>",
:medium => "300x300>",
:large => "400x400>" }
And your photo url is going to be as follows : /public/photos/(event#)/(size_name)/image_name
And you have to install ImageMagick and rb-magick ports to get this going.
For more details you can refer this links Upload Image using Paperclip gem
Check that your image magick installed correctly or not as resizing is depends on image magick.
to refresh all of your defined styles in one go (:thumb, :small, :medium from the above example)
rake paperclip:refresh CLASS=Modelname
and to refresh only missing styles:
a list of styles will be defined or updated in a file “/public/system/paperclip_attachments.yml”
rake paperclip:refresh:missing_styles
Additionally, if you want to only reprocess a single style you may do so like:
users_to_reprocess.each do |user|
user.image.reprocess! :small
end

paperclip not displaying medium or thumb in rails 3.1

I'm using paperclip with Rails 3.1. When I add the image, it shows me the original size but doesn't show me thumb or medium sizes:
Here is what I have in my view:
<%= image_tag #image.avatar.url(:thumb) %>
<%= image_tag #image.avatar.url(:medium) %>
image.rb
has_attached_file :avatar, :whiny => false, :styles => { :medium => "300x300>", :thumb => "100x100>" }
UPDATE:
Here is the error I'm getting with :whiny => true
Command :: identify -format %wx%h '/var/folders/54/txjcl9l130j6dq73r37hf2c00000gn/T/stream20111213-9180-1plu1me.png[0]'
[paperclip] An error was received while processing: #
Command :: identify -format %wx%h '/var/folders/54/txjcl9l130j6dq73r37hf2c00000gn/T/stream20111213-9180-1plu1me.png[0]'
[paperclip] An error was received while processing: #
Rendered images/new.html.erb within layouts/application (4.0ms)
First, make sure that Image Magick is installed.
To see if its installed properly, go to a terminal session and type which convert. You should see a path to the executable.
Once that is done, you may need to add the path to your environment.rb file. For example, my convert is located at /usr/local/bin/convert. Now I've seen two different ways of setting your path for paperclip, try one or the other and see what works.
# specifically set the paperclip path
Paperclip.options[:command_path] = '/usr/local/bin'
# set the path in general, might not be necessary
ENV['PATH'] = '/usr/local/bin:' + ENV['PATH']

Resources