我想在我的网站上显示图像但不想保存在我的主机上。我想展示这样的东西:<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');
- 1 回答
- 0 关注
- 193 浏览
添加回答
举报
0/150
提交
取消