Skip to content

Commit

Permalink
Merge pull request #286 from gintama91/local_image
Browse files Browse the repository at this point in the history
local images (base64)
  • Loading branch information
noahgibbs authored Jul 4, 2023
2 parents 28f4735 + 74d259b commit 8e931fe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ This is where most of the action is happening right now, and to have the full Sc
# dependencies - Mac version
brew install portaudio pkg-config # for sound!
# dependencies - Ubuntu Linux version
sudo apt-get install libportaudio2
sudo apt install libgtk-3-dev libwebkit2gtk-4.0-dev libportaudio2
for any other linux or windows. please see the webview docs for your [platform](https://github.com/webview/webview#prerequisites)
# get it
git clone http://github.com/scarpe-team/scarpe
Expand Down
4 changes: 4 additions & 0 deletions examples/local_images.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Shoes.app do
image "docs/static/man-builds.png"
end

24 changes: 24 additions & 0 deletions lib/scarpe/wv/image.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# frozen_string_literal: true

require "base64"
require "uri"

class Scarpe
class WebviewImage < WebviewWidget
def initialize(properties)
super

@url = valid_url?(@url) ? @url : "data:image/png;base64,#{encode_image_to_base64(@url)}"
end

def element
Expand All @@ -20,6 +25,25 @@ def element

private

def valid_url?(string)
uri = URI.parse(string)
uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
rescue URI::InvalidURIError, URI::BadURIError
false
end

def encode_image_to_base64(image_filename)
directory_path = File.dirname(__FILE__, 4)

image_path = File.join(directory_path, image_filename)

image_data = File.binread(image_path)

encoded_data = Base64.strict_encode64(image_data)

encoded_data
end

def style
styles = {}

Expand Down

0 comments on commit 8e931fe

Please sign in to comment.