Files
2025-05-22 13:53:49 +08:00

71 lines
1.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// 这是系统自动生成的公共文件
if (!function_exists('image_domain_concat')) {
/**
* 图片域名拼接
* @param $path
* @return string
*/
function image_domain_concat($path)
{
if (empty($path)) {
return '';
}
$domain = env('OPENAPI.RESOURCE_IMAGES_DOMAIN');
if (empty($domain)) {
return $path;
}
return rtrim($domain, '/') . '/' . ltrim($path, '/');
}
}
if (!function_exists('video_domain_concat')) {
/**
* 视频域名拼接
* @param $path
* @return string
*/
function video_domain_concat($path)
{
if (empty($path)) {
return '';
}
$domain = env('OPENAPI.RESOURCE_VIDEOS_DOMAIN');
if (empty($domain)) {
return $path;
}
return rtrim($domain, '/') . '/' . ltrim($path, '/');
}
}
if (!function_exists('html_image_replace')) {
/**
* 替换html中的图片路径
* @param $html
* @return string
*/
function html_image_replace($html)
{
if (empty($html)) {
return '';
}
return preg_replace_callback('/<img[^>]+src\s*=\s*([\'"])((?:(?!\1).)*)\1[^>]*>/i',
function($matches) {
$src = $matches[2];
if (!empty($src) && !str_starts_with($src, 'http')) {
// 保留原始标签只替换src属性
return str_replace($src, image_domain_concat($src), $matches[0]);
}
return $matches[0];
},
$html
);
}
}