Class: DouyinImage::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/douyin_image/image.rb

Instance Method Summary collapse

Instance Method Details

#generate(file_path, output_path, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/douyin_image/image.rb', line 5

def generate(file_path, output_path, options={})
  offset = (options.delete(:offset) || 8).to_i.abs

  im = Vips::Image.new_from_file(file_path)
  im_r = im * [1, 0, 0] # 获取 R 值数据
  im_gb = im * [0, 1, 1] # 获取 GB 值数据

  im_r = im_r.embed(offset, offset, im.width, im.height)

  # 生成图片数据
  target_im = im_r.add(im_gb)
  target_im = random_part_offset(target_im, options)

  # 优化偏移导致的图片边框颜色问题
  target_im = target_im.embed(-offset, -offset, im.width-offset, im.height-offset)
  target_im.write_to_file(output_path)
end