问题
I have the following model:
class ScreenshotUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
convert :jpg
version :thumb do
process resize_to_fill: [50, 50]
end
def extension_whitelist
%w(jpg jpeg gif png)
end
version :print do
process border: ['black']
process quality: 80
end
end
The upload of the image happens via pasting an image from the clipboard via https://github.com/layerssss/paste.js and is saved as a base64 encoded string into a <textarea>
, then uploaded using the https://github.com/y9v/carrierwave-base64 gem:
class Finding < ApplicationRecord
mount_base64_uploader :screenshot, ScreenshotUploader
end
In the HTML form, it looks like this:
After uploading, the result is the following files:
screenshot.png
it's a PNG, not a JPG!thumb_screenshot.jpg
print_screenshot.jpg
But I need the original file to be also converted to JPG, as I need to save disk space. How can I achieve this?
回答1:
You can do it like it written on the carrier wave documentation
Just replace system("mogrify -resize '1200\>' #{file.file}")
with system("mogrify -format jpg #{file.file}")
and then remove original file.
来源:https://stackoverflow.com/questions/60622916/carrierwave-convert-an-uploaded-png-to-jpg-by-replacing-the-original-version