经过了多次修改和限制,微信的文章里的图片并不是那么容易让你下载的了,而大多wordpress程序都无法读取微信的文章,原因其实很简单,因为大多数获取图片的函数都是按照有图片后缀名的方法写的正则表达式。
这里有一段代码,正则是直接提取img标签里的网址,而不是通过匹配后缀名,这样来的比较大众一点。
//获取一张图片作为首页缩略图 function thumb_img($soContent) { $soImages = '~<img [^\>]*\ />~'; preg_match_all($soImages, $soContent, $thePics); $allPics = count($thePics[0]); $site = get_permalink(); if ($allPics > 0) { echo "<div class='wpztw_thumbnail'>"; echo '<a href="'.$site.'">'; echo $thePics[0][0]; echo '</a>'; echo "</div>"; } else { //否则就用一张随机图片作为缩略图 $random = mt_rand(1, 10); echo '<div class="thumbnail"><a href="'.$site.'"><img src="'.get_bloginfo('template_url').'/img/random/tb'.$random.'.jpg" alt="'.trim(strip_tags($post -> post_title)).'" height=100 /></a></div>'; } }
而dedecms也是一样,我们看到“保存远程图片到本地”这个功能勾选以后,对微信的文章里的图片依然无法抓取保存,一个需要修改获取文章内容的图片正则表达式代码
位置:找到DEDECMS文件 /dede/inc/inc_archives_functions.php,找到函数function GetCurContent($body,&$litpic,$autolitpic)和function GetCurContent($body)找到
大概在11行和89行把匹配远程图片的规则
preg_match_all("/(src)=[\"|'| ]{0,}(http:\/\/([^>]*)\.(gif|jpg|png))/isU",$body,$img_array);
改成
preg_match_all("/src=[\"|'|\s]{0,}(http:\/\/([^>]*))(\"|'|\s)/isU",$body,$img_array);
最后还需要改一下下载部分的代码,具体要看每个人的函数位置,自己找了
$url="http://img04.sogoucdn.com/net/a/04/link?appid=100520033&url=http%3A%2F%2Fmmbiz.qpic.cn%2Fmmbiz_jpg%2FF1hLEK71icuDUfEY5a68K6C1f0yujogtpqJEuUcT3lWcqQR0xaslW0KhjOwaR3hccy7UDObIZPkzktIbI9ommmA%2F0%3Fwx_fmt%3Djpeg";
//$url = str_replace("http:/","http://",$url);
$dir = pathinfo($url);
$host = $dir['dirname'];
$refer = $host.'/';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_REFERER, $refer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$data = curl_exec($ch); curl_close($ch);
header("Content-type: image/jpeg");
print( $data );//打印出来,看看对不对,在写入数据
未经允许不得转载:www.gulangu.com孤狼工作室 » 获取微信文章内一张图片作为缩略图(附代码)