Ruby on Rails: How do you check if a file is an image?
I would use the ruby-filemagic gem which is a Ruby binding for libmagic.
How to detect a file type in a Rails app?
I guess (is not a good thing to let other people guess what you should already provided in your question) you have a model Document and your uploader is media, something like this:
class Document < ActiveRecord::Base
mount_uploader :media, MediaUploader
end
If this is the case, for each document you get the extension (document.media.file.extension.downcase
) and compare it with 'jpg', 'jpeg', 'png'
<% document.each do |doc| %>
<% if ['jpg', 'jpeg', 'png'].include?(document.media.file.extension.downcase) %>
<%= link_to image_tag(doc.media_url(:thumb)) %>
<% else %>
<%= link_to doc.media.path %> # This link downloading the file
<% end %>
<% end %>
Carrierwave can give you the content type if you want it by using:
document.media.content_type # this returns image/png for a png file ...
Edit:
I think a better way is to check it like this (it's cleaner):
<% document.each do |doc| %>
<% if document.media.content_type =~ /image/ %>
<%= link_to image_tag(doc.media_url(:thumb)) %>
<% else %>
<%= link_to doc.media.path %> # This link downloading the file
<% end %>
<% end %>
Verifying a remote image is actually an image file in ruby?
I would check to see if the service returns the proper mime types in the Content-Type HTTP header. (here's a list of mime types)
For example, the Content-Type of the StackOverflow homepage is text/html; charset=utf-8
, and the Content-Type of your gravatar image is image/png
To check the Content-Type header for image
in ruby using Net::HTTP, you would use the following:
def remote_file_exists?(url)
url = URI.parse(url)
Net::HTTP.start(url.host, url.port) do |http|
return http.head(url.request_uri)['Content-Type'].start_with? 'image'
end
end
How to know if an image has been uploaded or not? - Paperclip
When you added Paperclip to your model you added paperclip specific rows, mine are
cover_file_name
cover_content_type
cover_file_size
cover_updated_at
Then I check whether it is nil or not
Foo_Class.cover_file_name.nil?
check If images exists in rails
Write a helper method in application_helper.rb:
def print_image(url)
url = Rails.application.assets.find_asset(url).nil? ? nil : url
image_tag url || 'default_image.jpg'
end
Then in your view, use this:
<%= print_image(pco.printable_photo.url(:small)) %>
Try to avoid writing logic in your view. Take the help of view helpers.
How to check if image is available in ruby?
Why not just testing if the file exists?
stu_image ="/admin_num.jpg"
FileTest.exists?(RAILS_ROOT + stu_image ) # < rails 3.0
FileTest.exists?(Rails.root + stu_image ) # >= rails 3.0
How to check if image exists in Rails?
You can use File.exist?.
if FileTest.exist?("#{RAILS_ROOT}/public/images/#{img}")
image_check = image_tag("#{img}",options)
else
image_check = image_tag("products/noimg.gif", options)
end
Related Topics
Ruby Class Instance Variables and Inheritance
Rails 5 Actioncontroller::Invalidauthenticitytoken Error
Why Doesn't This Work If in Ruby Everything Is an Object
How to Force One Field in Ruby's CSV Output to Be Wrapped with Double-Quotes
Rails 4.1 Mailer Previews and Devise Custom Emails
How to Recursively Require All Files in a Directory in Ruby
How to Profile Ruby 1.9.2 Scripts with Memory Allocation Reports
How to Assert Certain Method Is Called with Ruby Minitest Framework
Get Text Directly Inside a Tag in Nokogiri
Rails - Displaying Foreign Key References in a Form
Change Error Field Name in Rails
Ruby Create Recursive Directory Tree
How to Convert a JSON String to an Object
How to Use Rspec Expectations in Irb