function grabImage( $url , $filename = '' ) {
if ( $url == '' ) {
return false; //如果 $url 为空则返回 false;
}
$ext_name = strrchr ( $url , '.' ); //获取图片的扩展名
if ( $ext_name != '.gif' && $ext_name != '.jpg' && $ext_name != '.bmp' && $ext_name != '.png' ) {
return false; //格式不在允许的范围
}
if ( $filename == '' ) {
$filename = time(). $ext_name ; //以时间戳另起名
}
//开始捕获
ob_start();
readfile( $url );
$img_data = ob_get_contents();
ob_end_clean();
$size = strlen ( $img_data );
$local_file = fopen ( $filename , 'a' );
fwrite( $local_file , $img_data );
fclose( $local_file );
return $filename ;
}