1 <?php 2 function getImage($url, $filename=""){ 3 if($url == ""){ 4 return false; 5 } 6 $ext = strrchr($url, "."); 7 if($ext!=".gif" && $ext!=".jpg" && $ext!=".bmp" && $ext!=".png"){ 8 echo "格式不支持"; 9 return false; 10 } 11 if($filename == ""){ 12 $filename = time().$ext; 13 } 14 ob_start(); 15 readfile($url); 16 $img = ob_get_contents(); 17 ob_end_clean(); 18 $size = strlen($img); 19 $fp = fopen($filename, "a"); 20 fwrite($fp, $img); 21 fclose($fp); 22 return $filename; 23 } 24 $result = getImage("图片路径","图片名称"); 25 if($result){ 26 echo ‘<img src="‘.$result.‘">‘; 27 }else{ 28 echo "抓取失败"; 29 } 30 ?>
时间: 2024-10-12 07:03:33