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
Related
My uploader allows images and pdf's
def extension_whitelist
%w(pdf jpg jpeg gif png)
end
I need to resize images and process quality. In order to do this, I need to check first if the file is an image.
I've added
version :large, :if => :image? do
process :quality => 70
process :resize_to_limit => [1400, 1200]
end
protected
def image?(new_file)
new_file.content_type.include? 'image'
end
However, I don't want a "version". I have the file link on the front-end and the link points to an optimized and not resized version.
How can I check file type without creating a new version of the file?
I've tried
if image?
process :quality => 70
process :resize_to_limit => [1400, 1200]
end
but I get No Method Error
I had to do this instead:
process :quality => 70, if: :image?
process :resize_to_limit => [1400, 1200], if: :image?
My default image is working with carrierwave, except I can't get a thumb version of the default. #profile.photo and #profile.photo.thumb both return the same default_profile.png. How do I get this to work
Also, when I do upload a photo, #profile.photo.thumb does correctly return the resized version of the uploaded photo.
In /assets/images:
I have default_profile.png and thumb_default_profile.png, the latter is, of course, a scaled down version of the former. I also tried renaming the thumb file to default_profile_thumb.png
relevant portion of profile_photo_uploader.rb:
def default_url
"default_profile.png"
end
version :thumb do
process :resize_to_fill => [60, 60]
end
def default_url
"default_profile.png"
end
version :thumb do
process :resize_to_fill => [60, 60]
def default_url
"thumb_default_profile.png"
end
end
Nicer way todo:
def default_url(*args)
ActionController::Base.helpers.asset_path([version_name, "default_profile.png"].compact.join('_'))
end
I have ImageUploader class and i want to save my original image with original size of the image after saving the particular version. Help me to solve this
Uploader
class ImageUploader < IconBase
process :resize_to_fill => [490,68]
version :normal do
process resize_to_fill: [245,34]
def full_filename(for_file = model.logo.file)
"avatar1.png"
end
end
def filename
"avatar.png"
end
end
Your original size is not saved, because you have process :resize_to_fill => [490,68] in your uploader. To keep original size, you can put this into another version, so your main image will stay unproccessed, like this:
version :large do
process :resize_to_fill => [490,68]
end
Then you'll have:
uploader.url # original image
uploader.large.url # [490,68] version
uploader.normal.url # [245,34] version
I'm currently trying to get the first frame of a gif file, resize it and save it as a jpg file.
The conversion seems fine I think. But it doesn't save it with the right file extension. It still gets saved as .gif
So when I try to open it it says can't open image, doesn't seem to be a GIF file. Then I rename the extension myself and it works.
Here is my processing code:
version :gif_preview, :if => :is_gif? do
process :remove_animation
process :resize_to_fill => [555, 2000]
process :convert => 'jpg'
end
def remove_animation
manipulate! do |img, index|
index == 0 ? img : nil
end
end
There is actually another, cleaner way to achieve this; and it is even somewhat documented in the official wiki: How To: Move version name to end of filename, instead of front
Using this method your version code would look like this:
version :gif_preview, :if => :is_gif? do
process :remove_animation
process :resize_to_fill => [555, 2000]
process :convert => 'jpg'
def full_filename(for_file)
super.chomp(File.extname(super)) + '.jpg'
end
end
def remove_animation
manipulate! do |img, index|
index == 0 ? img : nil
end
end
So ... I finally found a solution after hours of headaches why this didn't work. Turns out you have to touch/create a file first in order to make this work. I also switched from RMagick to Mini Magick. Not for a particular reason just tried it out if it would work with MiniMagick but I still had the same problem. Here is my new process code with Mini Magick:
version :gif_preview, :if => :is_gif? do
process :gif_to_jpg_convert
end
def gif_to_jpg_convert
image = MiniMagick::Image.open(current_path)
image.collapse! #get first gif frame
image.format "jpg"
File.write("public/#{store_dir}/gif_preview.jpg", "") #"touch" file
image.write "public/#{store_dir}/gif_preview.jpg"
end
I just don't understand why there is really 0 documenation about this ...
I crop the big version based on the original version that is left uncropped, I then wan't to recreate the same crop on the small version. Is there a way to recreate it based on the version big instead of calculating the crop based on the difference in the dimensions?
version :big do
process :crop
process :resize_to_fill => [600, 400]
process :convert => 'jpg'
end`
version :thumb do
process :resize_to_fill => [100, 80]
process :convert => 'jpg'
end
I know you can nest version within version by doing
version :big do
#process...
version :thumb do
#process...
end
end
But I haven't use that feature, so I don't know is the subversion will have the parent processes applied.