elixir-image/image

Converting upload image to base64

Closed this issue · 4 comments

I'm trying to convert the image which I wanted to upload to base64 string.

Tried:
Image.open!("/local/tmp/multipart-1681457413-462973371191-2")

But it doesn't work.

Im not clear on what you are indicating. That the image file doesn't open? Or are you asking how to convert the image to base64? Can you add the image file to the issue? Its impossible to do anything with the limited information you have posted.

I'm uploading an image but wanted to send the image string to another api which accept base64 string.

I have a multipart form from there I'm uploading an image.

upload_image = "#{File.cwd!()}/images/${upload.path}"
image = convert_to_binary(upload_image) # <--- I wanted to convert here
# image = Image.open!(upload_image)  <--- This didn't work
headers = %{Accept: "application/json", "Authorization: "xxxx"}
body = %{inputs: [%{data: %{image: %{base64: image}}}]}
{:ok, response} = HTTPoison.post(base_url(), body, header, [])

Is Image can convert to base64 strings? Or I should use other dependencies?

If all you are doing is encoding the image to base64 then you don't need the image library. Something like this should work:

iex> "#{File.cwd!()}/images/#{upload.path}"
...> |> File.read!()
...> |> Base.encode64()

Thanks that helps