mirror of
https://gitee.com/ulthon/ulthon_information.git
synced 2026-03-03 16:24:28 +08:00
优化水印处理
This commit is contained in:
@@ -50,6 +50,11 @@ class Image
|
||||
|
||||
public static function handelWatermarkSave($src, $watermarked_file_save_name = null)
|
||||
{
|
||||
// 判断src是否是base64
|
||||
if (strpos($src, 'data:image') === 0) {
|
||||
$src = static::base64ToImage($src);
|
||||
}
|
||||
|
||||
$src_file = new SplFileInfo($src);
|
||||
$watermark_text = get_system_config('watermark_text', 'phpreturn.com');
|
||||
|
||||
@@ -60,7 +65,9 @@ class Image
|
||||
}
|
||||
|
||||
// 如果以jpe结尾,转换为jpg
|
||||
if (str_ends_with($watermarked_file_save_name, '.jpe')) {
|
||||
$watermarked_file_save_name = str_replace('.jpe', '.jpg', $watermarked_file_save_name);
|
||||
}
|
||||
|
||||
$watermarked_file = App::getRootPath() . '/public' . $watermarked_file_save_name;
|
||||
|
||||
@@ -83,4 +90,38 @@ class Image
|
||||
|
||||
return $watermarked_file_save_name;
|
||||
}
|
||||
|
||||
public static function base64ToImage($base64)
|
||||
{
|
||||
$ext_name = 'jpg';
|
||||
|
||||
if (strpos($base64, 'data:image/jpeg;base64,') === 0) {
|
||||
$ext_name = 'jpg';
|
||||
} elseif (strpos($base64, 'data:image/jpg;base64,') === 0) {
|
||||
$ext_name = 'jpg';
|
||||
} elseif (strpos($base64, 'data:image/png;base64,') === 0) {
|
||||
$ext_name = 'png';
|
||||
} elseif (strpos($base64, 'data:image/gif;base64,') === 0) {
|
||||
$ext_name = 'gif';
|
||||
}
|
||||
|
||||
$base64 = str_replace('data:image/jpeg;base64,', '', $base64);
|
||||
$base64 = str_replace('data:image/jpg;base64,', '', $base64);
|
||||
$base64 = str_replace('data:image/png;base64,', '', $base64);
|
||||
$base64 = str_replace('data:image/gif;base64,', '', $base64);
|
||||
$base64 = str_replace(' ', '+', $base64);
|
||||
|
||||
$image = base64_decode($base64);
|
||||
|
||||
$file_name = '/upload/base64/' . md5($base64) . '.' . $ext_name;
|
||||
$file_path = App::getRootPath() . '/public' . $file_name;
|
||||
|
||||
if (!is_dir(dirname($file_path))) {
|
||||
mkdir(dirname($file_path), 0777, true);
|
||||
}
|
||||
|
||||
file_put_contents($file_path, $image);
|
||||
|
||||
return $file_path;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,9 @@ class PostShow
|
||||
|
||||
$src = $dom_img->attr('src');
|
||||
|
||||
if (strpos($src, 'data:image') !== 0) {
|
||||
$src = get_source_link($src);
|
||||
}
|
||||
|
||||
$watermarked_file_save_name = Image::handelWatermarkSave($src);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user