|
@@ -0,0 +1,73 @@
|
|
|
|
+<?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)
|
|
|
|
+ {
|
|
|
|
+ $searchUrl = env("OSS_ENDPOINT")."/".env("BUCKET")."/";// 获取环境变量中的图片基础URL
|
|
|
|
+ $quotedSearchUrl = preg_quote($searchUrl, '/');
|
|
|
|
+ // 正则表达式模式,用于匹配img标签中的src属性,并定位$searchUrl的位置
|
|
|
|
+ $pattern = '/(<img[^>]+src=")(?:' . $quotedSearchUrl . ')?([^"]*")/';
|
|
|
|
+ // 执行替换,确保全部匹配都被处理
|
|
|
|
+ $result = preg_replace($pattern, '$1$2', $content);
|
|
|
|
+ return $result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 补全富文本内容的图片地址
|
|
|
|
+ * @param $content
|
|
|
|
+ * @return array|string|string[]|null
|
|
|
|
+ */
|
|
|
|
+ public static function completionContentImg($content)
|
|
|
|
+ {
|
|
|
|
+ $prefix = env("OSS_ENDPOINT")."/".env("BUCKET")."/";// 获取环境变量中的图片基础URL
|
|
|
|
+ // 使用preg_quote来转义$prefix中的特殊字符
|
|
|
|
+ $quotedPrefix = preg_quote($prefix, '/');
|
|
|
|
+ // 正则表达式模式,用于匹配img标签中的src属性
|
|
|
|
+ $pattern = '/(<img[^>]+src=")([^"]*")/';
|
|
|
|
+
|
|
|
|
+ // 替换逻辑:在原src值前加上$prefix
|
|
|
|
+ $replacement = '$1' . $prefix . substr('$2', 0, -1); // 移除最后一个",然后加上前缀和"
|
|
|
|
+
|
|
|
|
+ // 执行替换
|
|
|
|
+ $result = preg_replace($pattern, "$1$prefix$2", $content);
|
|
|
|
+ return $result;
|
|
|
|
+ }
|
|
|
|
+}
|