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格式的技巧:
类MyImageUploaderapp/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模块WebPC转换器
# ...
def build_webp_full_文件名(文件名、版本名)
如果filename.split('.')。last=='webp',则返回“#{version_name}!#{filename}”
“#{version_name}#{filename}.webp”
结束
结束
从现在起,我可以将其用于需要转换为webp的版本:
类MyImageUploadermodule 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