Trimming and Compression the image of paperclip using rake task - ruby-on-rails

I want to trimming the image of paperclip in my database.
Because I`m running my own private web service, but I forgot to add the styles when user create the image...
has_attached_file :avatar,
:storage => :s3,
:styles => { # I forgot to add this styles
:medium => "370x370#", # I forgot to add this styles
:thumbs => "120x120#" # I forgot to add this styles
}
So I want to run the rake task and trimming like 370×370# and 120×120#.
But I cant`t find the way to trimming the image after user save.
Does anyone assist me ?

The paperclip gem comes with a rake task for exactly this purpose:
rake paperclip:refresh:thumbnails
Just run that from the command line and it will take care of generating all of your reduced-size images.

Related

Can't upload zip files using ruby on rails and paperclip gem

I'm working on a rails web application. Just created and prepared some models and stuff. The application is very simple. But now I have a problem by setting up the gem paperclip to add attachments to a model. Almost everything works fine, like attaching images/jpg or even pdf.
But I can't upload zip files. I tried different zip files, but I'm always getting:
"Attachment Paperclip::Errors::NotIdentifiedByImageMagickError"
This is my model:
class Order < ActiveRecord::Base
has_attached_file :attachment, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :attachment, :content_type => ["application/pdf", "application/zip", "application/x-zip", "application/x-zip-compressed","application/octet-stream","image/jpg","image/png"]
belongs_to :client
has_one :status
end
I'm developing on a Mac (Yosemite), installed imagemagick via brew and using SQLite.
I added this to my Gemfile:
gem "paperclip", "~> 4.2"
gem 'cocaine', '~> 0.5.4'
I did research on google the last hours, and there are many people struggling with paperclip, but I didn't find anybody with problems uploading zip files.
Maybe someone can help here.
Thanks
ImageMagick can't read .zip files. See the acceptable file types here:
http://www.imagemagick.org/script/formats.php
If you're trying to generate a thumbnail from a zip file ImageMagick should fail every time.
Try adding this to your model:
before_post_process :skip_for_zip
def skip_for_zip
! %w(application/zip application/x-zip).include?(asset_content_type)
end
Then your app won't try to process zip files as images

Paperclip with sidekiq

I am using paperclip for uploading files. Does anybody have use it with sidekiq for background jobs?
I was trying to achieve something similar to railscast 383 (uploading to Amazon S3) but with paperclip and sidekiq.
I didn't find too much information for using it with sidekiq and I am thinking if I should change to carrierwave or if there is any example for paperclip and sidekiq (not with FancyUploader and delayed_jobs).
I think what you're looking for is the delayed_paperclip gem:
https://github.com/jrgifford/delayed_paperclip/
According to the gem it has a nice ActiveRecord api so you can do something like:
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
process_in_background :avatar
end

Image file is not recognized by the 'identify' command. (heroku)

I'm trying to have Paperclip working with Heroku and Amazon S3.
Everything works fine on localhost (mac OS and Amazon), but when I'm deploying to heroku and trying the feature, I have this error :
2 errors prohibited this area from being saved:
Asset /tmp/paris20121005-2-2cwxgx.jpg is not recognized by the 'identify' command.
Asset /tmp/paris20121005-2-2cwxgx.jpg is not recognized by the 'identify' command.
It works when I remove the :styles => { } option in my model, but the file isn't processed (I need different image sizes).
I also have the rmagick gem in my gemfile.
Here is my gemfile (only the paperclip part) :
gem "paperclip"
gem "rmagick", :require => 'RMagick'
gem 'aws-sdk', '~> 1.3.4'
I don't have Paperclip.options[:command_path] set in my environment.rb or production.rb so no problem on this side.
Here is my model :
class Area < ActiveRecord::Base
require 'RMagick'
has_attached_file :asset, :styles => { :medium => "300x300>", :thumb => "180x190>" },
:storage => :s3,
:s3_credentials => "#{::Rails.root.to_s}/config/s3.yml",
:url => :s3_domain_url.to_s,
:path => "/:style/:id/:filename"
end
Any clue on that ? I've crawled every topics about it and nothing seems to work...
Thanks
Apparently the new update to Cocaine gem (0.4.0) breaks the file names for Paperclip and ImageMagick. try rolling back to the previous version (0.3.2), it worked for me.
See here:
https://github.com/thoughtbot/paperclip/issues/1038
PS I believe RMagick is no longer needed on Heroku, works fine for me without it

The perfect way to upload file in Rails 3.1

I'm new in Ruby/Rails, so, my question: what is the perfect way to upload file and save filename in database with rails? Are there any Gem? Or maybe there are good build-in feature?
Also try carrierwave, Ryan has a good screencast about this gem
Just take a look on the links to choose between paperClip & carrierwave :
Rails 3 paperclip vs carrierwave vs dragonfly vs attachment_fu
And
http://bcjordan.github.com/posts/rails-image-uploading-framework-comparison/
You can try Paperclip.It is most popular gem in rails community...
https://github.com/thoughtbot/paperclip
these lines do the all stuff
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
Try it......
Most people use the Paperclip Gem.

How can I make Paperclip process files from a server directory?

I want to run Paperclip on all files in a directory on the server. Basically, I would like to allow users to FTP some files to my webserver, then I can manually run a rake task to have Paperclip process all of the files (resize the images, update the database, etc).
How can I do this?
I'm not sure if I understood your question - are you asking to run the rake task remotely or how to import images?
In the later case there is an answer.
First you need some Model to keep the images and maybe some other data, something like this:
class Picture < ActiveRecord::Base
has_attached_file :image, :styles => {
:thumb => "100x100>",
:big => "500x500>"
}
end
You can create simple rake task in your lib/tasks folder (you should name the file with .rake extension)
namespace :import do
desc "import all images from SOURCE_DIR folder"
task :images => :environment do
# get all images from given folder
Dir.glob(File.join(ENV["SOURCE_DIR"], "*")) do |file_path|
# create new model for every picture found and save it to db
open(file_path) do |f|
pict = Picture.new(:name => File.basename(file_path),
:image => f)
# a side affect of saving is that paperclip transformation will
# happen
pict.save!
end
# Move processed image somewhere else or just remove it. It is
# necessary as there is a risk of "double import"
#FileUtils.mv(file_path, "....")
#FileUtils.rm(file_path)
end
end
end
Then you can call manually rake task from the console providing SOURCE_DIR parameter that will be the folder on the server (it can be real folder or mounted remote)
rake import:images SOURCE_DIR=~/my_images/to/be/imported
If you are planning to run this automatically I'd recommend you to go for Resque Scheduler gem.
Update: To keep things simple I've deliberately omitted exception handling

Resources