function geticonadd($content) {
if (preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)) {
$add = $matches[3][0]; //获取文件地址
$s = strripos($add, "/") - 8;
$name = substr($add, $s); //获取文件名
$name2 = substr($add, $s + 9);
//处理图片
$image = new \Think\Image();
$file = "../../ueditor/php/upload/image/{$name}"; //放到服务器时请去掉前面‘../’
if (file_exists($file)) {
$image->open($file);
$width = $image->width();
$height = $image->height();
if ($width > $height) {
$k = $height;
} else {
$k = $width;
}
// 先裁剪成正方形,再等比例缩放大小为100*100
$image->thumb($k, $k, \Think\Image::IMAGE_THUMB_CENTER)
->thumb(100, 100)
->save("Public/image/icon/{$name2}");
//$icon_add= '__PUBLIC__/upload/image/icon/{$name}';
return $name2;
} else {
return '';
}
} else {
return "default.jpg";
}
}
算法思路:
- 从ueditor的文章内容中匹配获取图片链接字符串,从字符串中提取图片名。
- 获取图片文件,进行裁剪。
- 另存icon到指定目录,返回icon的名称
- 如果文章内容中没有图片,则返回默认图片名。