@
Colorful 很久以前写的
php
<?php
$url = $_GET["url"];
$black_host = array('
ws2.sinaimg.cn','
ws3.sinaimg.cn','
ws4.sinaimg.cn');//黑名单域名
if(in_array(parse_url($url)['host'],$black_host)){
echo 'The domain is in the blacklist';
exit;
}
$dir = pathinfo($url);
$host = $dir['dirname'];
$refer = $host.'/';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_REFERER, $refer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//激活可修改页面,Activation can modify the page
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$ext = strtolower(substr(strrchr($img,'.'),1,10));
$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;
?>
js
function sinaimg() {
$('img').each(function() {
var domain = this.src.split('.');
if(domain[1] == 'sinaimg'){
img = new Image();
img.src = this.src;
if(img.complete||img.naturalWidth) {
//已缓存
}else{
//未缓存
this.src = '/sinaimg.php?url=' + this.src;//临时使用 php 来让图片显示
var ifrid = 'ifr_' + Math.random().toString(36).substr(2);//随机生成一个 ID
$('html').append('<iframe class="hide" src="" id ="' + ifrid +'"></iframe>');//创建一个 iframe
$('#'+ifrid).attr('src','javascript:"<script>location.replace(\''+img.src+'\')<\/script>"');//让 iframe 打开指定 url
}
}
});
};