仅在网页上显示时在 php 中临时存储图像 [英] Temporary store image in php only while displaying on web page

查看:37
本文介绍了仅在网页上显示时在 php 中临时存储图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从其他站点获取图像并在我的站点上显示.我只想在页面上显示时将该图像临时存储在我的身边,以便用户认为图像来自我的网站.这可能吗?如何?提前致谢.

I want to fetch images from other site and display on my site. I want to store that images in my side temporary only at time of showing on page , so that user think images are coming from my site.is it possible? How? Thanks in advance.

例如:-想象一下,在服务器中生成了一些图表(一些网络监控图表,如吸烟),这些图表以 .png 格式存储在文件夹中.

ex:- imagine there are some graphs generated in a server (some network monitoring graphs like smokeping )and those graphs are stored in a folder as .png format.

例如:- imagine_this_url_has_a_graph(但这是只是一个页面)

ex:- imagine_this_url_has_a_graph (but this is just a page)

以便用户希望在另一个网站上显示这些图表.

so that user want to show those graphs in another web site.

推荐答案

一个简单的缓存系统就可以做到这一点.给定一个 HTML 文件,例如:

A simple caching system would do this. Given an HTML file like:

<body>
 <img src="imagesource.php?file=1" />
</body>

您可以像这样创建图像数据源 (imagesource.php):

You could create your image data source (imagesource.php) like:

<?php
$files = array(
   'http://somesite.com/image_to_cache.gif',
   'http://somesite.com/image_to_cache2.gif'
);

if(!isset($_REQUEST['file']) || !isset($files[$_REQUEST['file']])) {
   header('HTTP/1.0 404 Not Found');
   exit;
}

// do we already have the cached file?
$requested_file = $files[$_REQUEST['file']];
$cache_dir = '/tmp/cached_images';
$cache_file = md5($requested_file);
$cache_path = $cache_dir . '/' . $cache_file;

header('Content-Type: image/gif');

if(file_exists($cache_path)) {
   readfile($cache_path);
   exit;
}

//need to create the cache.
$data = file_get_contents($requested_file);
file_put_contents($cache_path, $data);

echo $data;

如果远程图像尚未下载,这将在本地缓存它们,并在请求时将它们输出到浏览器.

This would cache the remote images locally if they haven't already been downloaded, and output them to the browser when requested.

这篇关于仅在网页上显示时在 php 中临时存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆