Skip to content

Instantly share code, notes, and snippets.

@nakajmg
Last active December 14, 2015 02:09
Show Gist options
  • Save nakajmg/5012026 to your computer and use it in GitHub Desktop.
Save nakajmg/5012026 to your computer and use it in GitHub Desktop.
macのターミナルから画像をhtmlとcssで使いやすいようにdata:とかつけてbase64エンコード(改行なしで)して出力するびー。 結果は元画像があるフォルダの中のbase64フォルダにされるよ。 #output format => data:[mime/type];base64,encodeString
#!/usr/bin/ruby
require 'base64'
require 'mime/types'
#install mime/types
#gem install mime-types
if File.exists?(ARGV[0])
img = open( ARGV[0] ) #file open
dir = File.dirname(ARGV[0]) #directory name
ext = File.extname(ARGV[0]); #an extension
fname = File.basename( ARGV[0], ext) #remove an extension filename
type = MIME::Types.type_for(ARGV[0])[0].to_s #get MIME/TYPE
encodetext = Base64.strict_encode64( img.read ) #encode base64 without new line
Dir::mkdir(dir+"/base64") unless File.exists?(dir+"/base64")
File.open(dir+"/base64/"+fname,'w'){|f| #output dir path & file name
f.write("data:"+type+";base64," + encodetext ) #output string
}
else
abort( "ERROR:" "FIle "+ARGV[0]+ "not exists." ) #error XD
end
###
## usage
# ruby b64e.rb filename
# or
# add bash_profile function (bashrc) #
## code
# b64e() { command ruby #b64e.rb's path#b64e.rb $1 }
#
## usage
# b64e filename
###
@sadah
Copy link

sadah commented Feb 23, 2013

セミコロンがあるのがきもいっすw

@nakajmg
Copy link
Author

nakajmg commented Feb 23, 2013

いつのまにか入ってた(^q^)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment