pics_or_it_didnt_happen 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/pics_or_it_didnt_happen.rb +39 -0
  3. metadata +51 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2a8e935ec65c38271506c2a5bece5d706a660c7386cd100c9282773e7e128b95
4
+ data.tar.gz: 323232618a4d30ed8f625c976b52380185fda853fa90d21fdb5f0db322a403d2
5
+ SHA512:
6
+ metadata.gz: c5974894490f3ff424a7a052f8a21dd9ab012d6debc5bba4311c302ce210a1112f75a08aede9859f3185f8d7198de6c691296d59de580c8c78d42f3581f469d6
7
+ data.tar.gz: 911e9d5a72afb150d2723a20cfeeb8f46459200046525917402ec7f670090192317305971a3f4003458f02804dfed484c2199f0d49e6794ad92b87a3c9fa8572
@@ -0,0 +1,39 @@
1
+ require 'base64'
2
+
3
+ module PicsOrItDidntHappen
4
+ def self.image_to_data_url_image_tag(file_path, alt_text: nil)
5
+ raise ArgumentError, "No file was found at #{file_path}" unless File.exist?(file_path)
6
+ file_in_binary = IO.binread(file_path)
7
+ base64_encoded_image_data = [file_in_binary].pack('m0')
8
+ src_string = "data:#{mime_type_of(file_path)};base64,#{base64_encoded_image_data}"
9
+ if alt_text
10
+ return "<img src=\"#{src_string}\" alt=\"#{alt_text}\">"
11
+ else
12
+ return "<img src=\"#{src_string}\">"
13
+ end
14
+ end
15
+
16
+ # this method was inspired by a Alain Beauvois's StackOverflow answer: https://stackoverflow.com/a/16635245
17
+ def self.mime_type_of(file_path)
18
+ raise ArgumentError, "No file was found at #{file_path}" unless File.exist?(file_path)
19
+ # define some regular expressions to match the most common image file types
20
+ png = Regexp.new("\x89PNG".force_encoding("binary"))
21
+ jpg = Regexp.new("\xff\xd8\xff\xe0\x00\x10JFIF".force_encoding("binary"))
22
+ jpg2 = Regexp.new("\xff\xd8\xff\xe1(.*){2}Exif".force_encoding("binary"))
23
+ # Read the first 10 bytes of the file
24
+ case IO.read(file_path, 10)
25
+ # MIME types from this list: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
26
+ when /^GIF8/
27
+ 'image/gif'
28
+ when /^#{png}/
29
+ 'image/png'
30
+ when /^#{jpg}/
31
+ 'image/jpeg'
32
+ when /^#{jpg2}/
33
+ 'image/jpeg'
34
+ else
35
+ raise UnprocessableEntity, "File doesn't appear to be a GIF, PNG, or JPEG (based the file's first 10 bytes)"
36
+ end
37
+ end
38
+ end
39
+
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pics_or_it_didnt_happen
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Greg Matthew Crossley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-11-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ Sometimes, you might want your HTML to include a one-off image file that is just for one person. Making this file public may be undesireable for security reasons, or perhaps simply because it is not worth the overhead of multiple HTTP requests.
15
+ This gem provides a utility method that takes a locally-saved image file, perhaps within your non-public tmp directory, encodes it as Base64, and returns an HTML <img> element with the correct data URL attributes.
16
+ It is made possible by the RFC 2397 scheme, which is now fairly well supported in modern browsers.
17
+ email:
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/pics_or_it_didnt_happen.rb
23
+ homepage: https://github.com/NeomindLabs/pics_or_it_didnt_happen
24
+ licenses:
25
+ - MIT
26
+ metadata:
27
+ bug_tracker_uri: https://github.com/NeomindLabs/pics_or_it_didnt_happen/issues
28
+ documentation_uri: https://github.com/NeomindLabs/pics_or_it_didnt_happen
29
+ homepage_uri: https://github.com/NeomindLabs/pics_or_it_didnt_happen
30
+ source_code_uri: https://github.com/NeomindLabs/pics_or_it_didnt_happen
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.1.6
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: A Ruby gem that lets you include images in HTML using data URLs (and avoid
50
+ serving the image file separatly).
51
+ test_files: []