正则匹配图片地址获取第一张图片地址
此为函数 在模块或是全局Common文件夹中的function.php中
/**
* [getPic description]
* 获取文本中首张图片地址
* @param [type] $content [description]
* @return [type] [description]
*/
function getPic($content){
if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)) {
$str=$matches[3][0];
if (preg_match('/\/Uploads\/images/', $str)) {
return $str1=substr($str,7);
}
}
}
$content=I('post.body');//获取富文本编辑器内容
$info=getPic($content);//使用函数 返回匹配地址 如果不为空则声称缩略图
if(!$info==null){
$thumb=$info.'thumb240x160.png';
$image = new \Think\Image();//实例化图像处理,缩略图功能
$image->open($info);// 生成一个居中裁剪为240*160的缩略图
$unlink=$image->thumb(240, 160,\Think\Image::IMAGE_THUMB_CENTER)->save($thumb);
}else{
$thumb='';
}本文为Jsky原创文章,转载无需和我联系,但请注明来自Jsky博客 www.tjin.link
