Save Image in Database

Asked

Viewed 462 times

4

In my system there is a registration of Professional who has the option to save the photo of Professional also, at the moment I am doing a photo conversion to Base64 with Javascript in the front-end and sending it to back-end that converts to byte[] and stored in the database in a column of the type bytea.

At the moment I’m studying all this flow and I realized that this slows down the system because, for example, a request from front-end to the back-end to get a list of Professionals ends up being slow because the JSON that comes from back-end is very big because of the photos in Base64.

Does anyone have any ideas that can help me get this flow faster, some other kind of image conversion instead of Base64 so that the JSON don’t get so heavy, or some other kind of conversion that I can do in my back-end to store in the bank.

Someone can help me?

Man Back-end is in Java with the framework Spring Boot. Man Front-end is in Angular 7 then use Typescript/Javascript. Man Banco de Dados is in PostgreSQL

  • 3

    When it comes to image saving, the most feasible is you to save the image in a directory on the server and save in the database only a reference to that image. Database image saving only makes the application slower to respond.

  • The front-end always expects Professionals to come with photo information or only in specific cases?

  • @Jorge. M I know this but in the planning of the system I am working they decided that I could not do this.

  • @Arthurferraz I already understood what you meant and yes I’ve been working on it since last week, I’m making the photo come only in requests q will actually use it, but I’m still looking for a solution to my problem, better conversions to save the image in the bank.

1 answer

1


The database is good for dealing with dice, as tables with multiple columns and information. File systems are good to handle files, as images of various sizes and documents.

Storing on a file server

If you have too many images or the server has too many requests that deal with the images, use an image hosting service such as Amazon S3 or Google Cloud Storage. This solution is good because:

  • Your database will only save a reference to that image and not the entire image. Example: imagens.host.com/bruno/imagem123.png.
  • The front-end will no longer convert the base 64 photo to image, but only upload it from an HTTP server.
  • Images will be cached, provided by browser and image hosting.
  • Less band being trafficked during requests.
  • The front end will be able to assemble the full page, with all relevant information coming from the back end, and the images can be uploaded later. This increases your SEO score and performance in the First Meaningful Paint category.

If you are going to opt for this solution, you can also use a tool to crop the images in various sizes, so you would have several sizes of the same image, praising the performance of the HTTP request depending on the speed of the client’s connection. This is why sometimes Facebook, for example, gives us smaller images (i.e., lower quality) when we are on a slower internet. One of the most used tools for this is Imagemagick, which has a wrapper for Java.

Storing in the database

If yet, seen so many advantages of saving the images on an external server, which was made for files, you want to save them in your database, keep in mind:

  • The database can grow very fast if you keep your images in it, decreasing your writing and reading performance
  • There are not many advantages, since there is no indexing, search, or joins in this type of column

The solution would be to use the type blob, available in Postgresql.

  • Thank you very much for your reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.