我想在我的网站上显示图像但不想保存在我的主机上。我想展示这样的东西:
<img src="https://example.com/img/pic.jpg">
或者
<img src="https://example.com/img/image.php?img=pic.jpg">
代替:
<img src="https://example.org/img/pic.php">
我一直在使用PHP尝试file_get_contents()和CURL等,但没有一个是作品。
下面的代码已经工作,将图像从 URL 转换为,data:image但加载时间太长。
$url = 'https://otherdomain.com/img/'.$_GET['img'];
$allow = ['gif', 'jpg', 'png']; // allowed extensions
$img = file_get_contents($url);
$url_info = pathinfo($url);
// if allowed extension
if(in_array($url_info['extension'], $allow)) {
// Format the image to data:image : data:{mime};base64,{img_data_base64};
$re = 'data:image/'. $url_info['extension'] .';base64,'. base64_encode($img);
}
else $re = 'Invalid extension: '. $url_info['extension'];
header('Content-Type: application/json');
echo json_encode($src); // output $re data
我想要正确的 URL 格式,正如我上面提到的(https://example.com/img/image.php?img=pic.jpg)而不在我的服务器上保存图像。
注意: 这https://example.com是我的域,但https://example.org不是。
谢谢
MYYA