I am using CarrierWave to upload images. I am also using this functionality in ActiveAdmin.
I followed the RailsCast and everything is working up to the point where the RMagick gem is coming into play. Before resizing an image and just getting the image by calling:
"<%= image_tag #client.image_url.to_s %>"
When I go to resize an image, the image breaks. The code I have in my image_uploader.rb file is the same as Ryan Bates in where I am doing the following:
"version :thumb do
process :resize_to_limit => [50, 50]
end"
Then, when adding the :thumb method to my previous erb tag with the following:
"<%= image_tag #client.image_url(:thumb).to_s %>"
The images break. The interesting thing for me is that I have done this process multiple times, but this is the first time I have tried using CarrierWave and RMagick with Active Admin.
Related
I would like to use the RMagick Compression in Carrierwave before the image is uploaded to Cloudinary.
The local imagemagick tests showed that a 37MB File (Please don't ask why it has that size ;) ) was compressed to only 4,6 MB with an acceptable quality.
So now I would like to use the same functionality in my rails app with rmagick, but it seems like the preprocessing does not take place at all. The uploader uploads the original file with 37MB.
This is what I have at the moment:
class ImageUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave
include CarrierWave::RMagick
process :compress => Magick::JPEGCompression
def compress(compression_type)
manipulate! do |img|
img = img.compression(compression_type)
end
img
end
...
How can I achieve that the compression takes place and only the compressed file is uploaded to cloudinary?
You might want to try our incoming transformation.
For example, you can upload the image using upload widget and add the incoming transformation in the upload preset. or use the SDK. For example:
Cloudinary::Uploader.upload("sample.jpg",
:width => 2000, :height => 1000, :crop => :limit, :quality => 70)
I have a rails4 app that using carrierwave with S3 and cloudfront. I only have problem with the fallback image. When I'm using html response (<%= image_tag user.profile.avatar.url(:base_thumb), class: "profile-index-avatar" %>) with the helper everything works fine, but can't figure it out how to make it work with json response.
If I check out the html (built from json) in production on the root page the code is:
1st jbuilder: <img src="https://example.com/small_thumb_default.png">
2st jbuilder: <img src="https://example.com/assets/small_thumb_default.png">
None of these are working.
On the top of this if I go let's say to the users page then it tries to get pic like:
1st jbuilder: <img src="https://example.com/users/small_thumb_default.png">
2nd jbuilder: <img src="https://example.com/users/assets/small_thumb_default.png">.
What should I change?
jbuilder 1st version
json.array! #other_notifications do |notification|
..
json.profile_image_url notification.sender.profile.avatar.url(:small_thumb)
...
end
jbuilder 2nd version
json.array! #other_notifications do |notification|
..
if notification.sender.profile.avatar_url == "default.png"
json.profile_image_url "assets/small_thumb_default.png"
else
json.profile_image_url notification.sender.profile.avatar.url(:small_thumb)
end
...
end
uploader
process :resize_to_fit => [400, 400]
version :base_thumb do
process resize_to_fill: [85, 85]
end
version :small_thumb, :from_version => :base_thumb do
process :resize_to_fill => [40, 40]
end
def default_url
[version_name, "default.png"].compact.join('_')
end
Most likely, the file in question is inside app/assets/ folder, files inside that folder are being precompiled by default and a random digest is being added on the end of the filename. For the described behavior to work your small_thumb_default.png file should be placed inside public/ folder, so it doesn't get a digest appended on precompile. Either that or you should avoid using HTML, do it the Rails way:
# AVOID THIS
<img src="small_thumb_default.png"/>
#THIS IS PREFERABLE
<%= image_tag 'small_thumb_default.png' %>
This way you will get your view rendered with the precompiled filename.
I am using paperclip to upload user avatar.
Here is User model:
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "user.png", :path => "app/assets/images/:class/:attachment/:id/:basename_:style.:extension", :url => ":class/:attachment/:id/:basename_:style.:extension"
So image is saving in app/assets/images/user/avatar/:id/:basename_:style.:extension
But when I do
<%= image_tag #user.avatar.url %>
It shown as:
<img src="/images/users/avatars/15/99e88dc27c19d8c6163d9cd305f738be_original.jpg" alt="99e88dc27c19d8c6163d9cd305f738be original">
i.e. inserts "/images" instead of "/assets"
I double-checked, the avatar image exists in the assets/images/user/avatar/ folder
Although all other images in page are showing correctly using assets pipeline "/assets/logo-thebighashgohere.png"
NOTE: This works correctly if I manually insert image url as string
i.e.:
<%= image_tag "users/avatars/15/99e88dc27c19d8c6163d9cd305f738be_original.jpg" %>
It correctly shows as
<img src="/assets/users/avatars/15/99e88dc27c19d8c6163d9cd305f738be_original-thebighashgohere.jpg" alt="99e88dc27c19d8c6163d9cd305f738be original thebighashgohere">
I strongly recommend you to not save user-generated-content into the assets folder!
if your website is going into production mode, the assets are compiled and everything you throw in at runtime won't be catched.
stuff like that belongs into the /public directory (!)
to solve your problem
:url => ":class/:attachment/:id/:basename_:style.:extension
you tell paperclip how to generate your "url". with "path" you define where the files are stored internally, with url you control how to generate the routes. your rote is wrong, no asstes path in it.
but again - do not save those picutres into assets !
btw: i wonder if your solution is even possible in productionmode. the assetspipeline is generting files with a digest appending on it, while paperclip knows nothing about those digest it will always render a route without the digest-stamp. by that you can't call the images from the assets pipeline. so your whole concept won't work in production, but i might be wrong
Using Paperclip to attach avatars to User profiles for my rails application. I followed the instructions on the paperclip github to initialize and attach to my app.
I have an image in the public/images/medium/missing.png and for both cases (when I upload or when I fallback on the default) I get no image. I've checked my directory and there is an image where it says it is looking but does not grab it. Additionally when I have tried uploading images, I know the image is uploaded correctly because when calling the User in rails console shows all the information properly attached.
I am calling the image in my view like:
<%= image_tag(#blog.user.avatar.url(":medium"), :class => "image-circle avatar") %>
my Paperclip declaration in the User model looks like the following:
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#"}, :default_url => "/public/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
Really not sure what is going on. The route errors that appear when I inspect the improperly loaded image point directly to the image in my local server. And the fact that it can't grab either the missing or the uploaded file also has me at a loss. Any help would be super appreciated!!
And for good measure the output when I examine a user with an uploaded avatar:
avatar_file_name: "11390219_10206114805925816_6595348111261743639_n.j...", avatar_content_type: "image/jpeg", avatar_file_size: 101926, avatar_updated_at: "2015-07-10 18:51:44">
Thanks in advance!
EDIT
This is the URL that is providing the 404 error:
http://localhost:3000/images/medium/missing.png
while in my local directory it goes "root/public/images/medium/missing.png"
not sure how its not grabbing it, unless I am just missing something really obvious somewhere. (i tried hard routing the public in there as well, but to no avail).
EDIT
There is the possibility that you're simply not serving the static assets, add:
config.serve_static_assets = true
to your development.rb
ORIGINAL POST
In you application.rb ( or an environment specific file ), you should have a config.paperclip_defaults = { ... }, here is the link in the docs: https://github.com/thoughtbot/paperclip#defaults
Here is an example one, using fog:
config.paperclip_defaults = {
:storage => :fog,
:fog_credentials => {
:provider => "Local",
:local_root => "#{Rails.root}/public"
},
:fog_directory => "",
:fog_host => "localhost:3000"
}
Do you have something like that in your application? I just tested on an app of mine, and I was able to upload an image, but not to see any without the paperclip_defaults hash. Also, don't forget to restart your app after you update the config files. I hope this helps!
Have you tried killing the quotes around the image style? Around :medium?
<%= image_tag(#blog.user.avatar.url(:medium), :class => "image-circle avatar") %>
As show here in the Paperclip Docs:
https://github.com/thoughtbot/paperclip#show-view
Same problem, solved it by reinstalling imagemagic with brew
Details: In case you're imagemagic, try to update the imagemagic and if it asks you to link it,
Try with this:
brew link --overwrite imagemagick
It worked for me. Hope its helpful
I am attempting to optimize images when resizing them with Carrierwave but haven't had any luck getting this to work. Carrierwave is generating the different sized versions, but my custom optimize and the convert processes aren't running. I've tried calling the different processes in different places, but nothing seems to work.
Any ideas on what I might be doing wrong? Could it be the same issue here: CarrierWave RMagick - How do I cause manipulate! to be called?
class FooUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
# Attempt #1
process :convert => 'jpg'
process :optimize
version :meow do
process :resize_to_fill => [700, 400]
end
# Attempt #2
version :meow do
process :convert => 'jpg', :optimize => nil, :resize_to_fill => [700, 400]
end
# Attempt #3
version :meow do
process :resize_to_limit => [700, 400]
process :optimize
process :convert => 'jpg'
end
# Attempt #4
# Tried switching order around since they are being resized but not converted
version :meow do
process :convert => 'jpg'
process :optimize
process :resize_to_limit => [700, 400]
end
# Optimize method used by all versions
def optimize
manipulate! do |img|
img.strip
img.combine_options do |c|
c.quality "96"
c.depth "24"
c.interlace "plane"
#c.interlace "Plane" # Tried both cases, seen examples of both
end
img
end
end
end
Well that not entirely possible they way you want if you are using carrierwave mini-magick or rmagick helper method
Solution 1)
if you get rid of carrierwave helper then perhaps it can work
do something like this
process :custom_processing
def custom_processing
// You have to write you own code for and not CarrierWave helper method to achieve this
// write your own code to convert to jpg
// write your code to optimize
// write your code to resize from the converted n optimize image
end
this way you do all your processing in one shot ,having said but you have to dig in mini_magick/rmagick documentation to know how what method to call for each as you cant rely on carrierwave helper (since they are registered/called via process method and one cannot call call/invoke process inside process )
so something like this you can't achieve
def custom_processing
process :convert => 'jpg'
process :optimize
process :resize_to_fit => [700, 400]
end
Solution 2)
With Carrierwave Helper but through multiple versions you can eventually achieve this
version :jpg do
process :convert => 'jpg'
end
version :optimize_jpg ,:from => :jpg do
process :optimize
end
version :meow,:from => :optimize_jpg do
process :resize_to_limit => [700, 400]
end
More reference of this can be found here
Now this approach take care of headache of Approach 1 digging to documentation(since we are using Carrierwave helper methods), but as you can see you have to create multiple versions to achieve this
Question : Why Multiple version ?
Well that how it Carrierwave work it create a uploader for each version and if from options isnt applied it consider the original images (uploaded images) as the source file(parent file) create the desired version