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
Related
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'm using Rails 3 with carrierwave gem.
I managed to removed the avatar image from my user model like this:
#user.remove_photo!
and it works perfectly. However, I want to set the picture url for that user back to the default_url (which is the image that every user has until they upload one).
Any ideas how?
--
Image display code:
<%= image_tag(#user.photo.send(:layout).url, :alt => #user.full_name, :class => 'photo large') %>
default_url code:
def default_url
"default_photo.jpg"
end
From the docs:
https://github.com/carrierwaveuploader/carrierwave
In many cases, especially when working with images, it might be a good
idea to provide a default url, a fallback in case no file has been
uploaded. You can do this easily by overriding the default_url method
in your uploader:
class MyUploader < CarrierWave::Uploader::Base
def default_url
"/images/fallback/" + [version_name, "default.png"].compact.join('_')
end
end
Or if you are using the Rails asset pipeline:
class MyUploader < CarrierWave::Uploader::Base
def default_url
ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
end
end
You need to force update the field in the database to null, after that carrierwave will use the default_url, you can do this by calling Model.where(<CONDITIONS>).update_all(photo: nil) or instance.update_column(photo: nil)
I'm using Carrierwave to upload images into my app and store it on S3. That part is working fine, my issue is when I create a thumbnail of the picture which it does it fine but is storing the thumbnail in Heroku instead of S3.
How can I specify to store the thumb on my S3 bucket?
Here is my code:
class FileUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :fog
include CarrierWave::MimeTypes
process :set_content_type
def extension_white_list
%w(jpg jpeg gif png pdf)
end
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :resize_to_limit => [100, 100]
end
View:
<%= image_tag doctor.doctor_img_url(:thumb).to_s if doctor.doctor_img? %>
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.