1、 在数据库新建字段font_md5
2、修改 /apps/admin/controller/content/ContentController.php
// 修改 add() 方法中的字体处理部分 foreach ($fontlist as $fontitem) { $currentData = $data; // 处理预览图 if (!$fontitem->issing) { $fontitem->yulanimg = "<p><img src=\"" . $fontitem->yulanimg . "\"/></p>"; } // 计算字体文件的md5值 if ($fontitem->fontpath && file_exists(ROOT_PATH . $fontitem->fontpath)) { $font_md5 = md5_file(ROOT_PATH . $fontitem->fontpath); // 检查是否已存在相同md5的字体文件 $check_result = $this->model->checkFontMd5($font_md5); if ($check_result) { $this->log('字体文件已存在,跳过上传:' . $fontitem->fontname . ',已存在字体:' . $check_result->title); // 删除已上传的临时文件 if ($fontitem->slimg && file_exists(ROOT_PATH . $fontitem->slimg)) { unlink(ROOT_PATH . $fontitem->slimg); } if ($fontitem->yulanimg) { preg_match('/<img[^>]+src="([^"]+)"/i', $fontitem->yulanimg, $matches); if (!empty($matches[1])) { $previewImg = ROOT_PATH . $matches[1]; if (file_exists($previewImg)) { unlink($previewImg); } } } if ($fontitem->fontpath && file_exists(ROOT_PATH . $fontitem->fontpath)) { unlink(ROOT_PATH . $fontitem->fontpath); } $errornum = $errornum + 1; continue; // 跳过当前文件,继续处理下一个 } $currentData["font_md5"] = $font_md5; // 提取字体名称 try { $font = Font::load(ROOT_PATH . $fontitem->fontpath); $font->parse(); // 获取字体名称 $fontName = $font->getFontName(); if (!empty($fontName)) { $fontitem->fontname = $fontName; } } catch (Exception $e) { $this->log('字体解析失败: ' . $e->getMessage()); // 如果解析失败,保持原来的文件名作为标题 } } $currentData["ico"] = $fontitem->slimg; $currentData["content"] = $fontitem->yulanimg; $currentData["enclosure"] = $fontitem->fontpath; $currentData["fontpath"] = $fontitem->fontpath; $currentData["title"] = $fontitem->fontname; // 其余代码保持不变...
3、修改\core\function\file.php
首先在文件顶部添加 php-font-lib 的引用:
use FontLib\Font;
use FontLib\TrueType\Collection;
修改 handle_upload
函数
if(in_array($file_ext, $font)) { // 检查文件存储路径 if (! check_dir($save_path . '/image' . '/' . date('Ymd'), true)) { return '存储目录创建失败!'; } // 提取字体真实名称 $fontName = $ctext; // 默认使用文件名 try { $fontObj = Font::load($file_path); $fontObj->parse(); $realFontName = $fontObj->getFontName(); if (!empty($realFontName)) { $fontName = $realFontName; } } catch (Exception $e) { // 如果解析失败,保持原来的文件名 } //字体大小 $size = 46; $txtlength = mb_strlen($fontName); // 使用真实字体名称计算长度 $imgwidth = 62 * $txtlength; $xnum = 0; //字数太多时,缩小字号 if($txtlength > 9) { $size = 46; $imgwidth = 32 * $txtlength; } //生成字体图片 - 使用真实字体名称 font_to_image($file_path, $fontName, $font_image_path, $size, $imgwidth, 100, 50, $xnum); $save_file = str_replace(ROOT_PATH, '', $font_image_path); //生成字符映射图片 - 使用真实字体名称 font_to_ysimage($file_path, $font_ysimage_path); $save_ysfile = str_replace(ROOT_PATH, '', $font_ysimage_path); $save_fontfile = str_replace(ROOT_PATH, '', $file_path); return $save_file.','.$save_ysfile.','.$save_fontfile.','.$fontName; // 返回真实字体名称 }
修改 font_preview
函数:
function font_preview($fontpath, $text, $size) { $font_image_path = DOC_PATH . STATIC_DIR . '/upload/cache' . '/' . time() . mt_rand(100000, 999999) . '.png'; // 提取字体真实名称 $fontName = $text; // 默认使用传入的文本 try { $fontObj = Font::load(DOC_PATH . $fontpath); $fontObj->parse(); $realFontName = $fontObj->getFontName(); if (!empty($realFontName)) { $fontName = $realFontName; } } catch (Exception $e) { // 如果解析失败,保持原来的文本 } //生成字体图片 - 使用真实字体名称 font_to_image_left(DOC_PATH . $fontpath, $fontName, $font_image_path, $size, getStringWidth($fontName, mb_strlen($fontName), $size), $size/66*120, $size/66*75); $save_file = str_replace(ROOT_PATH, '', $font_image_path); return $save_file; }