显示来自其他域的图像而不显示域名 - PHP

我想在我的网站上显示图像但不想保存在我的主机上。我想展示这样的东西:


<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不是。


谢谢


小唯快跑啊
浏览 194回答 1
1回答

MYYA

你可以试试这个您的 php 页面 url -&nbsp;https://example.com/img.php?img=abc.png;img.php 代码(您可以根据自己的情况更改图像路径或 url)将图像 url&nbsp;https://picsum.photos/id/666/536/354视为另一个域https://example.org在您的情况下header('Content-Type:&nbsp;image/png'); readfile('https://picsum.photos/id/666/536/354');
打开App,查看更多内容
随时随地看视频慕课网APP