Files
orico-official-website/app/index/common.php
2025-05-06 16:25:53 +08:00

118 lines
3.2 KiB
PHP

<?php
// 这是系统自动生成的公共文件
if (!function_exists('str_contains')) {
/**
* 检查字符串是否包含子字符串
* @param string $haystack 要搜索的字符串
* @param string $needle 要搜索的子字符串
* @return bool
*/
function str_contains(string $haystack, string $needle): bool
{
return \think\helper\Str::contains($haystack, $needle);
}
}
if (!function_exists('rgb_or_image')) {
/**
* 判断字符串是否为RGB颜色值或图片链接
* @param string $str 要判断的字符串
* @return string IMAGE|RGB|''
*/
function rgb_or_image(string $str): string
{
if (
\think\helper\Str::startsWith($str, ['http', 'https']) ||
\think\helper\Str::endsWith($str, ['.png', '.jpg', '.jpeg', '.gif', '.tif', '.svg', '.webp', '.bmp']) ||
preg_match('/^data:image\/[jpg|jpeg|png|svg\+xml];base64,.+$/', $str)
) {
return 'IMAGE';
}
if (
preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $str) ||
preg_match('/^rgb\(\d{1,3},\s*\d{1,3},\s*\d{1,3}\)$/', $str) ||
preg_match('/^rgba\(\d{1,3},\s*\d{1,3},\s*\d{1,3},\s*\d*(\.\d+)?\)$/', $str)
) {
return 'RGB';
}
return '';
}
}
if (!function_exists('style')) {
/**
* 输出style行内样式 - 避免编辑器因css校验规则提示错误
* @param array|string $styles 样式数组
* @return string
*/
function style(array|string $styles): string
{
$css = '';
if (is_array($styles)) {
$key = array_keys($styles);
foreach ($key as $v) {
if (empty($styles[$v])){
continue;
}
$css .= $v . ':' . $styles[$v] . ';';
}
} else {
$css = $styles;
}
if ($css == '') {
return '';
}
return 'style="' . $css . '"';
}
}
if (!function_exists('thumb')) {
/**
* 获取缩略图
* @param string $url 图片地址
* @return string
*/
function thumb(string $url): string
{
if (empty($url)) {
return '';
}
if (
str_contains($url, '_thumb') ||
\think\helper\Str::startsWith($url, ['http://', 'https://']) ||
!\think\helper\Str::endsWith($url, ['.png', '.jpg', '.jpeg', '.gif', '.tif', '.svg', '.webp', '.bmp'])
) {
return $url;
}
$idx = mb_strripos($url, '.', 0, 'utf-8');
if ($idx === false) {
return $url;
}
$len = mb_strlen($url, 'utf-8');
return mb_substr($url, 0, $idx, 'utf-8') . '_thumb' . mb_substr($url, $idx, $len - $idx, 'utf-8');
}
}
if (!function_exists('get_path_from_img_tag'))
{
/**
* 从img标签中获取图片路径
* @param string $img
* @return string
*/
function get_path_from_img_tag(string $img): string
{
$match = preg_match('/<img[^>]+src=[\'"](.*?)[\'"][^>]+>/i', rawurldecode($img), $matches);
if ($match) {
return $matches[1];
}
return '';
}
}