How to cache dynamic izobrazhaya using PHP?

Good afternoon.

To generate the images (status bar, signature) used data from MySQL and templates locally placed images and used fonts.

The process of generating the images in this code snippet:

$im = imagecreatefrompng("./bg_2.png");
$over = imagecreatefrompng("./over.png");
$flag = imagecreatefrompng("/dist/img/flags/".$country.".png");
$status = imagecreatefrompng("./".$online.".png");

$orange = imagecolorallocate($im, 205,66,43);
$white = imagecolorallocate($im, 255,255,255);
$green = imagecolorallocate($im, 0,204,51);
$red = imagecolorallocate($im, 255,61,61);
$blue = imagecolorallocate($im, 51,102,255);
$navy = imagecolorallocate($im, 75,100,111);

imagecopy($im, $over, 0, 0, 0, 0, 468, 60);
imagecopy($im, $flag, 10, 12, 0, 0, 16, 11);
imagecopy($im, $status, 350, 0, 0, 0, 120, 60);

imagettftext($im, 16, 0, 35, 25, $white, $font, $string);
imagettftext($im, 10, 0, 10, 50, $white, $font2, $string5);

header("Content-type: image/png");


The essence of the question how to cache the resulting image (Memcached or alternatives), given that the refresh interval data in a DB - 5 minutes?
June 26th 19 at 14:12
3 answers
June 26th 19 at 14:14
Just wait for the disc all by itself zacchiroli it's static
I do not quite understand. When accessing the file which generates the image it makes a request to the database. - esther21 commented on June 26th 19 at 14:17
: well, save the image to disk and if more than five minutes passed then update it - Eulalia22 commented on June 26th 19 at 14:20
June 26th 19 at 14:16
You have input parameters 4 in fact, the country, the status (online/offline) and two rows.
Line you likely rarely change, the country does not change, but the status often enough.
When you change the strings generates two pictures, one offline, the other online and kept separate.
Every 5 minutes - looked in the database and copied the picture with the right status in the right place (or renamed - but then need to somehow remember the previous name/location of the file).
So it was clear:
Constantly changing the current number of players , the status and version of the server, the others much less frequently. The status (online\offline) is a pre-prepared image. - esther21 commented on June 26th 19 at 14:19
Then my offer is for true. Overall, I still to every 5 minutes to generate the pictures and putting them on disk (if not many). Well or generates a request picture. Kind of got a request if there is a picture fresh 5 minutes, give it, if not - generis. The main thing is several generations at the same time don't run if they are resource-intensive. The text, by the way, it is possible using imagemagick to overlay, and can quickly recover.
For memcache not an expert, but read that the OS will the files that you access frequently to cache itself. - Eulalia22 commented on June 26th 19 at 14:22
: Generate and store locally the images are not interesting probably a lot of them, on every server 5 various, a number of servers around 450 and growing.
But Memcache would be more interesting because of the 8 Gb RAM used ~1.5-2 Gb - esther21 commented on June 26th 19 at 14:25
copy kartinki to disk just forget it method. It is possible to cache the contents of the generated image, and to give on request, forming the header. Menkes copes with it perfectly. To configure kakoynit to. htaccess in nginx no problem with the file name on a key in the cache. Ie redirect from url like /imgs/akjdhsfbajdsah.jpg on the infobar.php?key=akjdhsfbajdsah which will jerk the cache if it exists and be given the image. - saul.Trantow commented on June 26th 19 at 14:28
June 26th 19 at 14:18
As a suggestion, you can issue
The HTTP Expires header that specifies when to expire 5 minutes. Then the web customers will be able to know when to update the picture.
header("Content-type: image/png");
header('Pragma: public');
header('Cache-Control: max-age=250');
header('Expires: '. gmdate('D, D M Y H:i:s \G\M\T', time() + 250))
- esther21 commented on June 26th 19 at 14:21
: like? - Eulalia22 commented on June 26th 19 at 14:24

Find more questions by tags ImagesPHPMemcachedCaching