版权声明:转载请注明出处,谢谢! https://blog.csdn.net/dreamstone_xiaoqw/article/details/90756727
后端接口读取图片文件并输出到浏览器,既可以将资源文件的引用接口化,又能稍处理实现反爬虫,很不错呦。
方法
- 打开文件
- 添加 图片文件头
- echo输出
怎么样?简单吧?
初级代码
代码如下:
$file = "/home/yixzm/imgs/beauty.jpg";
header('Content-type:image/png');
echo file_get_contents($file);
OK!
封装为函数
function load_img()
{
$file = "/home/yixzm/imgs/beauty.jpg";
header('Content-type:image/png');
echo file_get_contents($file);
}
封装为模块类
<?php
namespace utils\load_img;
class load_img
{
public function __construct()
{ }
static function create()
{
static $instance;
if (!$instance) {
$instance = new load_img();
}
return $instance;
}
function load_img()
{
$file = "/home/yixzm/imgs/beauty.jpg";
header('Content-type:image/png');
echo file_get_contents($file);
}
}