I would like to use jpegoptim or optipng to compress the image uploaded by users via Paperclip.
I have a Paperclip model configured as:
has_attached_file :image,
:styles => {:thumb => '50x50>', :preview => '270x270>' },
:url => "/system/:class/:attachment/:id/:basename_:style.:extension",
:path => ":rails_root/public/system/:class/:attachment/:id/:basename_:style.:extension"
Question 1:
Is it possible to compress the original image uploaded by user, then let Paperclip resize it , so there's only one compress process? and how to do it?
Question 2:
I am going to do it via the after_post_process callback, and I could get all the instances of three files from image.queued_for_write and I would like to trigger jpegoptim/optipng by the file extension, but when I use current_format = File.extname(file.path), I get something like: .jpg20120508-7991-cqcpf2. Is there away to get the extension string jpg? or is it safe that I just check if the extension string is contained in that string?
Since there's no other answers, here is how I do it in my project and it seems it's being working ok for several months.
class AttachedImage < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
validates_attachment_presence :image
validates_attachment_content_type :image, :content_type=>['image/jpeg', 'image/png', 'image/gif']
has_attached_file :image,
:styles => {:thumb => '50x50>', :preview => '270x270>' },
# :processors => [:image_compressor],
:url => "/system/:class/:attachment/:id/:basename_:style.:extension",
:path => ":rails_root/public/system/:class/:attachment/:id/:basename_:style.:extension"
after_post_process :compress
private
def compress
current_format = File.extname(image.queued_for_write[:original].path)
image.queued_for_write.each do |key, file|
reg_jpegoptim = /(jpg|jpeg|jfif)/i
reg_optipng = /(png|bmp|gif|pnm|tiff)/i
logger.info("Processing compression: key: #{key} - file: #{file.path} - ext: #{current_format}")
if current_format =~ reg_jpegoptim
compress_with_jpegoptim(file)
elsif current_format =~ reg_optipng
compress_with_optpng(file)
else
logger.info("File: #{file.path} is not compressed!")
end
end
end
def compress_with_jpegoptim(file)
current_size = File.size(file.path)
Paperclip.run("jpegoptim", "-o --strip-all #{file.path}")
compressed_size = File.size(file.path)
compressed_ratio = (current_size - compressed_size) / current_size.to_f
logger.debug("#{current_size} - #{compressed_size} - #{compressed_ratio}")
logger.debug("JPEG family compressed, compressed: #{ '%.2f' % (compressed_ratio * 100) }%")
end
def compress_with_optpng(file)
current_size = File.size(file.path)
Paperclip.run("optipng", "-o7 --strip=all #{file.path}")
compressed_size = File.size(file.path)
compressed_ratio = (current_size - compressed_size) / current_size.to_f
logger.debug("#{current_size} - #{compressed_size} - #{compressed_ratio}")
logger.debug("PNG family compressed, compressed: #{ '%.2f' % (compressed_ratio * 100) }%")
end
end
There are also some gems to optimize paperclip images:
paperclip-optimizer
paperclip-compressor
Might be a performance compromise but I got images better compressed with FFMPEG or AVCONV.
sudo apt-get install ffmpeg
= 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
I got 1.7MB image compressed to 302.9KB!!!
Related
I currently have an application using paperclip that allows users to upload their creatives. This has worked flawless thus far when it comes to a user uploading an image file. We have since tested to upload a .mov file and I get this error:
Creative Paperclip::Errors::NotIdentifiedByImageMagickError
The weird thing, this error is only generated on Heroku. I can upload .mov files just fine on my local host.
My Current Gem Setup:
paperclip (3.4.1, 3.4.0)
paperclip-aws (1.6.7, 1.6.6)
paperclip-ffmpeg (0.10.2)
cocaine (0.5.1, 0.4.2)
Event.rb
has_attached_file :creative,
:processors => [:ffmpeg],
:styles => {
:thumb => [:geometry => "250x150", :format => 'png'],
:custcreative => [:geometry => "275x75", :format => 'png'],
:creativepreview => ["275x195",:png]
},
:url => "***",
:path => "***",
:s3_domain_url => "***",
:storage => :s3,
:s3_credentials => Rails.root.join("config/s3.yml"),
:bucket => '***',
:s3_permissions => :public_read,
:s3_protocol => "http",
:convert_options => { :all => "-auto-orient" },
:encode => 'utf8'
Spending hours trying to figure out why this works locally but throwing error on Heroku.
I even tried removing the :style setting, but still did not work.
TIA
EDIT
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/MidPen20130413-2-1mzetus.mov[0]'
Well, here is the answer in case any other newbies like us come across the same problem. The issue is with the geometry method that is being used for image cropping. The way it is suggested in railscasts assumes the file is in a local system and that needs to be changed.
OLD METHOD:
def avatar_geometry(style = :original)
#geometry ||= {}
#geometry[style] ||= Paperclip::Geometry.from_file(avatar.path(style))
end
NEW METHOD
def avatar_geometry(style = :original)
#geometry ||= {}
avatar_path = (avatar.options[:storage] == :s3) ? avatar.url(style) : avatar.path(style)
#geometry[style] ||= Paperclip::Geometry.from_file(avatar_path)
end
In my invoice system, I want a backup function to download all invoices at once in one zip file.
This system is running on heroku - so it's only possible to save the pdfs temporary.
I've the rubyzip and wicked_pdf gem installed.
My current code in the controller:
def zip_all_bills
#bill = Bill.all
if #bill.count > 0
t = Tempfile.new("bill_tmp_#{Time.now}")
Zip::ZipOutputStream.open(t.path) do |z|
#bill.each do |bill|
#bills = bill
#customer = #bills.customer
#customer_name = #customer.name_company_id
t = WickedPdf.new.pdf_from_string(
render :template => '/bills/printing.html.erb',
:disposition => "attachment",
:margin => { :bottom => 23 },
:footer => { :html => { :template => 'pdf/footer.pdf.erb' } }
)
z.puts("invoice_#{bill.id}")
z.print IO.read(t.path)
end
end
send_file t.path, :type => "application/zip",
:disposition => "attachment",
:filename => "bills_backup"
t.close
end
respond_to do |format|
format.html { redirect_to bills_url }
end
end
This ends with the message
IOError in BillsController#zip_all_bills closed stream
I think what is wrong in your code is that you have t for your zip but you also also use it for individual pdfs. So I think when you try to use the tempfile for a pdf it is a problem because you are already using it for the zip.
But I don't think you need to use tempfiles at all (and I never actually got a tempfile solution working on Heroku)
Here is a controller method that works for me--also using wickedpdf and rubyzip on heroku. Note that I'm not using a Tempfile instead I'm just doing everything with StringIO (at least I think that's the underlying tech).
def dec_zip
require 'zip'
#grab some test records
#coverages = Coverage.all.limit(10)
stringio = Zip::OutputStream.write_buffer do |zio|
#coverages.each do |coverage|
#create and add a text file for this record
zio.put_next_entry("#{coverage.id}_test.txt")
zio.write "Hello #{coverage.agency_name}!"
#create and add a pdf file for this record
dec_pdf = render_to_string :pdf => "#{coverage.id}_dec.pdf", :template => 'coverages/dec_page', :locals => {coverage: coverage}, :layout => 'print'
zio.put_next_entry("#{coverage.id}_dec.pdf")
zio << dec_pdf
end
end
# This is needed because we are at the end of the stream and
# will send zero bytes otherwise
stringio.rewind
#just using variable assignment for clarity here
binary_data = stringio.sysread
send_data(binary_data, :type => 'application/zip', :filename => "test_dec_page.zip")
end
I am trying to crop image using mini magick gem with carrierwave. I am facing following issue when i am creating user.
Errno::ENOENT in UsersController#create
No such file or directory - identify -ping /tmp/mini_magick20120919-5600-ai31ph.jpg
My Code :
model/user.rb
class User < ActiveRecord::Base
attr_accessor :crop_x, :crop_y, :crop_h, :crop_w
attr_accessible :username,:profile
after_update :reprocess_profile, :if => :cropping?
#field :username
mount_uploader :profile, ProfileUploader
def cropping?
!crop_x.blank? and !crop_y.blank? and !crop_h.blank? and !crop_w.blank?
end
def profile_geometry
#img = MiniMagick::Image.open(self.profile.large.path)
##geometry = {:width => img[:width], :height => img[:height] }
end
private
def reprocess_profile
#puts self.profile.large.path
#img = MiniMagick::Image.open(self.profile.large.path)
#crop_params = "#{crop_w}x#{crop_h}+#{crop_x}+#{crop_y}"
#img.crop(crop_params)
#img.write(self.profile.path)
#profile.recreate_versions!
end
end
uploader/profile_uploader.rb
# encoding: utf-8
class ProfileUploader < CarrierWave::Uploader::Base
# Include RMagick or ImageScience support:
#include CarrierWave::RMagick
# include CarrierWave::ImageScience
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(jpg jpeg gif png)
end
version :large do
end
version :thumb do
process :resize_to_fill => [100, 100]
end
end
What would be the problem ? please suggest any solution.
Need to install imagemagick.
In ubuntu:
sudo apt-get install imagemagick
I am using Rails 3 and carrierwave gem.
My problem is the next:
I am coding a wizard form with multiple models and relations. So My application has products which has many images through Image model (Its has been mounted with carrierwave).
Because the form has many steps I want save temporaly the images with cache option in CarrierWave. then i last step i want recover the images and load my product. So I create a model called TempData which save temporaly data (In this case cache_name), a type_identifier and a mark can_be_deleted.
For example these are my models:
Product Model:
class Product < ActiveRecord::Base
has_many :images, :as => :imageable, :dependent => :destroy
def save_images_in_temporal_folder(images_attributes)
begin
uploader = ImageUploader.new
images_attributes.to_hash.each do |image|
uploader.cache!(image[1]['filename'])
TempData.create(:data => uploader.cache_name, :can_deleted => false, :type_name => TempDataType.product_image_upload_to_words)
end
rescue
end
end
def load_images_from_temporal_folder
begin
uploader = ImageUploader.new
tmp_images = []
TempData.find_all_by_can_deleted_and_type_name(false, TempDataType.product_image_upload_to_words).map(&:data).each do |cache_name|
tmp_images << uploader.retrieve_from_cache!(cache_name)
end
tmp_images
rescue
end
end
end
Image model
class Image < ActiveRecord::Base
attr_accessible :imageable_type, :imageable_id, :filename
belongs_to :imageable, :polymorphic => true
mount_uploader :filename, ImageUploader
#validates :filename, :presence => true
end
My ImageUploader:
class ImageUploader < CarrierWave::Uploader::Base
configure do |config|
config.remove_previously_stored_files_after_update = false
end
include CarrierWave::RMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def self.store_dir_for_file(model, mounted, filename, version = "")
filename = "#{version}_#{filename}" unless version.blank?
"uploads/#{model.class.to_s.underscore}/#{mounted}/#{model.id}/#{filename}"
end
process :resize_to_limit => [200, 300]
version :thumb do
process :resize_to_limit => [50, 50]
end
version :big do
process :resize_to_limit => [400, 400]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
So How i save/cache the images with model and mount information?. How retrieve the cache and load my product with images in cache?
Thanks in advance. Sorry for my english and if you need more information comment this post.
View the next link:
CarrierWave with ActiveResource
Also I create a model which storage temporal data. Here I save the cache_name of carrierwave. When i save the product mark these register as can_be_deleted. Then I write a task which delete these registers and clean Carrierwave cache files.
I used rmagick in Rails to convert images I upload from one file type to JPEG. I can make it call from the new image now; I did:
image = Magick::ImageList.new 'public/system/photos/' + #picture.id.to_s +
'/original/' + #picture.photo_file_name
image.write 'public/system/photos/' + #picture.id.to_s + '/original/' +
#picture.photo_file_name.sub(/\.\w*/, '.jpg')
#picture.photo_file_name = #picture.photo_file_name.sub /\.\w*/,'.jpg'
Now I have created two files, how should delete the original file, or overwrite the original file rather than create a new one like I am now?
Re: the discussion in the comments, here's an example from the Paperclip docs:
class User < ActiveRecord::Base
has_attached_file :photo,
:styles => {
:small => {
:geometry => '38x38#',
:quality => 40,
:format => 'JPG'
},
:medium => {
:geometry => '92x92#',
:quality => 50
}
end
Note the ":format => 'JPG'" line. As you can see it's trivial to tell Paperclip to convert the file to JPEG while it's going about the rest of the business, so if you're using Paperclip already you don't need to do a separate conversion step with rmagick directly.