pics_or_it_didnt_happen 1.1.2 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pics_or_it_didnt_happen.rb +34 -12
- metadata +18 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ec3419513ce44b18ae77d5db3ff1a9a7da325d3341e7568ec9b7adfbd56e9e7
|
4
|
+
data.tar.gz: 3b615352701b3916a28e913f1aaeb34268f5696ca94a734fc844f84ffcb4617b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 968a4d86d80fdf761cc596b04a6e608a28d277e02a07f2f421f2ea7bc2af23fd7178a93c523638a07e29443a31b694a1d881a6d551523ff930ae8be806e3f075
|
7
|
+
data.tar.gz: 13233708d75d2a245d2ff9d589fe7f8d1263692228149c0898c9e0e194f3d0df6acf3bc94c35ee2ffa27022456b4d1814206688a08ce20f373c3ff425e19f2a8
|
@@ -1,28 +1,50 @@
|
|
1
1
|
require 'base64'
|
2
2
|
|
3
3
|
module PicsOrItDidntHappen
|
4
|
-
def self.image_to_data_url_image_tag(
|
5
|
-
raise ArgumentError, "
|
4
|
+
def self.image_to_data_url_image_tag(passed_image, alt_text: nil, class: nil)
|
5
|
+
raise ArgumentError, "passed_image must be a binary string or a file path" unless passed_image.is_a?(String)
|
6
|
+
# check if passed_image is a file path that exists
|
7
|
+
check_if_file_exists = Proc.new do |file_path|
|
8
|
+
begin
|
9
|
+
File.exist?(file_path)
|
10
|
+
rescue ArgumentError => e
|
11
|
+
false
|
12
|
+
end
|
13
|
+
end
|
14
|
+
if check_if_file_exists.call(passed_image)
|
15
|
+
# read the file as binary
|
16
|
+
begin
|
17
|
+
file_in_binary = IO.binread(passed_image)
|
18
|
+
rescue ArgumentError => e
|
19
|
+
raise ArgumentError, "#{passed_image} could not be read as a binary string"
|
20
|
+
end
|
21
|
+
else # passed_image may be a binary string of an image
|
22
|
+
file_in_binary = passed_image
|
23
|
+
end
|
24
|
+
# validate the keyword arguments
|
6
25
|
raise ArgumentError, "alt_text must be a string" unless alt_text.is_a?(String) || alt_text.nil?
|
7
|
-
|
8
|
-
|
26
|
+
klass = binding.local_variable_get(:class)
|
27
|
+
raise ArgumentError, "class must be a string or an Array of strings" unless klass.is_a?(String) || (klass.is_a?(Array) && klass.all? { |c| c.is_a?(String) } ) || klass.nil?
|
28
|
+
# convert an array of classes to a single string separated by spaces
|
29
|
+
klass = klass.join(" ") if klass.is_a?(Array)
|
30
|
+
# encode the binary string / image as base64
|
9
31
|
base64_encoded_image_data = [file_in_binary].pack('m0')
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
32
|
+
# build the HTML string
|
33
|
+
src_string = "data:#{mime_type_of(file_in_binary)};base64,#{base64_encoded_image_data}"
|
34
|
+
html_strings = ["src=\"#{src_string}\""]
|
35
|
+
html_strings << "alt=\"#{alt_text}\"" if alt_text
|
36
|
+
html_strings << "class=\"#{klass}\"" if klass
|
37
|
+
return "<img " + html_strings.join(" ") + "/>"
|
15
38
|
end
|
16
39
|
|
17
40
|
# this method was inspired by a Alain Beauvois's StackOverflow answer: https://stackoverflow.com/a/16635245
|
18
|
-
def self.mime_type_of(
|
19
|
-
raise ArgumentError, "No file was found at #{file_path}" unless File.exist?(file_path)
|
41
|
+
def self.mime_type_of(image_binary_string)
|
20
42
|
# define some regular expressions to match the most common image file types
|
21
43
|
png = Regexp.new("\x89PNG".force_encoding("binary"))
|
22
44
|
jpg = Regexp.new("\xff\xd8\xff\xe0\x00\x10JFIF".force_encoding("binary"))
|
23
45
|
jpg2 = Regexp.new("\xff\xd8\xff\xe1(.*){2}Exif".force_encoding("binary"))
|
24
46
|
# Read the first 10 bytes of the file
|
25
|
-
case
|
47
|
+
case image_binary_string[0, 10]
|
26
48
|
# MIME types from this list: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
|
27
49
|
when /^GIF8/
|
28
50
|
'image/gif'
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pics_or_it_didnt_happen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Greg Matthew Crossley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
12
|
-
dependencies:
|
11
|
+
date: 2023-08-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
description: |
|
14
28
|
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
29
|
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.
|
@@ -43,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
57
|
- !ruby/object:Gem::Version
|
44
58
|
version: '0'
|
45
59
|
requirements: []
|
46
|
-
rubygems_version: 3.4.
|
60
|
+
rubygems_version: 3.4.14
|
47
61
|
signing_key:
|
48
62
|
specification_version: 4
|
49
63
|
summary: A Ruby gem that lets you include images in HTML using data URLs (and avoid
|