2

I have a site that has a lot of images, like user's logo, product's logo/images or company's logo/images.

enter image description here

Now, I store these logos or images name in MySQL. If I want to know one user's logo, I must query mysql, and find the logo. Just like this:

<img src="{{FileManager::getFile('user_logo',$loop->photo)}}" />

The problem is , my site is growing and become bigger and bigger. Suppose, I have a page, which will show users's info (user's name, user's age... user's logo).
That I must query MySQL to find their logo. Everytime, this will cost lots of time waiting for page loading user's logo. I wonder how can I speed up loading logos/images ?

I am using Laravel 4 with this server specification:

  • Apache
  • PHP 5.4
  • MySQL 5.5
  • Redis 2.6
Cœur
  • 37,241
  • 25
  • 195
  • 267
diligent
  • 2,282
  • 8
  • 49
  • 64
  • 2
    "Now, I store these logo or images **name** in mysql,..." – Markus Kottländer Dec 25 '13 at 12:47
  • @JoachimIsaksson yes, just store images name in mysql, and the file itself are stored in file system. – diligent Dec 25 '13 at 12:50
  • It looks like caching isn't enabled/working well on the images, at least some of the 200's should be previously downloaded, no? If you're using md5 to name the images, you should be able to set caching to never expire an image since it will never change without the file name changing. – Joachim Isaksson Dec 25 '13 at 13:02
  • @JoachimIsaksson thanks man, I am not quite familiar with how to open or config cache on app server. Oh, I am using apache. – diligent Dec 25 '13 at 13:06
  • @diligent Try taking a look [here](http://stackoverflow.com/questions/447014/website-image-caching-with-apache). Only set long cache times for images that change name if they change contents (for example your md5'd files that change name if they change contents are great candidates for caching forever), but even a 10 minute timeout can help quite a lot for images that are included in every/many page loads. – Joachim Isaksson Dec 25 '13 at 13:13

3 Answers3

2

The idea

The best way is to store the logo images in your servers hierarchy according to the user ids. So you don't need to query for the images because you know everything to get them:

  • The images place: Will be a fix folder.
  • The images name: The user's id.

For example you can create an image directory where you can save all the logos with a filename that matches the user's id. In this way you can simply get the user logos through a simple URL like example.com/images/{userid}.

If this doesn't fit for your directory structure just restructure it. For example you can create a folder for each user where you save their pictures with names like logo1, logo2 , etc or simply put this to a subfolder too. You could get those via an URL like example.com/images/{userid}/logo1 or example.com/images/{userid}/logos/1.

Implementation guide

Here are some implementation guides for the server side:

User's profile page

  1. Get the user id from the URL. Like the id 150 from the URL example example.com/users/150.
  2. Validate it.
  3. Load the logo like: <img src="example.com/images/150/logos/1" alt="Logo">

User's list page

  1. Query for all users like: SELECT id, name FROM users WHERE id > 50
  2. Render the list with the images using the user ids.

An example loop for the list:

foreach ($users as $user) {
    echo $user->name;
    echo '<br>';
    echo '<img src="example.com/images/' . $user->id . '/logos/1" alt="Logo">';
}

Every other list

The same as the user's list implementation just make sure you store or join the users so you can get the id's from one query again.

totymedli
  • 29,531
  • 22
  • 131
  • 165
  • thanks totymedli, now do it in that way. On my server, I create a folder to store all the images or logos, everytime when I need load one user's logon. First I read mysql, get the user's logo's name, then I find from path where I put the user's logo on server. Finally, I got a full name of the logo. Although, in this case, page will still cost lots of time to load the logo. – diligent Dec 25 '13 at 12:59
1

I'm supposing logo is not an information to be protected.

You should create a user cookie in which you store (for example) the ID of the user's logo. If cookie is not set, then execute the query, load the logo image and create the cookie.

Example

DB schema:
USER(id, name, blah, ..., logoID)
LOGOS (id, path, size, blah,...)
USER2LOGO(idUser, idLogo)

you store in the client cookie the couple (id,idlogo). If it is present in USER2LOGO, then load it, otherwise execute the query

Lyonard
  • 36
  • 2
0

Store the image in a cookie.

if(!isset($_COOKIE['imgCookie']))
{
    //runs if the user doesn't have the cookie
    //run randomizer code
    //setcookie('imgCookie',$imageNumber);
} else {
    //runs if the user already has the cookie
    $imagenumber=$_COOKIE['imgCookie'];
}
//run code for sending image to user

Also you can instruct a user's browser to cache your page for a really long time (up to a year, shown below), by specifying the cache duration in the header:

header('Cache-Control: max-age=31556926');

Source

Community
  • 1
  • 1
Eisa Adil
  • 1,743
  • 11
  • 16
  • 1
    Copy-paste answer http://stackoverflow.com/questions/17297950/cache-images-on-php-script – sergio Dec 25 '13 at 12:51
  • if the user change his logo, do I need to rewrite cookie ? Or, do I need to rewrite the cache? If yes, everytime when I load the image, I must test whether these images had been changed. – diligent Dec 25 '13 at 12:53
  • Something like that, but you can also destroy the cookies after a small duration, so it can update after that. – Eisa Adil Dec 25 '13 at 12:55
  • @sergio Sorry for not mentioning the source. – Eisa Adil Dec 25 '13 at 12:56