Paperclip watermark processor

260 views Asked by At

I'm having issues to execute command in my processor. The same command works in console. Basically, I need to use two commands in one - convert and composite with tile so that watermark is repeated all over the image.

The command I ran in console is this:

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

And the result was perfect. However, when I put it into my processor:

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

I can see from log that it executes the command:

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'

Gives this unspecific error:

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

As you can see I've commented a lot of things in the code just to make it run, but still no luck. Any ideal what am I doing wrong?

0

There are 0 answers