Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

isaackearl's avatar

Image hosting with a laravel API (architecture help)

Hello!

So I've been racking my brain trying to come up with the best solution for a problem I need to tackle in the next few days. I would really appreciate some good ideas or some tips on best practices from anybody who reads this.

So I have a Laravel API (laravel 5), which is the backend to an angular front end. One of the fields in the database for the User is the avatar. Currently they can use an image URL to show an avatar image (which they can pull in through facebook and google).

I'm creating the functionality to upload an image to the backend so they can set a custom avatar using whatever image they want. Now I'm contemplating how I should store the images and how I should return them.

the rest of the API communicates solely with json responses etc... anyway here are my ideas

#1 so I'm wondering if I should just grab the image and store it in the database as a base64 string or something, and then return it when it is requested. I could then decode it and display it on the front end.. but is that a good idea? Not sure how it would perform.

#2 Would it be better for me to just put the images in the file storage somewhere using the Storage facade, and then actually serve them up from my php backend... then in the API response I could send a URL to the image location? The benefit of this to me would be I could eventually store them on amazon cloud or something.

#3 Another option is I could store them in my public folder and then serve them up and do the same as I said above except for without using Storage.

#4 Should I upload to some other image hosting service, and then just return the URL to the front-end?

#5 is there an idea that is better then all my other ideas? maybe I store in Storage... and then return them as base64 strings rather then serve it up?

#6 I could use a service like cloudinary, and host files there.. then just return the cloudinary url to my frontend..

Also i'm not looking for the easiest solution.. I'm looking for the best long term solution, because we are building this for a large user base.

Thanks!

0 likes
3 replies
daniel3d@ymail.com's avatar

Hi mate i will say using storage for long term solution as you will have more flexibility when it come to re-sizing, re-allocating the images also another interesting thing i found is the problem of saving allot of files in one location so much better to go with something like the amazon s3 storage.

I like the facebook api approach for the avatars you can re-size the images by passing url prams on fly. what i imagine they have is a storage place where they store the raw photos and then some kind of cashing mechanism to serve the photos.

Im working on something similar in the moment i will post some code later.

RobinMalfait's avatar

Hey @isaackearl

I want you to introduce you to a new service a created.

https://avatarize.me you could argue that it is somewhat like Gravatar, but I've built this for all my own projects.

You can look at the API on how you can use it here (https://avatarize.me/api)

You can get a users image using their e-mail address or using an md5 hash of the e-mail address for more security and obfuscating the e-mail address.

There are also some options like scaling the size automatically, greyscale and pixelate.

All the images are also cached, so you have almost no latency, once a user uploads a new image, the cache will be cleared for that user :)

Last but not least, I have this wonderful embed code that only requires 2 (two!!) lines of code in your application

You can look at this link for that:

https://avatarize.me/api#embed

Example of the upload embed:

and

I hope you like it.

Please or to participate in this conversation.