# Step 1: Require necessary libraries
require 'base64'
require 'open-uri'
# Step 2: Provide the URL of the image
image_url = 'https://example.com/image.jpg'
# Step 3: Open and read the image file
image_data = open(image_url, 'rb') { |file| file.read }
# Step 4: Convert the image data to base64 encoding
base64_encoded_image = Base64.encode64(image_data)
# Step 5: Optional - Remove newlines from the base64-encoded string
base64_encoded_image.gsub!("\n", '')
# Step 6: Display or use the base64-encoded image as needed
puts base64_encoded_image