Skip to content

Instantly share code, notes, and snippets.

@85636682
Created November 9, 2018 12:12
Show Gist options
  • Save 85636682/6840c9e1be08fda055eb3b37d6ccac4f to your computer and use it in GitHub Desktop.
Save 85636682/6840c9e1be08fda055eb3b37d6ccac4f to your computer and use it in GitHub Desktop.
class ShareImageComposerService
attr_reader :red_pack, :base, :avatar, :wxacode, :watchword
def initialize(red_pack)
@red_pack = red_pack
end
def perform
set_material_images
tempfile1 = base.composite(avatar) { |c| c.compose "Over"; c.geometry "+296+102" }
tempfile2 = tempfile1.composite(wxacode) { |c| c.compose "Over"; c.geometry "+276+508" }
tempfile2.combine_options{ |c| c.font "#{Rails.root}/vendor/assets/fonts/PingFang.ttc" ; c.encoding "UTF-8"; c.pointsize 40; c.fill 'white'; c.gravity "north"; c.draw "text 0,360 '#{watchword}'" }
QiniuUploader.upload_file(ENVConfig.qiniu_domain, ENVConfig.qiniu_bucket, tempfile2).tap do
[base, avatar, wxacode, tempfile1, tempfile2].each { |img| img.destroy! }
end
end
private
def wxacode_params
{scene: "voice-redpack:#{red_pack.id}", page: "pages/voice_redpack/show", width: 246}
# {scene: "voice-redpack:#{red_pack.id}", page: "pages/index/index", width: 246}
end
def set_material_images
@base = MiniMagick::Image.open "#{Rails.root}/vendor/assets/images/voice-red-pack-share-base-image.jpg"
@avatar = MiniMagick::Image.open "#{red_pack.user.avatar_url}?imageView2/0/w/160/h/160|roundPic/radius/!50p|imageslim"
# @wxacode = set_wxacode_by_qiniu
@wxacode = set_wxacode_by_minimagick
@watchword = red_pack.watchword
end
def set_wxacode_by_qiniu
wxacode_url = WeixinMiniProgramService.new(ENVConfig.weixin_oauth_app_id, ENVConfig.weixin_oauth_app_secret).create_wxacode_url(wxacode_params)
MiniMagick::Image.open "#{wxacode_url}?imageView2/0/w/200/h/200|roundPic/radius/!50p"
end
def set_wxacode_by_minimagick
original_wxacode_file = WeixinMiniProgramService.new(ENVConfig.weixin_oauth_app_id, ENVConfig.weixin_oauth_app_secret).create_wxacode_tempfile(wxacode_params)
original_wxacode_image = MiniMagick::Image.open(original_wxacode_file.path).tap { original_wxacode_file.unlink }
original_wxacode_image.resize "200x200"
original_wxacode_image.format 'png'
width = original_wxacode_image[:width]-2
radius = width/2
mask = ::MiniMagick::Image.open original_wxacode_image.path
mask.format 'png'
mask.combine_options do |m|
m.alpha 'transparent'
m.background 'none'
m.fill 'white'
m.draw 'roundrectangle 1,1,%s,%s,%s,%s' % [width, width, radius, radius]
end
overlay = ::MiniMagick::Image.open original_wxacode_image.path
overlay.format 'png'
overlay.combine_options do |o|
o.alpha 'transparent'
o.background 'none'
o.fill 'none'
o.stroke 'white'
o.strokewidth 2
o.draw 'roundrectangle 1,1,%s,%s,%s,%s' % [width, width, radius, radius]
end
masked = original_wxacode_image.composite(mask, 'png') do |i|
i.alpha "set"
i.compose 'DstIn'
end
original_wxacode_image.destroy!
mask.destroy!
result = masked.composite(overlay, 'png') do |i|
i.compose 'Over'
end
masked.destroy!
overlay.destroy!
result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment