david234

david234

Image resizing and uploading to s3 bucket with mogrify - Image not resized

I was uploading images to the s3 bucket using this library ex_aws_s3. Things were working really fine.

Now I need the image to be resized before uploading to the s3 bucket. I am using mogrify to resize image in backend.

When I did that, the image is stored in the same size. It is not resized. I know that I have missed something. Below is my code.

  def upload_image(file) do

    image_bucket = "#{bucket_name}"

    # Extract the file extension
    file_extension = Path.extname(file.filename)
    # Generate a unique filename
    s3_filename = unique_filename(file_extension)

    m = file.path |> Mogrify.open |> Mogrify.resize("100x100") 

    # Load the file into memory
    #{:ok, file_binary} = File.read(file.path)
    IO.inspect m
    {:ok, file_binary} = File.read(m.path)

    upload_to_s3 =
      S3.put_object(image_bucket, s3_filename, file_binary)
      |> ExAws.request()

    image_url = "https://s3.ap-south-1.amazonaws.com/" <> "#{image_bucket}/#{s3_filename}"

    {:ok, image_url}
  end

Can someone help me to find out what I am missing here?

Most Liked

alanvardy

alanvardy

Hello!

You are just missing the save:

      import Mogrify

      open(path)
      |> resize("100x100")
      |> save()

I also suggest using auto_orient if users are uploading images that have been taken on their mobile devices, ie:

      import Mogrify

      open(path)
      |> auto_orient()
      |> resize("300x300")
      |> save()

When someone takes a picture in any direction other than upright the phone doesn’t reorient the picture, it just stores meta-data telling the viewer which way it needs to be flipped. Not all browsers support it so auto_orient could go a long way to retaining your sanity.

Hope that helps!

TechedImpala

TechedImpala

Solved

I have solved my issue by installing ImageMagick on my machine. :tada:
If it is not installed it just does not save the image that you are trying to resize.

One should be able to run the magick command in the terminal you are running.

Hope this helps a future dev.

besnikshabani

besnikshabani

It should works like this. I just tested and works.

"SUCCESS"

Are you sure that you don’t delete files from tmp directory directly?
Try copy the file in another directory and read it.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement