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
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
Solved
I have solved my issue by installing ImageMagick on my machine. ![]()
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









