Sha256: a0aeb1eb13e72f7afe55b44da54a68cdc7a569eca832abdae049094ae4ac641e

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 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(
        convert
        #{from}
        -background transparent
        -gravity center
        -thumbnail #{width}x#{height}^
        -extent #{width}x#{height}
        -interpolate bicubic
        -unsharp 2x0.5+0.7+0
        -quality 98
        #{to}
      ).join(' ')
    )
    true
  rescue
    false
  end

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

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

    _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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
letter_avatar-0.3.2 lib/letter_avatar.rb
letter_avatar-0.3.1 lib/letter_avatar.rb
letter_avatar-0.3.0 lib/letter_avatar.rb
letter_avatar-0.2.0 lib/letter_avatar.rb