jnicklas/carrierwave — Issue #513: Possible to upload several formats of some image?
Author: tdp2110
Created: 2011-11-03 15:41:32+00:00
State: open

This is a repost of http://stackoverflow.com/questions/7997134/trouble-with-carrierwave-convert
I hope this isn't the wrong place to post such a question:

I'd like Carrierwave to produce a few versions of an image, one in png format, and one in jpeg format, but I can't seem to get it to work. I realize several similar questions have been asked both here and elsewhere, but I've failed to find a solution. Roughly, here's what I have:

class MyUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick

version :jpeg do
process :convert => 'jpg'
process :processed => :jpg
end

version :png do
process :convert => 'png'
process :processed => :png
end

def store_path(for_file = filename)
if version_name != :png
return "#{version_name}.jpg"
else
return "#{version_name}.png"
end
end

def filename
filename = if version_name != :png
super != nil ? super.split('.').first + '.jpg' : super
else
super != nil ? super.split('.').first + '.png' : super
end
end

end
So, regardless of the type of image format I pass in, this produces two files: one named jpg.jpg, and one named png.png. jpg.jpg is indeed always a JPEG file (as verified with Unix's file command), BUT png.png is only a true PNG formated file if and only if I originally pass in a png-formated file (passing in a jpeg results in a jpeg formatted file)!

Ideas?

This might not be useful, but I find the convert method in Carrierwave itself suspicious: convert(some_format) calls manipulate!(:format => some_format), which in turn calls (some_image_object).write("#{some_format}:#{current_path}"). Now if the original image passed in is bla.format (format = jpg, png, etc), current_path with have an extension of .format (tested via a monkey-patch on manipulate!). And the way image format conversion works in RMagick is by calling write with an argument having the desired extension. So why would the jpeg conversion above actually take place? And if so, why would the png conversion fail?

Also, if it's useful, I'm on Rails 3.0.9 and Carrierwave 0.5.6

Thanks!