利用php轻松解决图片防盗链显示问题

最近有个项目需要使用第三方图片,可对方设置了图片防盗链,为此,找到了这个php代码解决图片防盗链显示问题!

PHP代码如下:

<?php
 error_reporting(E_ERROR | E_PARSE );
 @ini_set('max_execution_time', '0');
 @ini_set("memory_limit",'-1');
 $url = $_GET["url"];
 if (!empty($url) && substr($url,0,4)=='http') {
     $dir = pathinfo($url);
     $host = $dir['dirname'];
     $ext = $dir['extension'];
     $refer = $host.'/';
     $ch = curl_init($url);
     curl_setopt ($ch, CURLOPT_REFERER, $refer);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
     curl_setopt($ch, CURLOPT_TIMEOUT, 30);
     curl_setopt($ch, CURLOPT_HEADER,0);
     curl_setopt($ch, CURLOPT_POST, 0);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
     curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
     $data = @curl_exec($ch);
     curl_close($ch);
     $types = array(
         'gif'=>'image/gif',
         'jpeg'=>'image/jpeg',
         'jpg'=>'image/jpeg',
         'jpe'=>'image/jpeg',
         'png'=>'image/png',
     );
     $type = $types[$ext] ? $types[$ext] : 'image/jpeg';
     header("Content-type: ".$type);
     echo $data;
 }

将代码保存为img.php(名字可以随便改,不过后面引用的时候一定要用你改的名字)

图片引用方法:

/img.php?url=https://www.heilo.cn/wp-content/uploads/2021/03/cropped-嘿咯.png