回形针水印处理器

时间:2017-09-02 15:36:38

标签: ruby-on-rails paperclip solidus

我在处理器中执行命令时遇到问题。相同的命令在控制台中工作。基本上,我需要在一个中使用两个命令 - 转换并与tile复合,以便在整个图像上重复水印。

我在控制台中运行的命令是:

run_string = "convert utah.jpg -resize 1200x1200 miff:- | composite -tile Watermark-Photo.png - utah2.jpg"=> "convert utah.jpg -resize 1200x1200 miff:- | composite -tile Watermark-Photo.png - utah2.jpg"
Paperclip.run run_string

结果很完美。但是,当我把它放入我的处理器时:

module Paperclip
  class Watermark < Processor
    def initialize(file, options = {}, attachment = nil)
      super
      geometry = options[:geometry]
      @file = file
      @crop = geometry[-1,1] == '#'
      @target_geometry = Geometry.parse geometry
      @current_geometry = Geometry.from_file @file
      @convert_options = options[:convert_options]
      @whiny = options[:whiny].nil? ? true : options[:whiny]
      @format = options[:format]
      @watermark_path = options[:watermark_path]
      @position = options[:position].nil? ? "SouthEast" : options[:position]
      @watermark_offset = options[:watermark_offset]
      @overlay = options[:overlay].nil? ? true : false
      @current_format = File.extname(@file.path)
      @basename = File.basename(@file.path, @current_format)
      Rails.logger.info "watermark initialized"
    end

     # Returns true if the +target_geometry+ is meant to crop.
    def crop?
      @crop
    end

    # Returns true if the image is meant to make use of additional convert options.
    def convert_options?
      not @convert_options.blank?
    end

    # Performs the conversion of the +file+ into a watermark. Returns the Tempfile
    # that contains the new image.

    def make
      Rails.logger.info "watermark make method"
      src = @file
      dst = Tempfile.new([@basename].compact.join("."))
      dst.binmode


      if @watermark_path.present?
        # params = ["convert"]
        # params += %W[#{fromfile}]
        # params += transformation_command
        # params += %W[| composite -tile #{@watermark_path} -]
        # params << "#{tofile(dst)}"
        run_string = "convert '#{fromfile}' #{transformation_command.join(' ')} | composite -tile '#{@watermark_path}' - '#{tofile(dst)}'"
      else
        # params = ["convert"]
        # params += ["'#{fromfile}'"]
        # params += transformation_command
        # params << "'#{tofile(dst)}'"
        run_string = "convert '#{fromfile}' #{transformation_command.join(' ')} '#{tofile(dst)}'"
      end

      Rails.logger.info 'params:' + run_string

      begin
        # Paperclip.run(params.join(' '))
        Paperclip.run(run_string)
      rescue ArgumentError, Cocaine::CommandLineError
        raise Paperclip::Error.new("There was an error processing the watermark for #{@basename}") if @whiny
      end

      dst
    end

    def transformation_command
      scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
      trans = %W[-resize #{scale}]
      trans += %W[-crop #{crop} +repage] if crop
      trans += %W[miff:- ]
      trans << @convert_options if @convert_options.present?
      trans
    end

    def fromfile
      File.expand_path(@file.path)
    end

    def tofile(destination)
      File.expand_path(destination.path)
    end
  end
end

我可以从日志中看到它执行命令:

Command :: convert '/tmp/7179f28e8260099624b8c2e7e8fe8fb420170902-1-1ud7pah.jpg' -resize 600x600> miff:- | composite -tile '/storycraftstock/app/assets/images/Watermark-Photo.png' - '/tmp/7179f28e8260099624b8c2e7e8fe8fb420170902-1-1ud7pah20170902-1-1kbmamu'

给出了这个非特定错误:

exception while processing Spree::Image ID 11:
 There was an error processing the watermark for 32a2a05e7393f7ddc78f0134bcf32e4a20170902-1-19oyq1j

正如你所看到的,我在代码中评论了很多东西只是为了让它运行,但仍然没有运气。我有什么理想的错误吗?

0 个答案:

没有答案