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
Related
I want to use specific version of image by their width and height. So I followed How-to:-Get-image-dimensions this wiki, but got undefined method "width=".
My uploader looks like,
class S3uploaderUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
process :store_dimensions
# Choose what kind of storage to use for this uploader:
# storage :file
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :detail do
process :quality => 90
process :store_dimensions
end
version :mainVertical do
process :quality => 80
process :store_dimensions
process :resize_to_fit => [240, 180]
end
version :mainHorizontal do
process :quality => 80
process :store_dimensions
process :resize_to_fit => [240, 320]
end
private
def store_dimensions
if file && model
model.width, model.height = ::MiniMagick::Image.open(file.file)[:dimensions]
end
end
end
What am I missing? Any suggestions? Thanks.
I got an idea from this post. conditional-image-resizing-with-carrierwave. It works well.
And I changed my code like this.
class S3uploaderUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :detail do
process :quality => 90
end
version :main, :if => :image? do
process :quality => 90
process :resize_to_fit => [240, 180] ,:if => :horizontal?
process :resize_to_fit => [240, 320] ,:if => :vertical?
end
def image?(new_file)
self.file.content_type.include? 'image'
end
def horizontal?(new_file)
image = MiniMagick::Image.open(self.file.file)
true if image[:height] < image[:width]
end
def vertical?(new_file)
image = MiniMagick::Image.open(self.file.file)
true if image[:height] > image[:width]
end
end
I have a carrierwave installed with mini-magick on a rails-3-2 project.
I am facing a problems creating versions for uploaded svg images.
My uploader code is as follows
class SVGUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
process resize_to_fit: [400, 400]
version :thumb do
resize_to_fit(140, 140)
end
def extension_white_list
[:svg]
end
def store_dir
#dir ||= if ENV['PARALLEL_TEST_GROUPS']
"system/uploads/#{ENV['TEST_ENV_NUMBER']}/#{Rails.env}/#{model.class.to_s.underscore}/#{model.name}"
else
"system/uploads/#{Rails.env}/#{model.class.to_s.underscore}/#{model.id.to_s}"
end
end
end
The problem is when ever I upload any svg image, it takes a very long time to convert. and when I try to display the image browsers don't render them.
Anybody faced this issue? Please help.
I saving without format svg to png
I solve this away:
class FileUploader < CarrierWave::Uploader::Base
include CarrierWave::MimeTypes
include CarrierWave::MiniMagick
process :set_content_type
version :super_thumb, :if => :is_picture? do
process :resize_to_fill => [50, 50]
end
protected
def is_picture?(picture)
return false if set_content_type(picture).include?('svg')
set_content_type(picture).include?('image')
end
end
Hi I am uploading the video from my rails app.
For this I am using carrierwave gem.
I am referring this tutorial for it.
But when I tried to upload the video from my computer by using Browse button it is not allowing me to browse.
When I checked in the firebug I have getting this type of error
"NetworkError: 404 Not Found - http://localhost:3000/videos/uploadify.swf?preventswfcaching=1393936089114"
I tried to solve it but I didn't get any solution for it.
Here is my
videos controller
class VideosController < ApplicationController
def index
end
def show
#video = Video.find(params[:id])
end
def new
#video = Video.new(video_params)
end
def create
#video = Video.new(video_params)
if #video.save
render :nothing => true
else
render :nothing => true, :status => 400
end
end
private
def video_params
params.require(:video).permit(:attachment, :zencoder_output_id, :processed, :video, :meta_info) if params[:gallery]
end
end
model - video.rb
class Video < ActiveRecord::Base
belongs_to :gallery
mount_uploader :video, VideoUploader
end
uploader file
class VideoUploader < CarrierWave::Uploader::Base
include CarrierWave::Video
storage :file
def store_dir
"videos/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
process encode_video: [:mp4, resolution: "200x200"]
def extension_white_list
%w(avi mov mkv mpg mpeg mp4 m4v flv)
end
end
I am using rails 4 and ruby 2.1.0
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!!!
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.