Class: Jekyll::AssetImg

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/ext.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, tokens) ⇒ AssetImg

Returns a new instance of AssetImg.



31
32
33
34
35
36
37
38
# File 'lib/ext.rb', line 31

def initialize(tag_name, text, tokens)
  arr =  text.strip.split(" ")
  @img_name = arr[0]
  @img_width = nil
  if arr.count > 1
    @img_width = arr[1]
  end
end

Instance Method Details

#guess_image_mime_by_base64(base64_str) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ext.rb', line 40

def guess_image_mime_by_base64(base64_str)
  prefix = base64_str[0, 15]  # 取前 15 个字符足够判断

  case
  when prefix.start_with?('/9j/')
    'image/jpeg'
  when prefix.start_with?('iVBOR')
    'image/png'
  when prefix.start_with?('R0lGODdh', 'R0lGODlh')
    'image/gif'
  when prefix.start_with?('UklGR')
    'image/webp'
  else
    'image/png'
  end
end

#render(context) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ext.rb', line 58

def render(context)
  path = context['page']['path']
  

  dirPath0 = path[0...(path.length - 3)]
  pathComponent = dirPath0.split("/")
  dirPath = pathComponent[-1]
  
  base = $g_config['baseurl']
  link = "/pics/#{dirPath}/#{@img_name}"
  if base && base.length
    link = "#{base}/pics/#{dirPath}/#{@img_name}"
  end 

  enc = EncFilterClass.new
  encid = enc.get_encrypt_id('',context['page'])
  if !encid.nil? && encid.length > 0
    begin
      file = File.open(File.expand_path("pics/#{dirPath}/#{@img_name}"))
      b64 = Base64.strict_encode64 file.read()
      link =  "data:image/#{guess_image_mime_by_base64(b64)};base64," + b64  
      puts "Encrypt img #{"pics/#{dirPath}/#{@img_name}"}"
    rescue => exception
      puts "\n----------------------------"
      puts exception

      puts "\nEncrypt img #{"pics/#{dirPath}/#{@img_name}"} Fail"
      puts "----------------------------"
      
      link = 'ERROR '
    end
    
    


    
  else
  end
  

  if @img_width != nil
    return "<img src='#{link}' style='width:#{@img_width}px'>"
  else
    return "![](#{link})" 
  end

  
end