|
@@ -0,0 +1,70 @@
|
|
|
+<?php
|
|
|
+namespace App\Tools;
|
|
|
+
|
|
|
+use function Hyperf\Support\env;
|
|
|
+
|
|
|
+class PublicData
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 拼接图片地址
|
|
|
+ * @param string $imgUrl
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function getImageUrl(string $imgUrl)
|
|
|
+ {
|
|
|
+ return env("OSS_ENDPOINT")."/".env("BUCKET")."/".$imgUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 截取图片地址
|
|
|
+ * @param string $imgUrl
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function saveImageUrl(string $imgUrl)
|
|
|
+ {
|
|
|
+ $baseToRemove = env("OSS_ENDPOINT")."/".env("BUCKET")."/";
|
|
|
+ // 使用 substr 和 strpos 函数找到并移除基础路径
|
|
|
+ if (strpos($imgUrl, $baseToRemove) === 0) {
|
|
|
+ // 如果URL以指定的基础路径开始,则移除该部分
|
|
|
+ $result = substr($imgUrl, strlen($baseToRemove));
|
|
|
+ } else {
|
|
|
+ $result = $imgUrl; // 如果URL不以指定的基础路径开始,则保持原样
|
|
|
+ }
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 替换富文本里面图片的
|
|
|
+ * @param $content
|
|
|
+ * @return array|string|string[]|null
|
|
|
+ */
|
|
|
+ public static function replaceContentImg($content)
|
|
|
+ {
|
|
|
+ $richTextContent = $content;
|
|
|
+ $imageBaseUrl = env("OSS_ENDPOINT")."/".env("BUCKET")."/";// 获取环境变量中的图片基础URL
|
|
|
+ $pattern = '/<img\s+src=[\'"]([^\'"]+)[\'"]/i'; // 匹配<img>标签中的src属性值
|
|
|
+ $replacement = '<img src="' . $imageBaseUrl . '$1"'; // 替换为带基础URL的路径
|
|
|
+ $updatedContent = preg_replace($pattern, $replacement, $richTextContent);
|
|
|
+ return $updatedContent;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 补全富文本内容的图片地址
|
|
|
+ * @param $content
|
|
|
+ * @return array|string|string[]|null
|
|
|
+ */
|
|
|
+ public static function completionContentImg($content)
|
|
|
+ {
|
|
|
+ // 假设这是从数据库获取的富文本内容
|
|
|
+ $richTextContent = $content;
|
|
|
+ // 从环境变量或配置文件中读取图片基础URL
|
|
|
+ $imageBaseUrl = env("OSS_ENDPOINT")."/".env("BUCKET")."/"; // 这里应该用getenv('IMAGE_BASE_URL')等方法获取实际值
|
|
|
+ // 使用正则表达式匹配<img>标签中的src属性值,并替换为完整URL
|
|
|
+ $pattern = '/<img\s+[^>]*src=[\'"]([^\'"]+\.[a-z]{3,4})[\'"][^>]*>/i';
|
|
|
+ $replacement = '<img src="' . $imageBaseUrl . '$1"'; // 注意这里的$1表示匹配到的第一个分组,即原始的src值
|
|
|
+ // 执行替换
|
|
|
+ $updatedContent = preg_replace($pattern, $replacement, $richTextContent);
|
|
|
+ // 输出更新后的内容
|
|
|
+ return $updatedContent;
|
|
|
+ }
|
|
|
+}
|