If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Database Storage for Images

Started by DietamiiO21, Jul 21, 2023, 07:01 AM

Previous topic - Next topic

DietamiiO21Topic starter

 When I was designing a website, I encountered an issue regarding the presentation of images on different pages. Around 20-30 images were intended to be displayed on each page, accompanied by a corresponding description.
In terms of implementation using PHP, what would be the most efficient approach? One option I am considering is storing each image and its dimensions in the MySQL database. However, I wonder if this would still be reasonable when dealing with a large number of pictures.
  •  


tamilabi

When dealing with a large number of pictures, storing each image and its dimensions directly in the MySQL database may not be the most efficient approach. Retrieving and storing images from a database can put a strain on the server's resources and slow down the website's performance.

A more efficient approach would be to store the images as files on the server's file system and only store the image filenames and descriptions in the database. You can create a table in your database that includes columns for the filename, description, and any other relevant information about the images.

To display the images on different pages, you can query the database for the desired number of images and their corresponding information. Then, dynamically generate the HTML code to display the images on the webpage using PHP.

This approach allows the database to handle the retrieval and storage of the image metadata efficiently, while the actual images are served directly from the file system, which is usually faster and less resource-intensive.

Additionally, you can further optimize the image loading by implementing techniques such as lazy loading or progressive loading, where images are loaded as the user scrolls down the page, reducing the initial loading time and improving the overall user experience.
  •  

xhuyvn

To establish a table within the database, three columns are required: ID, img_name, and img_description. This structure enables efficient organization of image-related data. Once created, PHP can be utilized to retrieve information from the database.
Additionally, by indicating the directory where the photos are stored, developers can customize the output to meet their specific requirements.
  •  

perfectdomains

In my view, it is necessary to eliminate this.
If there are 1000 managers in your company who don't do anything, but they can only work through a CMS, or rather they can't even do that, but they claim they need to, then it's logical.

For a considerable period, the images have been stored simply based on the external product code (whether it's the 1C or supplier code or assigned by a script) - and they are all stored in one folder. When we come across a product without an image, we rename it and add it. Then, your website resizes the images and based on the name of the original photo, it can always check if there is a cached smaller size and display it.

For managers, instructions are provided that explain how to rename and here is the form - I select it from my computer, drag it. The script records who and when after the download is complete.

Of course, you can save some memory by not retrieving a new cached name from the path/name every time - and store it in any table - be it a DB, excel, json, or any other format you prefer. However, searching in this table will require some time.

Honestly, even in the legendary c++ with hash table structures (a limited array with a list (an unlimited array) of keys), it is impractical to use a hash function instead of simply accessing a list cell (unlimited arrays).

So, storing a list of paths is neither a more expensive nor a less expensive operation than obtaining its name from the image's path every time and checking for the presence of a file. It's just as bothersome to check for its presence, even if you have a list of paths.
  •  

itaviotojence

Storing each image and its dimensions in the MySQL database might not be the best solution due to potential performance issues, especially as the number of images grows.

A more efficient approach would be to store the images themselves on the server and then maintain the metadata, such as descriptions and file names, in a MySQL database. This way, you can leverage the database for managing the associated data while keeping the actual images stored on the server.

In terms of implementation using PHP, you can create a database table to store the metadata of the images, including fields for the image description, file name, dimensions, and any other relevant details. When a page needs to display a set of images, your PHP script can query the database to fetch the necessary information and then dynamically generate the HTML markup to display the images along with their descriptions.

To enhance performance, consider implementing caching mechanisms to reduce the load on the database and improve the overall responsiveness of the pages. You can also optimize the images for web display by using appropriate compression techniques and responsive design principles to ensure fast loading times and effective presentation across various devices.

Furthermore, consider implementing lazy loading techniques to progressively load and display images as the user scrolls through the page, reducing the initial load time and improving the overall user experience.
  •  

gopasruhafoni

Storing image dimensions in the database is so last century! Why not use a more modern approach like storing images in a NoSQL database like MongoDB or Couchbase?
These databases are designed to handle large volumes of unstructured data like images and can provide fast query performance. Alternatively, you could use a graph database like Neo4j to store image metadata and relationships, providing a more flexible and scalable solution. The possibilities are endless, and storing image dimensions in a relational database is just plain boring.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...