简体   繁体   English

缓存在php上生成的动态图像

[英]Caching dynamic image generated on php

My php script parses user's profile on other site, takes some info and generates .png image with it for certain user (script.php?username=). 我的php脚本在其他网站上解析用户的个人资料,获取一些信息,并为某个用户生成.png图像(script.php?username =)。 Each time page with these images is loaded, script runs again and again. 每次加载这些图像的页面时,脚本会一次又一次地运行。 How can I cache images and only run script again if information it outputs was changed? 如果更改了输出的信息,我该如何缓存图像并仅再次运行脚本? It would save pretty much resources. 它可以节省相当多的资源。

Cache the image to disk and let Apache take care of the rest. 将映像缓存到磁盘并让Apache处理剩下的事情。

First, redo your image URI's so they are similar to: 首先,重做您的图像URI,使它们类似于:

<img src="/images/profiles/johnsmith.png" />

Then, in the /images/profiles/ , place a .htaccess file with: 然后,在/images/profiles/ ,放置一个.htaccess文件:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.png$ /script.php?username=$1 [QSA,L]
</IfModule>

Then have your script write the resulting png to disk before serving it to the user. 然后让您的脚本将生成的png写入磁盘,然后再将其提供给用户。 Next time the image is requested, it will get it directly from the web server. 下次请求图像时,它将直接从Web服务器获取。

When the user's profile info changes, simply delete the existing .png file from the server and your script will be run the next time the image is requested. 当用户的配置文件信息发生更改时,只需从服务器中删除现有的.png文件,您的脚本将在下次请求映像时运行。

If you don't want the web server to be able to write within the web root, write outside of it and have a cron job move them. 如果您不希望Web服务器能够在Web根目录中写入,请在其外部写入并让cron作业移动它们。

Caching images is probably the easiest caching problem to solve as it is just a matter of saving a local copy of any image to your server after it is generated and checking for a local copy before running the code that generates it. 缓存图像可能是最容易解决的缓存问题,因为它只是在生成后将任何图像的本地副本保存到服务器并在运行生成它的代码之前检查本地副本。

Something like:- 就像是:-

if(file_exists(image12345.png && !checkIfDataChanged()){
    serve cached file;
} else {
    generate new file;
    save new file to image12345.png;
    serve cached file;
}

This pseudo-code ofcourse, but it should be easy enough for you to translate it into PHP. 这个伪代码,但它应该很容易将它翻译成PHP。

设置php标头以通知浏览器资源被缓存:

header("Last-Modified: " . date("D, d M Y H:i:s", getlastmod()));

here, you can find how can you cache images using php. 在这里,你可以找到如何使用PHP缓存图像。 You can call these script when you find update from database, otherwise every time image will be load from cache. 当您从数据库中找到更新时,可以调用这些脚本,否则每次都将从缓存加载图像。

// put this above any php image generation code:
session_start(); 
header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,strtotime(" 2 day")));

https://dtbaker.net/blog/web-development/2009/06/how-to-cache-images-generated-by-php/ https://dtbaker.net/blog/web-development/2009/06/how-to-cache-images-generated-by-php/

You will need to parse the user's profile again on each request to find out if something has changed. 您需要在每个请求中再次解析用户的配置文件,以确定是否有更改。

You can then throw all the information into some kind of hash like md5($name.$location) and store this information anywhere. 然后,您可以将所有信息放入某种哈希值,如md5($name.$location) ,并将此信息存储在任何位置。 If you now get a request for an image, parse the user's profile, create the hash again and look this hash up. 如果您现在获得图像请求,请解析用户的配置文件,再次创建哈希并查看此哈希值。 If you have stored it, you previously created the image and can just output it. 如果您已经存储它,您之前创建了图像并可以输出它。 If the hash is different, the user's information has changed, as well and you will have to recreate the image. 如果哈希值不同,用户的信息也会发生变化,您将不得不重新创建图像。

You can also apply some heuristic like the fact that a user might only change his profile once an hour, or even only once a day. 您还可以应用一些启发式操作,例如用户每小时只能更改一次配置文件,甚至每天只更改一次。 With this assumption you can compare the creation date of the user's image and only parse the user's information if the image is older than an hour (or day). 通过此假设,您可以比较用户图像的创建日期,并仅在图像超过一小时(或一天)时解析用户的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM