Image Paperclip::Errors::NotIdentifiedByImageMagickError #Windows - ruby-on-rails

I'm getting tired of this error...
I tried everything that's on the internet (that I found so far)
Gem
gem 'paperclip', '~> 4.3', '>= 4.3.6'
config/environments/development.rb
Paperclip.options[:command_path] = "/c/Program Files/ImageMagick-7.0.1-Q16/"
Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin'
path for ImageMagick/convert
https://gyazo.com/2e8714546606b796b63f5b64663cab31
file.exe it installed
https://gyazo.com/5d0d3d5723c52e6cc812d72202ba4038
my model
has_attached_file :image, styles: { medium: "300x300>"}
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/

This might be a little late.
Lots of people with this issue are running Windows.
Image Magick should be installed correctly (all options selected during install). It is a big step to reinstall and reboot so leave this option until last.
Open config/environments/development.rb
Add the following line: `Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin'. You check the path (mine is shown) by running which convert at the command line. Not the command line that ships with windows. I run git bash.
Restart your Rails server
Check your model. My syntax in app\models\post.rb was
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
which I changed to
has_attached_file :image, style: { :medium => "300x300>", :thumb => "100x100>" }
You may find the error goes but the image is not showing. Check your code in the show page.
If I remove (:medium) the image shows fine.
It doesn't show when I leave the code like below (Haml, sorry!)
= image_tag #recipe.image.url(:medium), class: "recipe_image"

Related

Paperclip gem with Ruby IO File Object

I'm trying to attach images to an ActiveRecord object with the Paperclip gem. However, I'm trying to attach images, not from a form, but from a script that looks for images in a folder.
Here is my code
file = File.open("some_image.jpg", "r")
my_object.image = file
my_object.save
file.close
The script runs successfully (no errors). However, when I check the website, the images are not showing up. The images were apparently never attached.
I've looked through the documentation, but wasn't able to find anything that solved the problem.
Also, here is MyObject class
class MyObject < ActiveRecord::Base
# Attachments
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
Any ideas?

Setting up cloudinary and paperclip

I used the paperclip gem to upload images onto my app. Now I'm trying to deploy to Heroku and want to use Cloudinary to host the images. I followed the documentation and added my cloudinary.yml file into my config directory. Even locally when I add this my images disappear
this works locally:
has_attached_file :avatar, styles: { medium: "300x300", thumb: "100x100" }
Adding the commands for cloudinary makes all my images disappear:
has_attached_file :avatar, styles: { medium: "300x300", thumb: "100x100" },:storage => :cloudinary,
:path => ':id/:style/:filename'
If I try to push to heroku it also doesn't work.

Ruby on rails gem paperclip How to generate missing image

I uploaded a missing.png to public/images/ folder. But the the missing.png not showing correctly.
The website requires images/small/missing.png
So I guest I should generate the missing.png for thumb small medium large size.
What should I do?
Update 1:
I manually create a folder public/images/small and put missing.png inside the small folder. The website shows the missing.png. But What's is the correct way to generate all size of missing.png?
Try to add this in your model:
has_attached_file :image, styles: { :small => "150x150>", medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"

ruby on rails - 1 error prohibited this listing from being saved: Image has contents that are not what they are reported to be

1 error prohibited this listing from being saved:
Image has contents that are not what they are reported to be
That's the error that I get when I try to upload a picture for a listing. I've tried various types of validations and nothing is working.
This is what my Model looks like.
class Listing < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "200x", :thumb =>"100x100>" }, :default_url => "default.jpg"
validates_attachment :image, content_type: { content_type: /\Aimage\/.*\Z/ }
end
Can someone please explain to me what I did wrong, and what I can do to fix it. I really want to keep working on this application but I've hit a problem!
You need to change the following
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
Also make sure that you are indeed uploading images with the mentioned extensions
It may be caused because you don't have file program (probably you don't use Unix system?)
file is used by paperclip to check whether sent file is what it should be or not (based on your validator).
If you are on Windows, you should manually download and install file for Windows and add it to either
Windows PATH environment variable,
project environments (config/environments/development.rb, add line Paperclip.options[:command_path] = '{your_path_to_dir_with_file/bin}' )
After that, restart console, server and it should work.
This is what you can do only for windows :
Install the exe by
download the exe from this link file.exe
test if is well installed by running your cmd and put the following instructions convert logo: logo.miff then run ' imdisplay logo.miff '
you will get custom logo image,that will pop up on your windows screen.
From here now you can start configuring everything on rails app
Open config/environments/development.rb
Add the following line: Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin'
If your rails server is currently running,brake the server and then run again rails s.After that you should be ready to go.Upload image on your app

Rails Paperclip image compression compared to what Page Speed produces

I've set up paperclip in rails and everything is working hunky-dory (i actually had to google that...:).
I've noticed however that Page Speed tells me I could losslessly compress my thumbnail and large images (the ones that paperclip produces) further. Is there an option I can put into my model which does this? I've noticed that mod_deflate doesn't compress images (I'm using Firefox).
You can add compression to paperclip processing using the paperclip-compression gem.
In your Gemfile:
gem "paperclip-compression", "~> 0.1.1"
(of course run bundle install)
In your model:
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:processors => [:thumbnail, :compression]
"jpegtran works by rearranging the compressed data (DCT coefficients), without ever fully decoding the image. Therefore, its transformations are lossless"
Note: if you are running on heroku, you'll need jpegtran, and optipng binaries added to your application. Here's a good article on running binaries on heroku.
You should do your own testing on various JPEG compression levels but I've noticed that I can bump ImageMagicks quality setting down to 75 and still not see any noticeable difference - with about a 30-40% file size savings.
My model looks like:
has_attached_file :photo,
:styles => {
:"185x138" => {
:geometry => "185x138>"
} },
:convert_options => {
:all => "-auto-orient",
:"185x138" => "-quality 75",
-quality 75 is for ImageMagick. If you're using a different processor you will need to adjust accordingly.
What about FFMPEG or AVCONV?
sudo apt-get install ffmpeg/avconv
= initializer
Paperclip.options[:command_path] = "/usr/bin/" # see `which ffmpeg`
= Modal
after_save :compress_with_ffmpeg
def compress_with_ffmpeg
[:thumb, :original, :medium].each do |type|
img_path = self.avtar.path(type)
Paperclip.run("ffmpeg", " -i #{img_path} #{img_path}")
end
end

Resources