我想在我的网站上显示图像,但不想保存在我的主机上。 我想向它展示这样的东西:
<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">
我尝试过使用 PHPfile_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
但不是。
谢谢
你可以试试这个,
您的 php 页面网址 - https://example.com/img.php?img=abc.png;
img.php 代码(您可以根据自己更改图像路径或 URL( 将图片网址 https://picsum.photos/id/666/536/354 视为另一个域名 https://example.org 在您的情况下
header('Content-Type: image/png');
readfile('https://picsum.photos/id/666/536/354');