我想在我的網(wǎng)站上顯示圖像但不想保存在我的主機(jī)上。我想展示這樣的東西:<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等,但沒有一個(gè)是作品。下面的代碼已經(jīng)工作,將圖像從 URL 轉(zhuǎn)換為,data:image但加載時(shí)間太長(zhǎng)。$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)而不在我的服務(wù)器上保存圖像。注意: 這https://example.com是我的域,但https://example.org不是。謝謝
1 回答

MYYA
TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊
你可以試試這個(gè)
您的 php 頁(yè)面 url - https://example.com/img.php?img=abc.png;
img.php 代碼(您可以根據(jù)自己的情況更改圖像路徑或 url)將圖像 url https://picsum.photos/id/666/536/354視為另一個(gè)域https://example.org在您的情況下
header('Content-Type: image/png'); readfile('https://picsum.photos/id/666/536/354');
- 1 回答
- 0 關(guān)注
- 213 瀏覽
添加回答
舉報(bào)
0/150
提交
取消