为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
小唯快跑啊 2021-11-26 16:52:39
我想在我的网站上显示图像但不想保存在我的主机上。我想展示这样的东西:<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 extensionif(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不是。谢谢
查看完整描述

1 回答

?
MYYA

TA贡献1868条经验 获得超4个赞

你可以试试这个

您的 php 页面 url - https://example.com/img.php?img=abc.png

img.php 代码(您可以根据自己的情况更改图像路径或 url)将图像 url https://picsum.photos/id/666/536/354视为另一个域https://example.org在您的情况下

header('Content-Type: image/png');
readfile('https://picsum.photos/id/666/536/354');


查看完整回答
反对 回复 2021-11-26
  • 1 回答
  • 0 关注
  • 159 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信