问题描述
我需要有条件地创建不同版本的上传图片。我知道Carrierwave支持这个功能。但我的要求有点棘手。
I need to create different versions of uploaded image conditionally. I know Carrierwave supports this feature. But my requirements are a bit tricky.
对于每个上传的图像,我需要创建2个版本,需要根据条件缩放原始图像。
For each uploaded image I need to create 2 versions and need to scale the original image based on conditions.
下面的代码可以让您更好地了解我要做的事情:
Below code will give you better idea what I am trying to do:
version :leftright, :if => :image? do
process :resize_to_fill => [667*2, 778*2] ,:if => :is_retina_resolution?
process :resize_to_fill => [667, 778] ,:if => !:is_retina_resolution?
end
version :updown, :if => :image? do
process :resize_to_fill => [1024*2, 487*2] ,:if => :is_retina_resolution?
process :resize_to_fill => [1024, 487] ,:if => !:is_retina_resolution?
end
#resize the original image
process :resize_to_fill => [1024*2, 768*2] ,:if => :is_retina_resolution?
process :resize_to_fill => [1024, 768] ,:if => !:is_retina_resolution?
def is_retina_resolution?(new_file)
image = MiniMagick::Image.open(new_file.file)
true if image[:height] >= 1536 and image[:width] >= 2048
end
显然这不起作用。 Carrierwave抛出此错误:
Apparently this is not working. Carrierwave throws this error:
Errno :: ENOENT - 没有这样的文件或目录 - #< ActionDispatch :: Http :: UploadedFile:0xe41d508>
我尝试了另一种变体:
version :leftright, :if => :image? do
if :is_retina_resolution?
process :resize_to_fill => [667*2, 778*2]
else
process :resize_to_fill => [667, 778]
end
end
version :updown, :if => :image? do
if :is_retina_resolution?
process :resize_to_fill => [1024*2, 487*2]
else
process :resize_to_fill => [1024, 487]
end
end
def is_retina_resolution?(new_file)
image = MiniMagick::Image.open(new_file)
true if image[:height] >= 1536 and image[:width] >= 2048
end
这不会引发任何错误。但它始终以视网膜大小生成图像
(第一个条件)
This doesn't throw any error. But it always generates the image in retina size
(1st condition)
所以我尝试了另外一个变体:
So I tried one more variation:
version :leftright, :if => :image? && :is_retina_resolution do
process :resize_to_fill => [667*2, 778*2]
end
version :leftright, :if => :image? && !:is_retina_resolution do
process :resize_to_fill => [667, 778]
end
version :updown, :if => :image? && :is_retina_resolution do
process :resize_to_fill => [1024*2, 487*2]
end
version :updown, :if => :image? && !:is_retina_resolution do
process :resize_to_fill => [1024, 487]
end
这不会引发任何错误,也不会创建任何版本。
This doesnt throw any error and also not created any version.
有人可以帮助我吗?
更新:
根据@DMKE的建议,我做了这个改动,现在工作正常
Based on the suggestion by @DMKE, I made this changes, now it works fine
version :leftright, :if => :image? do
process :resize_to_fill => [667*2, 778*2] ,:if => :is_retina_resolution?
process :resize_to_fill => [667, 778] ,:if => :is_not_retina_resolution?
end
version :updown, :if => :image? do
process :resize_to_fill => [1024*2, 487*2] ,:if => :is_retina_resolution?
process :resize_to_fill => [1024, 487] ,:if => :is_not_retina_resolution?
end
#resize the original image
process :resize_to_fill => [1024*2, 768*2] ,:if => :image_and_retina?
process :resize_to_fill => [1024, 768] ,:if => :image_and_not_retina?
process :if => :not_image?
def image_and_retina?(img)
is_img = image? img
return false unless is_img
return is_retina_resolution?(img)
end
def image_and_not_retina?(img)
is_img = image? img
return false unless is_img
return !is_retina_resolution?(img)
end
# returns true if image file
def image?(new_file)
self.file.content_type.include? 'image'
end
def not_image?(new_file)
!self.file.content_type.include? 'image'
end
def is_retina_resolution?(new_file)
image = MiniMagick::Image.open(self.file.file)
true if image[:height] >= 1536 and image[:width] >= 2048
end
def is_not_retina_resolution?(new_file)
image = MiniMagick::Image.open(self.file.file)
true if image[:height] < 1536 and image[:width] < 2048
end
推荐答案
虽然不是语法错误,此代码有一个语义缺陷:
Although not a syntax error, this code has a semantical flaw:
version :updown, :if => :image? && !:is_retina_resolution do
# ...
end
此处,:形象? &安培;&安培; !:is_retina_resolution
始终评估为false(在IRb终端中尝试!:foo
),因此:updown
版本从不创建。同样的解释适用于进程foo:[sx,sy],如果:!:bar?
Here, :image? && !:is_retina_resolution
always evaluates to false (try !:foo
in an IRb terminal), thus the :updown
version is never created. The same explanation goes for process foo: [sx,sy], if: !:bar?
自CarrierWave不支持:除非
选项(据我所知),实现目标的唯一方法是 CarrierWave中的一些方法定义: :Uploader :: Base
子类:
Since CarrierWave does not support an :unless
option (as far as I can tell), the only way to achieve your goal are some method definitions in your CarrierWave::Uploader::Base
subclass:
process resize_to_fill: [667*2, 778*2], if: :image_and_retina?
process resize_to_fill: [667, 778], if: :image_and_not_retina?
def image_and_retina?(img)
image?(img) && is_retina_resolution(img)
end
def image_and_not_retina?(img)
image?(img) && !is_retina_resolution(img)
end
这篇关于使用Carrierwave调整条件图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!