Ruby on rails 如何在RubyonRails中将用户上传的图像转换为webp

Ruby on rails 如何在RubyonRails中将用户上传的图像转换为webp,ruby-on-rails,image-processing,imagemagick,carrierwave,webp,Ruby On Rails,Image Processing,Imagemagick,Carrierwave,Webp,我想将用户在webp中上传的图像转换为站点性能。我知道webp现在只支持两种浏览器,googlechrome和Opera 我正在使用carrierwave将图像上载到s3 在carrierwave中找不到任何将图像转换为webp的支持 image-magick具有库libwebp,可以将图像转换为webp rails中有这样的宝石吗 如何将图像转换为carrierwave并将其保存。 除了carrierwave之外的解决方案也可以使用。我有类似的任务,但没有上传到S3。 我使用gem编写自定义转

我想将用户在
webp
中上传的图像转换为站点性能。我知道
webp
现在只支持两种浏览器,
googlechrome
Opera

我正在使用
carrierwave
将图像上载到
s3

carrierwave
中找不到任何将图像转换为
webp
的支持

image-magick
具有库
libwebp
,可以将图像转换为
webp

rails中有这样的宝石吗

如何将图像转换为
carrierwave
并将其保存。
除了
carrierwave
之外的解决方案也可以使用。

我有类似的任务,但没有上传到S3。 我使用gem编写自定义转换器

这是帮助我的解决方案

首先,您需要安装gem的自述文件中指定的

之后,将gem添加到您的项目中:

gem'webp ffi'
如果您不需要存储原始图像,您可以将自定义方法添加到上载程序中,并使用CarrierWave的process方法调用它。以下是我将图像转换为webp格式的技巧:

类MyImageUploader 您可以将此方法移动到项目中的某个模块(确保其正确)。例如,我将此代码放入
app/services/web\u p\u converter.rb

模块WebPC转换器
def convert_to_webp(选项={})
webp_path=“#{path}.webp”
编码(路径,WebP_路径,选项)
@filename=webp_path.split('/').pop
@file=CarrierWave::SanitizedFile.new(
tempfile:webp_路径,
文件名:webp_路径,
内容类型:'image/webp'
)
结束
结束
现在,我可以在每个需要webp转换的上传程序中包含此模块:

类MyImageUploader 但是,如果您需要存储文件的原始版本并在uploader中创建一个版本,则需要使用以下方法:

类MyImageUploader 必须这样做,因为CarrierWave使用原始映像名构建到其他版本的路径

还可以将逻辑从#full_filename移动到方法中。例如,我将构建完整文件名的逻辑移到WebPConverter模块中,使其看起来像这样:

模块WebPC转换器
# ...
def build_webp_full_文件名(文件名、版本名)
如果filename.split('.')。last=='webp',则返回“#{version_name}!#{filename}”
“#{version_name}#{filename}.webp”
结束
结束
从现在起,我可以将其用于需要转换为webp的版本:

类MyImageUploader 签出gem,我使用它作为示例来创建解决此问题的方案(由于某些原因,此gem不适用于我)

还检查了我为演示工作解决方案而做的检查

module WebPConverter
  private

  def build_webp_full_filename(filename, version_name)
    return "#{version_name}_#{filename}" if filename.split('.').last == 'webp'

    "#{version_name}_#{filename}.webp"
  end
end
在图像上传器中

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  include CarrierWave::WebP::Converter
  include WebPConverter

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :thumb do
    process resize_to_fit: [50, 50]
  end
  
  version :img_quality do
    process resize_to_fit: [600, 600]
    process quality: 60
  end

  version :webp do
    process convert_to_webp: [{ quality: 60, method: 5 }]
    process resize_to_fit: [600, 600]
    def full_filename(file)
      build_webp_full_filename(file, version_name)
    end
  end

  def extension_whitelist
    %w[jpg jpeg gif png]
  end
end

也许RMagick at与webp代理库at在一起,但carrierwave中是否支持它?抱歉,我不知道Carrierwave,也不知道它是如何使用Imagemagick的。@WaqasAhad你用它取得了什么进展吗?@Chrisewards还没有……如果你发现update me,也请使用优秀的解决方案,但它不会删除图像。@cool\u php你是说原始图像吗?没有。webp图像。我必须用一个单独的回调方法来清理这个问题。
<%= image_tag set_browser(deals_first(@category).brand.logo) %>
 def set_browser(object)
    browser.chrome? || browser.opera? || browser.platform.android? ? 
    object.webp.url : object.quality.url
  end