你也考虑过base64吗?比如Display PNG image as response to jQuery AJAX request
在 PHP 上:
<?php
$img = file_get_contents('someimage.png');
$imb64 = base64_encode($img);
header('Content-Type: image/png');
echo $imb64;
exit;
?>
在 Javascript 上:
$.ajax({
type: "POST",
url:'myimage.php',
contentType: "image/png",
data: {},
success: function(data){
$('#myimage').css('background-image', 'url(' + 'data:image/png;base64,' + data+ ')');
}
});
当然你需要改变上面的,它只是一个简单的例子。?未在编写/修改为基本基础时进行测试
当然你不需要使用jquery,这只是为了缩短例子..你应该可以使用你的xhr
资源:
https://en.wikipedia.org/wiki/Data_URI_scheme
https://css-tricks.com/data-uris/
此方法非常方便,允许您控制热链接并显示丢失文件的占位符以及使用电子标签控制缓存的能力...
---------我感觉很好-------------
function cacheByModified($file) {
if ($GLOBALS['VTIS']['cache']!==false) {
$ts = filemtime($file);
$gmt_mtime = gmdate('r', $ts);
header('ETag: "'.md5($ts.$file).'"');
header('Last-Modified: '.$gmt_mtime);
header('Cache-Control: public');
// IF HAS HEADERS
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $gmt_mtime || str_replace('"', '', stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])) == md5($ts.$file)) {
header('HTTP/1.1 304 Not Modified');
if (extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler")) {
ini_set("zlib.output_compression", 1);
}
exit;
}
}
}
}
// Cacher.. send normal headers, check if found.. if are.. return 304 and exit.
cacheByModified("filename.ext or some other unique id here per file..");
// Enable gzip before sending the file
if (extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler")) {
ini_set("zlib.output_compression", 1);
}
// Obtain the file itself
$img = file_get_contents('someimage.png');
$imb64 = base64_encode($img);
// Send main header and output
header('Content-Type: image/png']);
echo $imb64;
exit;
上面/附加的代码是我缓存功能的快速提取,我没有在项目之外对其进行测试,因为只是快速执行此操作。但如果我没记错的话应该可以工作。