Disable auto rotate in paperclip - ruby-on-rails

I'm using Paperclip in my project but some of my users are complaining that it's incorrectly rotating some images.
For some reasons I can't even imagine I figured it out that some files are with wrong exif orientation attributes. I was looking and I saw that paperclip calls ImageMagick by default using -auto-orient. I saw that the Thumbnail processor has an option to turn auto-orient on or off.
But I couldn't find a way to pass this to the Processor.
This is the code I have:
has_attached_file :photo,
styles: { :square => "400x400#" }
Does anyone now how to do that?
Thanks!

In the end I created a new processor which extends from the paperclip default Thumbnail processor to send the correct options.
class WithouAutoOrientProcessor < Paperclip::Thumbnail
def initialize(file, options = {}, attachment = nil)
options[:auto_orient] = false
super
end
end
And in the model I added
has_attached_file :photo,
styles: { :square => "400x400#" },
processors: [:WithouAutoOrientProcessor]

Although it is a valid option to add your own processor, this is how you pass the option to the processor:
In your styles hash replace your dimension strings with another hash
Put your old dimensions in the key geometry into this hash
The other key/value pairs are the options passed to the processor
You can of course pass auto_orient: false, too
Applying this to your model's code:
has_attached_file :photo,
styles: { square: { geometry: "400x400#", auto_orient: false } }

Related

Rails Active Storage: How to create "named variants" that are cropped by user-supplied coordinates

I have something like:
class User < ApplicationRecord
has_one_attached :avatar do |attachable|
attachable.variant :large, resize_to_limit: [300, nil]
attachable.variant :medium, resize_to_limit: [100, nil]
attachable.variant :small, resize_to_limit: [ 50, nil]
end
end
How do I create...
such "named variants"
that are (first) cropped by user-supplied coordinates such as: [x, y, width, height]
while keeping the uploaded (original) file unchanged?
Is it possible to "pass the coordinates to the model" somehow? If yes, how?
If not: Would the cropping need to happen separately in a controller action, creating a cropped version (of the original file), based upon which the "named variants" would be created? If so, how would that look?
This was added to Rails 7 (https://github.com/rails/rails/pull/39135):
class User < ActiveRecord::Base
has_one_attached :avatar, variants: {
thumb: { resize: "100x100" },
medium: { resize: "300x300", monochrome: true }
}
end
class Gallery < ActiveRecord::Base
has_many_attached :photos, variants: {
thumb: { resize: "100x100" },
medium: { resize: "300x300", monochrome: true }
}
end
<%= image_tag user.avatar.variant(:thumb) %>
I'll try to answer your question:
I don't tink you can pass params to variant that way as definition is in model. You can generate the variant in a method (in the controller - in a before/after save/create filter) or in a view using: user.avatar.variant(:large). If done in the view append .processed to retrieve the already generated variant if present.
To crop and resize add: resize_to_limit: [300, 100], crop: '200x300+0+0'
If you want to get dimensions and coordinates from params then i would not use named variant, but something like
In model:
has_one_attached :avatar
In controller:
def image_height
params[:height]
end
def generate_variant_small
#user.avatar.variant(resize_to_limit: "300x#{image_height}^", crop: '200x300+0+0').processed
end
Original image will be untouched. You upload original image, then all variants will be processed as new images.

validates_attachment for optional field

I have an uload field which is optional, it can be left empty. But when it is is used, I want to validate the size and content of the attachment. So I use this validation in the model:
validates_attachment :attachment, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }, size: { in: 0..500.kilobytes }
This works when there is an attachment, but fails when it is left empty. How can I make sure it only validates when there is an attached file?
The solutions mentioned here are not working unfortunately.
The link you provided is giving you what I would suggest - using the if: argument
--
if:
Using if: in your validation basically allows you to determine conditions on which the validator will fire. I see from the link, the guys are using if: :avatar_changed?
The problem you've likely encountered is you can either use a Proc or instance method to determine the condition; and as these guys are using a method on avatar (albeit an inbuilt one), it's not likely going to yield the result you want.
I would do this:
validates_attachment :attachment, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }, size: { in: 0..500.kilobytes }, if: Proc.new {|a| a.attachment.present? }
This basically determines if the attachment object is present, providing either true or false to the validation
try this:
has_attached_file :attachment, :styles => { :small => "200x200>" }
validates_attachment :attachment,
:size => { :in => 0..500.kiobytes },
:content_type => { :content_type => /^image\/(jpeg|png|gif|tiff)$/ }
its working on my app. except i have set a default attachment in case user chooses not to upload one.

Use paperclip for saving base64 images obtained from an api

I have a Photo model with an image attribute. The image contains a base64 string obtained from an api. I need to run an after_create callback and I was thinking I could use Paperclip for saving the image to the disk in the callback as it would save me some work implementing the folder structure in the public folder and generating thumbnails. Is there an easy way to do that?
To answer my own question, here is what I've come up with:
class Photo < ActiveRecord::Base
before_validation :set_image
has_attached_file :image, styles: { thumb: "x100>" }
validates_attachment :image, presence: true, content_type: { content_type: ["image/jpeg", "image/jpg"] }, size: { in: 0..10.megabytes }
def set_image
StringIO.open(Base64.decode64(image_json)) do |data|
data.class.class_eval { attr_accessor :original_filename, :content_type }
data.original_filename = "file.jpg"
data.content_type = "image/jpeg"
self.image = data
end
end
end
image_json is a text field containing the actual base64 encoded image (just the data part, eg "/9j/4AAQSkZJRg...")
your set_image should look something like this
def set_image
self.update({image_attr: "data:image/jpeg;base64," + image_json[PATH_TO_BASE64_DATA]})
end
At least with Paperclip 5 it works out of the box you need to provide base64 string with format data:image/jpeg;base64,#{base64_encoded_file}
For you model it will be
Photo.new(
image: "data:image/jpeg;base64,#{image_json}",
image_file_name: 'file.jpg' # this way you can provide file_name
)
Additionally in your controller you do not need to change anything:-) (maybe you would like to accept :image_file_name in params)
As of Paperclip 5.2 you need to register the DataUriAdapter for Paperclip to handle base64 images for you.
In config/initializers/paperclip put:
Paperclip::DataUriAdapter.register
Then as #eldi says you can just do:
Photo.new(
image: "data:image/jpeg;base64,#{image_json}",
image_file_name: 'file.jpg' # this way you can provide file_name
)
(See Paperclip release notes here)
require 'RMagick'
data = params[:image_text]# code like this data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABPUAAAI9CAYAAABSTE0XAAAgAElEQVR4Xuy9SXPjytKm6ZwnUbNyHs7Jc7/VV9bW1WXWi9q
image_data = Base64.decode64(data['data:image/png;base64,'.length .. -1])
new_file=File.new("somefilename.png", 'wb')
new_file.write(image_data)
After you kan use image as file
Photo.new(image: image)#save useng paperclip in Photo model

Ruby validation - multiple permutations for a single hash

Say there is a hash field that can have two possible value permutations, "foo" and "bar". How can I validate the hash value is one of the two?
class ValidateMe
validates :type => { :type => "foo" or :type => "bar" }
end
This results in an error. What is the proper way to handle this use case?
My actual case is using Paperclip to attach an image. I need to enforce the image is only .png or .jpg
class ValidateMe
validates_attachment :image,
presence => true,
:content_type => { :content_type => "image/png" }
end
Help with either code block is greatly appreciated. Thanks!
The best way to do this would be to pass an array of types to :content_type
class ValidateMe
validates_attachment :image,
presence => true,
:content_type => { :content_type => ['image/png', 'image/jpeg'] }
end
(My answer is based on code in Paperclip - Validate File Type but not Presence)
This can also be done using regular expressions. (Not as preferable)
class ValidateMe
validates_attachment :image,
presence => true,
:content_type => { :content_type => /^image\/(jpeg|png)$/ }
end
(source How can I restrict Paperclip to only accept images?)
I think Btuman's first answer would be considered canonical: The content_type key of content_type in validates_attachment can accept an array of valid content-types.
You can use :inclusion attribute for more see this http://guides.rubyonrails.org/active_record_validations_callbacks.html

Image orientation and validation with Paperclip?

I'm looking for a way to determine image orientation preferably with Paperclip, but is it even possible or do I need to user RMagick or another image library for this?
Case scenario: When a user uploads an image i want to check the orientation/size/dimensions to determine if the image is in portrait/landscape or square and save this attribute to the model.
Here's what I generally do in my image models. Perhaps it will help:
I use IM's -auto-orient option when converting. This ensures images are always rotated properly after upload
I read the EXIF data after processing and get the width and height (among other things)
You can then just have an instance method that outputs an orientation string based on width and height
has_attached_file :attachment,
:styles => {
:large => "900x600>",
:medium => "600x400>",
:square => "100x100#",
:small => "300x200>" },
:convert_options => { :all => '-auto-orient' },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:s3_permissions => 'public-read',
:s3_protocol => 'https',
:path => "images/:id_partition/:basename_:style.:extension"
after_attachment_post_process :post_process_photo
def post_process_photo
imgfile = EXIFR::JPEG.new(attachment.queued_for_write[:original].path)
return unless imgfile
self.width = imgfile.width
self.height = imgfile.height
self.model = imgfile.model
self.date_time = imgfile.date_time
self.exposure_time = imgfile.exposure_time.to_s
self.f_number = imgfile.f_number.to_f
self.focal_length = imgfile.focal_length.to_s
self.description = imgfile.image_description
end
Thanks for the answer jonnii.
Although I did find what I was looking for in the PaperClip::Geometry module.
This worked find:
class Image < ActiveRecord::Base
after_save :set_orientation
has_attached_file :data, :styles => { :large => "685x", :thumb => "100x100#" }
validates_attachment_content_type :data, :content_type => ['image/jpeg', 'image/pjpeg'], :message => "has to be in jpeg format"
private
def set_orientation
self.orientation = Paperclip::Geometry.from_file(self.data.to_file).horizontal? ? 'horizontal' : 'vertical'
end
end
This of course makes both vertical and square images have the vertical attribute but that's what I wanted anyway.
When I take a photo with my camera the dimensions of the image are the same regardless if the photo is landscape or portrait. However, my camera is smart enough to rotate the image for me! How thoughtful! The way this work is that is uses something called exif data which is meta data placed on the image by the camera. It includes stuff like: the type of camera, when the photo was taken, orientation etc...
With paperclip you can set up callbacks, specifically what you'll want to do is have a callback on before_post_process that checks the orientation of the image by reading the exif data using a library (you can find a list here: http://blog.simplificator.com/2008/01/14/ruby-and-exif-data/), and then rotating the image clockwise or counterclockwise 90 degrees (you won't know which way they rotated the camera when they took the photo).
I hope this helps!

Resources