Last active
January 9, 2016 09:16
-
-
Save iLiutaotao/cb21764f5887ebe253fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function saveImage($path) { | |
if(!preg_match('/\/([^\/]+\.[a-z]{3,4})$/i',$path,$matches)) //匹配图片 | |
die('Use image please'); | |
$image_name = strToLower($matches[1]); | |
$ch = curl_init ($path); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); | |
$img = curl_exec ($ch); | |
curl_close ($ch); | |
$fp = fopen("./download/".$image_name,'w'); //下载目录以及文件名 | |
fwrite($fp, $img); | |
fclose($fp); | |
} | |
$str = file_get_contents('http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'); | |
$array = json_decode($str); | |
$imgurl = $array->{"images"}[0]->{"url"};//图片URL | |
$copyright = $array->{"images"}[0]->{"copyright"};//图片描述版权 | |
if($imgurl){ | |
echo '<img src="'.$imgurl.'" alt="'.$copyright.'" /><br>'; | |
saveImage($imgurl); //下载抓到的图片 | |
exit(); | |
}else{ | |
exit('error'); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$page=isset($_GET['page'])?$_GET['page']:0;//从零开始 | |
$imgnums = 10; //每页显示的图片数 | |
$path="./download"; //图片保存的目录 | |
$handle = opendir($path); | |
$i=0; | |
while (false !== ($file = readdir($handle))) { | |
list($filesname,$ext)=explode(".",$file); | |
if($ext=="gif" or $ext=="jpg" or $ext=="JPG" or $ext=="GIF" ) { | |
if (!is_dir('./'.$file)) { | |
$array[]=$file;//保存图片名称 | |
++$i; | |
} | |
} | |
} | |
if($array){ | |
rsort($array);//修改日期倒序排序 | |
} | |
for($j=$imgnums*$page; $j<($imgnums*$page+$imgnums)&&$j<$i; ++$j){ | |
echo '<div>'; | |
echo $array[$j],'<br />'; | |
echo "<img src=".$path."/".$array[$j]."><br />"; | |
echo '</div>'; | |
} | |
$realpage = @ceil($i / $imgnums) - 1; | |
$Prepage = $page-1; | |
$Nextpage = $page+1; | |
if($Prepage<0){ | |
echo "上一页 "; | |
echo "<a href=?page=$Nextpage>下一页</a> "; | |
echo "<a href=?page=$realpage>最末页</a> "; | |
}elseif($Nextpage >= $realpage){ | |
echo "<a href=?page=0>首页</a> "; | |
echo " <a href=?page=$Prepage>上一页</a> "; | |
echo " 下一页"; | |
}else{ | |
echo "<a href=?page=0>首页</a> "; | |
echo "<a href=?page=$Prepage>上一页</a> "; | |
echo "<a href=?page=$Nextpage>下一页</a> "; | |
echo "<a href=?page=$realpage>最末页</a> "; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment