function download_images($article_url = '', $image_path = 'tmp'){
// 擷取文章類容
$content = file_get_contents($article_url);
// 利用正規表達式得到圖檔連結
$reg_tag = '/<img.*?\"([^\"]*(jpg|bmp|jpeg|gif|png)).*?>/';
$ret = preg_match_all($reg_tag, $content, $match_result);
$pic_url_array = array_unique($match_result1[1]);
// 建立路徑
$dir = getcwd() . DIRECTORY_SEPARATOR .$image_path;
mkdir(iconv("UTF-8", "GBK", $dir), 0777, true);
foreach($pic_url_array as $pic_url){
// 擷取檔案資訊
$ch = curl_init($pic_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$fileInfo = curl_exec($ch);
$httpinfo = curl_getinfo($ch);
curl_close($ch);
// 擷取圖檔檔案字尾
$ext = strrchr($pic_url, '.');
$filename = $dir . '/' . uniqid() . $ext;
// 儲存圖檔資訊到檔案
$local_file = fopen($filename, 'w');
if(false !== $local_file){
if( false !== fwrite($local_file, $filecontent) ){
fclose($local_file);
}
}
}
}
複制
釋出者:全棧程式員棧長,轉載請注明出處:https://javaforall.cn/112590.html原文連結:https://javaforall.cn