杰克工作室 发表于 2024-2-10 15:40

实现简单的图片CDN

<pre>
   .htaccess文件内容:

   RewriteEngine On

   #RewriteCond %{HTTP_HOST}^t.boss.com$
   RewriteCond %{REQUEST_URI}!^$
   #RewriteCond %{REQUEST_FILENAME}^.*\.(jpg|jpeg|png|gif)$
   RewriteCond %{REQUEST_FILENAME}!-d
   RewriteCond %{REQUEST_FILENAME}!-f
   RewriteRule^(.*\.jpg)$   /sync_client.php?url=$1

</pre>

<pre>
sync_client.php文件内容:

&lt;?php
//var_dump($_GET[&#39;url&#39;]);
//echo &#39;&lt;img src=&quot;http://hazy.allalla.com/inc/logo.jpg&quot;&gt;&#39;;


$filepath &nbsp; = empty($_GET[&#39;url&#39;]) ? exit(&#39;no file&#39;) : $_GET[&#39;url&#39;];
$filepath &nbsp; = str_ireplace(&#39;\&#39;&#39;, &#39;&#39;, str_ireplace(&#39;&quot;&#39;, &#39;&#39;, $filepath));
$filepath &nbsp; = str_ireplace(&#39;\\&#39;, &#39;/&#39;, $filepath);
if(!is_file($filepath)){
&nbsp;&nbsp; &nbsp;$dir&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;= dirname($filepath);
&nbsp;&nbsp; &nbsp;$ext&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;= substr(strrchr( $filepath, &#39;.&#39; ), 1);
&nbsp;&nbsp; &nbsp;if(!in_array($ext, array(&#39;jpg&#39;, &#39;jpeg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;bmp&#39;))) exit(&#39;not allow file ext&#39;);&nbsp;

&nbsp;&nbsp; &nbsp;$domain&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;= &#39;http://qqqiushi.com/&#39;;<span style="color:#e74c3c">//源站,你的源站必须要允许此站点访问你的图片</span>
&nbsp;&nbsp; &nbsp;$url&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;= $domain . $filepath;

&nbsp;&nbsp; &nbsp;$content &nbsp; &nbsp;= file_get_contents_($url);
&nbsp;&nbsp; &nbsp;if(strlen($content) &lt; 20) exit(&#39;not allow content&#39;);

&nbsp;&nbsp; &nbsp;if(!is_dir($dir)) mkdir($dir, 0777, true);

&nbsp;&nbsp; &nbsp;file_put_contents($filepath, $content);
}
echo &#39;&lt;img src=&quot;/&#39; . $filepath . &#39;&quot; /&gt;&#39;;

//获取URL的内容
function file_get_contents_($url)
{
&nbsp; &nbsp; if(empty($url)) return false;
&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;$useragent = &#39;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19&#39;;
&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;if(function_exists(&#39;curl_init&#39;)) {
&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;$ch = curl_init();
&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;curl_setopt($ch, CURLOPT_URL, $url);
&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;curl_setopt($ch, CURLOPT_TIMEOUT,30);
&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;curl_setopt($ch, CURLOPT_REFERER, $url);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;curl_setopt($ch, CURLOPT_HEADER, 0);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$content = curl_exec($ch);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;curl_close($ch);
&nbsp;&nbsp; &nbsp;}else{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$content = file_get_contents($url);
&nbsp;&nbsp; &nbsp;}

&nbsp;&nbsp; &nbsp;return $content;
}

exit;
?&gt;

</pre>

<p>就这么简单的几句话!放到根目录:会自动将图片文件和源站图片文件的目录结构保持一致。</p>
页: [1]
查看完整版本: 实现简单的图片CDN