|
@@ -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")."/";
|
|
|
+
|
|
|
+ if (strpos($imgUrl, $baseToRemove) === 0) {
|
|
|
+
|
|
|
+ $result = substr($imgUrl, strlen($baseToRemove));
|
|
|
+ } else {
|
|
|
+ $result = $imgUrl;
|
|
|
+ }
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 替换富文本里面图片的
|
|
|
+ * @param $content
|
|
|
+ * @return array|string|string[]|null
|
|
|
+ */
|
|
|
+ public static function replaceContentImg($content)
|
|
|
+ {
|
|
|
+ $richTextContent = $content;
|
|
|
+ $imageBaseUrl = env("OSS_ENDPOINT")."/".env("BUCKET")."/";
|
|
|
+ $pattern = '/<img\s+src=[\'"]([^\'"]+)[\'"]/i';
|
|
|
+ $replacement = '<img src="' . $imageBaseUrl . '$1"';
|
|
|
+ $updatedContent = preg_replace($pattern, $replacement, $richTextContent);
|
|
|
+ return $updatedContent;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 补全富文本内容的图片地址
|
|
|
+ * @param $content
|
|
|
+ * @return array|string|string[]|null
|
|
|
+ */
|
|
|
+ public static function completionContentImg($content)
|
|
|
+ {
|
|
|
+
|
|
|
+ $richTextContent = $content;
|
|
|
+
|
|
|
+ $imageBaseUrl = env("OSS_ENDPOINT")."/".env("BUCKET")."/";
|
|
|
+
|
|
|
+ $pattern = '/<img\s+[^>]*src=[\'"]([^\'"]+\.[a-z]{3,4})[\'"][^>]*>/i';
|
|
|
+ $replacement = '<img src="' . $imageBaseUrl . '$1"';
|
|
|
+
|
|
|
+ $updatedContent = preg_replace($pattern, $replacement, $richTextContent);
|
|
|
+
|
|
|
+ return $updatedContent;
|
|
|
+ }
|
|
|
+}
|