zoukankan      html  css  js  c++  java
  • 写一个函数,获取一篇文章内容中的全部图片,并下载

    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);
                }
            }
        }
    
    }
  • 相关阅读:
    【luogu4719】动态DP模板 [动态DP]
    【2019.9.22】
    [JSOI2010]连通数[tarjan缩点]
    【2019.9.16】Za
    【2019.9.18】Za
    [USACO14OPEN]GPS的决斗Dueling GPS's [最短路]
    【CF891C】Envy [最小生成树]
    【2019.9.17】Za
    【2019.9.17】
    【luogu3403】跳楼机 [同余最短路]
  • 原文地址:https://www.cnblogs.com/lxwphp/p/10243241.html
Copyright © 2011-2022 走看看