如何用MiniMagick在Ruby on Rails中根据图像方向调整大小?

2026-04-11 16:421阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计342个文字,预计阅读时间需要2分钟。

如何用MiniMagick在Ruby on Rails中根据图像方向调整大小?

我想根据它们是风景图还是肖像素来处理不同的图像。这是我的图像上传器模型中的代码:

pythondef is_landscape(file): if @file.image==:MiniMagick::Image.open(file): Rails.logger.info from is_landscape?:

我想根据它们是风景还是肖像来处理不同的图像.

这是我的图像上传器模型中的代码:

def is_landscape? if @file image = ::MiniMagick::Image.open(file) Rails.logger.info "from in is_landscape? : #{image[:width] > image[:height]}" image[:width] >= image[:height] end end def is_portrait? Rails.logger.info "from in is_portrait? : #{image[:height] > image[:width]}" image[:height] > image[:width] end process :resize_to_fill => [667, 500], if: :is_landscape? process :resize_to_fill => [500, 667], if: :is_portrait? version :preview do process :resize_to_fill => [380,285] end version :thumb do process :resize_to_fill => [105,79], if: :is_landscape? process :resize_to_fill => [105, 158], if: :is_portrait? end

我收到了错误

如何用MiniMagick在Ruby on Rails中根据图像方向调整大小?

"ArgumentError (wrong number of arguments (1 for 0)): app/uploaders/image_path_uploader.rb:31:in `is_landscape?'"

我究竟做错了什么?

我不得不将新文件传递给is_landscape?和is_portrait?让它工作的方法:

def is_landscape?(new_file) image = ::MiniMagick::Image::read(File.binread(@file.file)) Rails.logger.info "from in is_landscape? : #{image[:width] > image[:height]}" image[:width] >= image[:height] end def is_portrait?(new_file) Rails.logger.info "from in is_portrait? : #{ !is_landscape?(new_file)}" !is_landscape?(new_file) end process :resize_to_fill => [667, 500], if: :is_landscape? process :resize_to_fill => [500, 667], if: :is_portrait? version :preview do process :resize_to_fill => [380,285] end version :thumb do process :resize_to_fill => [105,79], if: :is_landscape? process :resize_to_fill => [105, 158], if: :is_portrait? end

本文共计342个文字,预计阅读时间需要2分钟。

如何用MiniMagick在Ruby on Rails中根据图像方向调整大小?

我想根据它们是风景图还是肖像素来处理不同的图像。这是我的图像上传器模型中的代码:

pythondef is_landscape(file): if @file.image==:MiniMagick::Image.open(file): Rails.logger.info from is_landscape?:

我想根据它们是风景还是肖像来处理不同的图像.

这是我的图像上传器模型中的代码:

def is_landscape? if @file image = ::MiniMagick::Image.open(file) Rails.logger.info "from in is_landscape? : #{image[:width] > image[:height]}" image[:width] >= image[:height] end end def is_portrait? Rails.logger.info "from in is_portrait? : #{image[:height] > image[:width]}" image[:height] > image[:width] end process :resize_to_fill => [667, 500], if: :is_landscape? process :resize_to_fill => [500, 667], if: :is_portrait? version :preview do process :resize_to_fill => [380,285] end version :thumb do process :resize_to_fill => [105,79], if: :is_landscape? process :resize_to_fill => [105, 158], if: :is_portrait? end

我收到了错误

如何用MiniMagick在Ruby on Rails中根据图像方向调整大小?

"ArgumentError (wrong number of arguments (1 for 0)): app/uploaders/image_path_uploader.rb:31:in `is_landscape?'"

我究竟做错了什么?

我不得不将新文件传递给is_landscape?和is_portrait?让它工作的方法:

def is_landscape?(new_file) image = ::MiniMagick::Image::read(File.binread(@file.file)) Rails.logger.info "from in is_landscape? : #{image[:width] > image[:height]}" image[:width] >= image[:height] end def is_portrait?(new_file) Rails.logger.info "from in is_portrait? : #{ !is_landscape?(new_file)}" !is_landscape?(new_file) end process :resize_to_fill => [667, 500], if: :is_landscape? process :resize_to_fill => [500, 667], if: :is_portrait? version :preview do process :resize_to_fill => [380,285] end version :thumb do process :resize_to_fill => [105,79], if: :is_landscape? process :resize_to_fill => [105, 158], if: :is_portrait? end