what I want to do is to save a website url with a full-size snapshot via IMGKit. In one of the views I also want to have a thumbnail version of the snapshot. I'm using carrierwave in order to associate the snapshot with the object an MiniMagick to manipulate it, the problem is that it generates the 'thumbnail' image but doesn't resize it so as a result, I have two full-size snapshots, one of then with 'thumb' as prefix.
I have this model in rails
class Webpage
mount_uploader :snapshot, SnapshotUploader
field :url, type: String
field :title, type: String
after_create :get_snapshot
private
def get_snapshot
file = Tempfile.new(["#{id}#{title}".downcase, '.jpg'], 'tmp', :encoding => 'ascii-8bit')
image = IMGKit.new(url, quality: 90).to_jpg
file.write(image)
file.flush
self.snapshot= file
self.save
file.unlink
end
end
And I have this in the Uploader in order to create the thumbnail version:
class SnapshotUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
version :thumb do
process resize_to_fill: [180, 180]
end
end
Using the console I tried MiniMagick for resizing an image and it works fine son I don't know what is happening.
I'm not sure if I'm doing it right so any help would be appreciated. Thanks.
OK, I'm stupid. I had an initializer with
config.enable_processing = false
so it will never process the image. Just setting it to true or deleting the line solved my problem.
Related
I have a user module and I have generated paperclip attachment: profile_pic
user.rb:
has_attached_file :profile_pic,
style: { :medium => "300x300>", thumb: "100x100>" },
default_url: "/images/:style/missing.png"
controller:
image_base = params[:manager][:profile_pic]
if image_base != nil
image = Paperclip.io_adapters.for(image_base)
image.original_filename = params[:manager][:file_name]
current_user.profile_pic = image
current_user.errors.delete(:profile_pic)
current_user.save
end
config/initializers/paperclip.rb:
Paperclip::DataUriAdapter.register
It's not showing any errors but If I am trying to display the image, it gives me following error:
ActionController::RoutingError (No route matches [GET] "/system/managers/profile_pics/000/000/008/original/icon_new.png"):
When I am trying in console like:
user.profile_pic.display
/system/managers/profile_pics/000/000/008/original/icon_new.png?
1556082410 => nil
Picture was saved in public folder
The default folder is the following, so Paperclip uploaded to the right place:
:rails_root/public/system/:class/:attachment/:id_partition/:style/:filename
But it looks like you are having troubles accessing the right location. Have you tried user.profile_pic.url? I tried on my working example, both .display and .url are the same but I use an AWS S3 bucket.
user.profile_pic.url should be where to find the file on the web.
user.profile_pic.path should be where to find the file on the file system.
The problem is with production mode and Solved this by adding the following line in production.rb
config.public_file_server.enabled = true
I use CarrierWave to generate versions (thumbnails with different sizes) and also to add a watermark on each versions.
I have currently manage to apply the watermark for each thumbnails but I would like to add it on the original image to.
Here is what I tried:
def watermark
manipulate! do |img|
watermark = Magick::Image.read(Rails.root.join('app/assets/images/watermark_512.png')).first
img = img.composite(watermark, Magick::CenterGravity, Magick::OverCompositeOp)
end
end
version :original do
process :watermark
end
version :thumb_512 do
process :resize_to_fit => [512, 512]
process :watermark
end
version :thumb_256 do
process :resize_to_fit => [256, 256]
process :watermark
end
But this does not work. However I tried to simply add
process :watermark
outside any "version" block but all it does is adding twice the watermark on my thumbnails.
You can use store callbacks provided by CarrierWave in your uploader class like this
class SomeUploader < CarrierWave::Uploader::Base
# Your code ...
before :store, :watermark_method
def watermark_method(*args)
# watermark it!
end
end
I'm trying to upload to amazon s3 an existing image on facebook or twitter from an user that has just signed up in my application, but some validation don't let me save the user object, throws: Image is invalid. I thought that was for my extension_white_list but I removed it and it's keeping saying Image is invalid.
This it's not an error, it's just a message from a validation on carrierwave I think, even if the image url string is correct.
AvatarUploader
# encoding: utf-8
class AvatarUploader < CarrierWave::Uploader::Base
include CarrierWaveDirect::Uploader
include CarrierWave::RMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
include CarrierWave::MimeTypes
process :set_content_type
def store_dir
"avatar/#{model.id}"
end
version :thumb do
process resize_to_fill: [50, 50]
end
# def extension_white_list
# %w(jpg jpeg gif png bmp)
# end
end
Creating user:
...
new_user = User.create( :name => auth['info']['name'],
:email => User.email_from_auth(auth) )
auth_image_url = Authentication.larger_image(auth) # a string of user image url from social network authentication data. ie: http://a0.twimg.com/profile_images/1255111511/skyline.jpg
unless auth_image_url.blank?
new_user.remote_image_url = auth_image_url
new_user.save
end
...
Fixed! The error has nothing to do with carrierwave, just the fact that the object does not exist on database in the moment where the image is upload, first I save the new user and then:
after_create :upload_image_from_auth
def upload_image_from_auth
auth = self.authentications.first
unless auth.nil?
self.remote_image_url = auth.larger_image
self.save
end
end
I'm using rails-translate-routes gem to translate "front" routes only.
I'm using carrierwave to upload some files in my admin. Here's an uploader:
class CheatsheetUploader < CarrierWave::Uploader::Base
[...]
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
[...]
end
As you see, the path is using the name of the model and the name of the field.
When I try to get the file:
link_to "my file", download.cheatsheet.url
The path is the default one:
http://localhost:3000/uploads/download/cheatsheet/1/a_really_nice_file.pdf
And Rails give me a nice Routing error:
No route matches [GET] "/uploads/download/cheatsheet/1/a_really_nice_file.pdf"
Any way to handle this?
You can pass keep_untranslated_routes: true in your call to ActionDispatch::Routing::Translator.translate_from_file.
For example:
ActionDispatch::Routing::Translator.translate_from_file(
'config/locales/routes.yml',
no_prefixes: true,
keep_untranslated_routes: true)
I have a CarrierWave::Uploader that produces three version of the uploaded image.
# Process files as they are uploaded:
process :resize_to_fit => [400, 400]
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fit => [60, 60]
end
version :small do
process :resize_to_fit => [24, 24]
end
And in my tests I try to verify the dimensions of the generated images
require 'spec_helper'
require 'carrierwave/test/matchers'
describe 'manufacturer logo uploader' do
include CarrierWave::Test::Matchers
before(:each) do
image_path = Rails.root.join('test/fixtures/images', 'avatar100.gif').to_s
#manufacturer = Factory.create(:manufacturer, :page_status => 1)
#manufacturer.logo_image = File.open(image_path)
#manufacturer.save!
end
context "manufacturer logo dimensions" do
it "should have three versions" do
#manufacturer.logo_image.should have_dimensions(400,400)
#manufacturer.logo_image.thumb.should have_dimensions(60,60)
#manufacturer.logo_image.small.should have_dimensions(24,24)
end
end
end
but this test depends on the actual image and resize_to_fit will not necessarily resize it to the specified dimensions. Any ideas on how to test this using stubs?
Here's my solution, which actually processes an image. This is slower than stubs, but verifies the actual resize (as long as the input image is larger than the target size).
describe 'images' do
include CarrierWave::Test::Matchers
before do
MyUploader.enable_processing = true
end
it 'are resized' do
path = Rails.root.join *%w[ spec data sample.png ]
my_model = FactoryGirl.create :my_model, image: path.open
my_model.artwork.small.should be_no_larger_than(300, 400)
end
after do
MyUploader.enable_processing = false
end
end
long shot but can you try to add this
before do
DocumentUploader.enable_processing = true
end
because processing (current version and other versions) could be turned off by default for performance reason
had similar problem related to process set_file_name_to_model that was doing something setting "file_name" on model attribute
http://ruby-on-rails-eq8.blogspot.co.uk/2015/03/carrierwave-uploader-not-triggering.html