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?

1 Like

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!

3 Likes

Now i have used this

  def upload_image(file) do

    image_bucket = "#{bucket_name}"

    # Extract the file extension
    IO.inspect "......................................................file extension....................................................................................."
    file_extension = Path.extname(file.filename)
    IO.inspect file_extension
    # Generate a unique filename
    s3_filename = unique_filename(file_extension)
    # Load the file into memory
    #{:ok, file_binary} = File.read(file.path)

    m = open(file.path)
          |> auto_orient()
          |> resize("300x300")
          |> save()

    IO.inspect m 
    
    file_binary=""

    case File.read(m.path) do
      
      {:ok, file_binary} ->
        IO.inspect "SUCCESS"
        {:ok, file_binary}

      {:error, error} ->
        IO.inspect "ERROR"
        IO.inspect error
    end

    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}"
    image_url

    {:ok, image_url}
  end

Terminal I am getting this

%Mogrify.Image{
  animated: false,
  buffer: nil,
  dirty: %{},
  ext: "",
  format: nil,
  frame_count: 1,
  height: nil,
  operations: [],
  path: "/tmp/451985-multipart-1577185904-453828663469126-2",
  width: nil
}
"ERROR"
:enoent

Image stored in s3 bucket empty like 0kb

Error in this line
case File.read(m.path) do

Any idea why it fails to read from this path: “/tmp/451985-multipart-1577185904-453828663469126-2”

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.

1 Like

No i did not delete anything from/tmp directory.

After you said i tried again and went to the /tmp directory. I could not find the specified filename.

All i found is this only

root@ubuntu-s:/tmp# ls
plug-1576  systemd-private-93a952987b404560ac58e7c6458e0af6-systemd-resolved.service-F3dKrl   v8-compile-cache-0
plug-1577  systemd-private-93a952987b404560ac58e7c6458e0af6-systemd-timesyncd.service-Fu1rrC

I tried using both

mix phx.server

sudo mix phx.server

in both, I get the same error.

Can you paste the code which you tested. So that i can check for any difference.

I am experiencing the exact same issue.

Have you had any success @david234

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.

2 Likes