2

recently i have tried to store images in Mysql database (use BLOB data type), using php and web to upload and store it. It works fine, except when loading large enough image , it is going to load very slowly. Is there any way to load this image faster ?

note : my friend suggests me to use force caching to this image ( he says something about change the content-header of image ), but i don't know how to do it. and i doubt it will bring significantly better performance.

Thanks in advance

Ian Overton
  • 1,060
  • 7
  • 17
DennyHiu
  • 4,861
  • 8
  • 48
  • 80
  • Perhaps this post about [caching dynamic data](http://stackoverflow.com/questions/10596116/caching-http-responses-when-they-are-dynamically-created-by-php/10596231#10596231) can help you – Ja͢ck Dec 14 '12 at 13:46

4 Answers4

2

The images should definitely be cached. I think this can be done mostly just by making sure the image url you make is always the same for the same picture. What I think your problem is though is you need to change max_allowed_packet. If this is too small it won't be able to send much data over the network at one time. Also if the pictures are truly that big I'd also consider changing the quality of the picture to maybe 70%? All the resize image functions have a way to change it. ie: http://php.net/manual/en/function.imagejpeg.php. Hope that helps. I'd also look into YSlow. It'll help point out what exactly is wrong with your images that is making it load slowly. Whether it is quality, cache, compression or w/e it may be.

Ian Overton
  • 1,060
  • 7
  • 17
  • thx for reply. i had make sure that image url always point to the same picture for same address. still reloading and still slowly. But, when talking about your suggestion to PHP function of imagejpeg, it's rocks! it my reduces reloading time a lot – DennyHiu Dec 14 '12 at 14:04
  • I'd also look into yslow plugin. It'll help point out what exactly is wrong with your images that is making it load slowly. Whether it is quality, cache, compression or w/e it may be. – Ian Overton Dec 14 '12 at 14:07
  • I still have not checked out other factors. I just got weird impression when all my site's element loading successfully, and i still have to wait for image from MySQL loading – DennyHiu Dec 14 '12 at 14:18
1

Caching the images could be used when images stored on filesystem. If they are dynamically popped and printed from the database they will be fetched each time the PHP code ask for them.

It could be that images are fetched in a dozens of ms, but a 3MB image data could be downloaded to clients browser for 5 seconds to 1 minute (depending on the connection speed). There is not much to do with it (even less on common shared hostings).

I would suggest storing the images on a filesystem so they could be cached by the browser, or You could even set a memcache on Apache server so until the expire they would be served from the cache.

shadyyx
  • 15,825
  • 6
  • 60
  • 95
  • 2
    Who says images can't be cached if they're coming from a database? :) – Ja͢ck Dec 14 '12 at 14:03
  • It depends on the way they are printed...If they are printed inline as a base64 data like `` they probably won't be cached...Unless FPC cache is enabled... – shadyyx Dec 14 '12 at 14:35
0

I guess that caching can be good or not. Instead of I suggest you to upload images or other files to a folder and save on DB just the information about file: name, type, size, folder, etc...

0

If you don't have any requisite that requires you to store an image in the database, images are better inside a folder and what you should store in the database is the path or the name of each of them.

This would make them load normally. Just depending on the size of the image, of course.

That's what you will find in almost all web applications.

Alvaro
  • 40,778
  • 30
  • 164
  • 336
  • 1
    thx. i know that, but i like the simplicity when i need to delete, update or backup database. i just use sql query to do that without take care of the images in folder. – DennyHiu Dec 14 '12 at 13:55
  • It's ok. Just be careful with the Database space then. – Alvaro Dec 14 '12 at 13:56