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

  • 本文关键字:显示 域名 PHP 其他 图像 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">

我尝试过使用 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');

最新更新