Carrierwave: convert an uploaded PNG to JPG by replacing the original version

早过忘川 提交于 2020-03-25 18:47:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!