【问题标题】:Carrierwave creating all versions of imagesCarrierwave 创建所有版本的图像
【发布时间】:2018-06-16 21:41:02
【问题描述】:

我安装了carrierwave 和minimagick,我尝试调整图像的大小,如果它是纵向的还是横向的,我遵循了carrierwave 文档,但是有些东西不起作用,它正在创建纵向和横向图像。我已经玩了一段时间了,我完全没有想法!任何想法将不胜感激。

这是我的代码:uploader.rb

class CoverUploader < CarrierWave::Uploader::Base
      include CarrierWave::MiniMagick

      storage :file
      # storage :fog

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

      version :landscape, if: :is_landscape?


        version :landscape do
        process resize_to_fit: [@land_height, 250]
      end

      version :portrait, if: :is_portrait?


        version :portrait do
          process resize_to_fit: [250, @port_width]
        end


      process :store_dimensions

      def extension_whitelist
        %w(jpg jpeg png)
      end


      private

      def store_dimensions
        if file && model
          model.width, model.height = ::MiniMagick::Image.open(file.file)[:dimensions]
        end
      end

      def is_landscape? picture
        image = MiniMagick::Image.open(picture.path)
        image[:width] > image[:height]
        width = image[:width]
        aspect = image[:width] / image[:height].to_f
        @height = aspect * width
      end

      def is_portrait? picture
        image = MiniMagick::Image.open(picture.path)
        image[:width] < image[:height]
        height = image[:height]
        aspect = image[:width] / image[:height].to_f
        @width = height / aspect
      end
    end

所以这是创建 3 个图像而不是 2 个。

提前致谢!

【问题讨论】:

    标签: ruby-on-rails ruby carrierwave image-resizing


    【解决方案1】:

    在这里进行疯狂的猜测:您正在覆盖 version 具有相似但不同的块,这些块省略了您的条件。把它清理干净。

    version :portrait, :if => :is_portrait? do
      process resize_to_fit: [250, @port_width]
    end
    
    version :landscape, :if => :is_landscape? do
      process resize_to_fit: [@land_height, 250]
    end
    

    【讨论】:

    • 嗨乔希,感谢您的回复。问题出在方法上,我应该在底部有“ image[:width] > image[:height] ”,因为它没有返回 true 或 false。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 2011-10-18
    • 2013-02-01
    • 2015-10-24
    • 1970-01-01
    • 2020-04-08
    • 2017-01-18
    相关资源
    最近更新 更多