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'
# more procesors
process :processed => :jpg
end
version :png do
process :convert => 'png'
# more processors
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!
So I'm not sure why this is the case, but it turns out that the problem is in the # more processors
part of the png version which I hid above. Omitting them from the version leads to a genuine PNG file for any input type. These processors call manipulate!
and do some stuff. The fix was instead to call manipulate!(:format => 'png')
instead. Yay.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With