Sha256: 09c1b9d6f62fc36b2ea64a49109d631c4ee4e21cdf82e028ded07f5ad86e8004

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'open3'

require 'letter_avatar/version'
require 'letter_avatar/configuration'
require 'letter_avatar/avatar'
require 'letter_avatar/avatar_helper'
require 'letter_avatar/colors'

module LetterAvatar
  extend LetterAvatar::Configuration

  class ExecutionError < StandardError; end

  def self.setup(&_block)
    yield(self)
  end

  def self.resize(from, to, width, height)
    execute(
      # NOTE: ORDER is important!
      %W[
        magick
        #{from}
        -background transparent
        -gravity center
        -thumbnail #{width}x#{height}^
        -extent #{width}x#{height}
        -unsharp 2x0.5+0.7+0
        -quality 98
        #{to}
      ].join(' ')
    )
    true
  rescue StandardError
    false
  end

  def self.generate(username, size)
    Avatar.generate(username, size)
  end

  def self.execute(cmd)
    cmd = cmd.join(' ') if cmd.is_a?(Array)
    cmd.tr!("'", '"') if Gem.win_platform?

    _stdout_str, err = Open3.capture3(cmd)
    if !err.nil? && !err.empty?
      raise ExecutionError.new("letter_avatar execution error (when calling '#{cmd}'): '#{err.strip}'")
    end

    true
  end

  def self.path_to_url(path)
    path.to_s.sub('public/', '/')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
letter_avatar_for7-0.1.0 lib/letter_avatar.rb