├── testfiles ├── test.base64 └── birdie.png ├── test ├── test_helper.exs └── image64_test.exs ├── .gitignore ├── LICENSE ├── config └── config.exs ├── mix.exs ├── lib └── image64.ex └── README.md /testfiles/test.base64: -------------------------------------------------------------------------------- 1 | = 2 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /_build 2 | /cover 3 | /deps 4 | erl_crash.dump 5 | *.ez 6 | /testbuild 7 | -------------------------------------------------------------------------------- /testfiles/birdie.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mechasparrow/image64/HEAD/testfiles/birdie.png -------------------------------------------------------------------------------- /test/image64_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ImageBase64HandlerTest do 2 | use ExUnit.Case 3 | doctest ImageBase64Handler 4 | 5 | test "simple image conversion" do 6 | ImageBase64Handler.imagetobase64url("testfiles/birdie.png", "testbuild/test.base64") 7 | ImageBase64Handler.base64urlFileToImage("testbuild/test.base64","testbuild/birdie.png") 8 | 9 | {:ok, file1} = File.read("testbuild/birdie.png") 10 | {:ok, file2} = File.read("testfiles/birdie.png") 11 | 12 | assert file1 == file2 13 | end 14 | 15 | 16 | 17 | end 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Michael Navazhylau 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /config/config.exs: -------------------------------------------------------------------------------- 1 | # This file is responsible for configuring your application 2 | # and its dependencies with the aid of the Mix.Config module. 3 | use Mix.Config 4 | 5 | # This configuration is loaded before any dependency and is restricted 6 | # to this project. If another project depends on this project, this 7 | # file won't be loaded nor affect the parent project. For this reason, 8 | # if you want to provide default values for your application for 9 | # 3rd-party users, it should be done in your "mix.exs" file. 10 | 11 | # You can configure for your application as: 12 | # 13 | # config :image64, key: :value 14 | # 15 | # And access this configuration in your application as: 16 | # 17 | # Application.get_env(:image64, :key) 18 | # 19 | # Or configure a 3rd-party app: 20 | # 21 | # config :logger, level: :info 22 | # 23 | 24 | # It is also possible to import configuration files, relative to this 25 | # directory. For example, you can emulate configuration per environment 26 | # by uncommenting the line below and defining dev.exs, test.exs and such. 27 | # Configuration from the imported file will override the ones defined 28 | # here (which is why it is important to import them last). 29 | # 30 | # import_config "#{Mix.env}.exs" 31 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule ImageBase64Handler.Mixfile do 2 | use Mix.Project 3 | 4 | def project do 5 | [app: :image64, 6 | version: "0.0.2", 7 | elixir: "~> 1.2", 8 | build_embedded: Mix.env == :prod, 9 | start_permanent: Mix.env == :prod, 10 | description: description(), 11 | package: package(), 12 | deps: deps] 13 | end 14 | 15 | # Configuration for the OTP application 16 | # 17 | # Type "mix help compile.app" for more information 18 | def application do 19 | [applications: [:logger]] 20 | end 21 | 22 | # Dependencies can be Hex packages: 23 | # 24 | # {:mydep, "~> 0.3.0"} 25 | # 26 | # Or git/path repositories: 27 | # 28 | # {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"} 29 | # 30 | # Type "mix help deps" for more examples and options 31 | defp deps do 32 | [] 33 | end 34 | 35 | defp description do 36 | """ 37 | A tool for working with base64 encoded images 38 | """ 39 | end 40 | 41 | defp package do 42 | [# These are the default files included in the package 43 | name: :image64, 44 | files: ["lib", "priv", "mix.exs", "README*", "readme*", "LICENSE*", "license*"], 45 | maintainers: ["Michael Navazhylau"], 46 | licenses: ["MIT Liscense"], 47 | links: %{"GitHub" => "https://github.com/Mechasparrow/image64"}] 48 | end 49 | 50 | end 51 | -------------------------------------------------------------------------------- /lib/image64.ex: -------------------------------------------------------------------------------- 1 | defmodule ImageBase64Handler do 2 | ## Note that these are url encoding and decoding 3 | import Base 4 | 5 | 6 | def base64urlFileToImage(fileInputPath, fileOutputPath) do 7 | {:ok, base64data} = File.read(fileInputPath) 8 | base64urlToImage(base64data, fileOutputPath) 9 | end 10 | 11 | def base64urlToImage(base64data, fileOutputPath) do 12 | imgdata = url_decode64!(base64data) 13 | {:ok, file} = File.open(fileOutputPath, [:write]) 14 | IO.binwrite(file, imgdata) 15 | File.close(file) 16 | true 17 | end 18 | 19 | def imagetobase64url(fileInputPath, fileOutputPath) do 20 | {:ok, imageData} = File.read(fileInputPath) 21 | base64data = url_encode64(imageData) 22 | {:ok, file} = File.open(fileOutputPath, [:write]) 23 | IO.binwrite(file, base64data) 24 | File.close(file) 25 | end 26 | 27 | def imagetobase64(fileInputPath, fileOutputPath) do 28 | {:ok, imageData} = File.read(fileInputPath) 29 | base64data = encode64(imageData) 30 | {:ok, file} = File.open(fileOutputPath, [:write]) 31 | IO.binwrite(file, base64data) 32 | File.close(file) 33 | end 34 | 35 | def base64ToImage(base64data, fileOutputPath) do 36 | imgdata = decode64!(base64data) 37 | {:ok, file} = File.open(fileOutputPath, [:write]) 38 | IO.binwrite(file, imgdata) 39 | File.close(file) 40 | true 41 | end 42 | 43 | end 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ImageBase64Handler 2 | 3 | ## What is image64 4 | image64 is a library for elixir that helps convert base64 images into regular image files. 5 | 6 | ## Installation 7 | 8 | If [available in Hex](https://hex.pm/docs/publish), the package can be installed as: 9 | 10 | 1. Add image64 to your list of dependencies in `mix.exs`: 11 | 12 | def deps do 13 | [{:image64, "~> 0.0.1"}] 14 | end 15 | 16 | 2. Ensure image64 is started before your application: 17 | 18 | def application do 19 | [applications: [:image64]] 20 | end 21 | 22 | ## Usage 23 | 24 | First import the module 25 | 26 | `import ImageBase64Handler` 27 | 28 | if you want to convert a base64 url and output it to a file 29 | 30 | `base64ToImage(base64data, fileOutputPath)` 31 | 32 | This will output the image to the designated `fileOutputPath` 33 | 34 | if you want to convert base64 file and convert it to an image file 35 | 36 | `base64urlFiletoImage(fileInputPath, fileOutputPath)` 37 | 38 | if you want to convert a image to a base64url file 39 | 40 | `imagetobase64(fileInputPath, fileOutputPath)` 41 | 42 | This will take a image file path and an output file path and output the base64 data into that file 43 | 44 | Those are the two key functions to convert to and from base64 images 45 | 46 | ## Think something should be added? 47 | Open a issue or pull request and I will take a look. 48 | --------------------------------------------------------------------------------