【问题标题】:Limiting image dimensions based on aspect ratio within Paperclip and ImageMagick根据 Paperclip 和 ImageMagick 中的纵横比限制图像尺寸
【发布时间】:2014-04-03 19:24:00
【问题描述】:

我的 rails 应用使用 Paperclip 和 ImageMagick 来处理上传的照片。

我现在是这样设置的

as_attached_file :photo, :styles => { :original => "1500x1500>", :thumb => "400x400>#", :large => "1080x1080>" }, :convert_options => { :thumb => '-quality 60', :large => '-quality 60'},  :default_url => "/missing.png"

例如,如果有人上传尺寸为 1000x100(10:1 纵横比)的图像,我想限制纵横比(在 :large 和 :original 上),以便在纵横比太大时裁剪图像极端。

即:如果比例超过 4:1 或 1:4 则裁剪

【问题讨论】:

    标签: ruby-on-rails image-processing imagemagick paperclip


    【解决方案1】:

    最好的方法是实现一个自定义处理器。这样您就可以实现您的逻辑并决定何时以您想要的方式更改图像。

    查看自定义处理器的示例实现。就我而言,我需要在图像上应用水印。

    lib/paperclip_processors/watermark.rb

    module Paperclip
      class Watermark < Thumbnail
    
        attr_accessor :format, :watermark_path, :watermark_gravity, :watermark_dissolve
    
        def initialize file, options = {}, attachment = nil
          super
          @file               = file
          @format             = options[:format]
          @watermark_path     = options[:watermark_path]
          @watermark_gravity  = options[:watermark_gravity].nil? ? "center" : options[:watermark_gravity]
          @watermark_dissolve = options[:watermark_dissolve].nil? ? 40 : options[:watermark_dissolve]
    
          @current_format     = File.extname(@file.path)
          @basename           = File.basename(@file.path, @current_format)
        end
    
        def make
          return @file unless watermark_path
    
          dst = Tempfile.new([@basename, @format].compact.join("."))
          dst.binmode
    
          command = "composite"
          params = "-gravity #{@watermark_gravity} -dissolve #{@watermark_dissolve} #{watermark_path} #{fromfile} #{tofile(dst)}"
    
          begin
            success = Paperclip.run(command, params)
          rescue PaperclipCommandLineError
            raise PaperclipError, "There was an error processing the watermark for #{@basename}"
          end
    
          dst
        end
    
        def fromfile
          "\"#{ File.expand_path(@file.path) }[0]\""
        end
    
        def tofile(destination)
          "\"#{ File.expand_path(destination.path) }[0]\""
        end
    
      end
    end
    

    models/image.rb

    has_attached_file :file,
    processors: [:thumbnail, :watermark],
    styles: {
    layout:    "100%",
    preview:   {geometry: "900x900>", watermark_path: "#{Rails.root}/app/assets/images/watermarks/watermark_200.png"},
    thumbnail: "300x300>",
    miniature: "150x150>"
    },
    convert_options: {
    layout:    "-units PixelsPerInch -density 100",
    preview:   "-units PixelsPerInch -density 72",
    thumbnail: "-units PixelsPerInch -density 72",
    miniature: "-units PixelsPerInch -density 72"
    }
    

    您可以参考自定义处理器的文档:

    https://github.com/thoughtbot/paperclip#custom-attachment-processors

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 2013-06-13
      • 2016-08-06
      • 2019-04-12
      • 1970-01-01
      • 2014-09-26
      • 2015-04-23
      相关资源
      最近更新 更多