$imgpath = str_replace('\\', '/', $imgpath); // 文件上传目录
$imgresource = realpath(ltrim($imgpath,"/"));
$image = \think\Image::open($imgresource);
if ($image->width() > $water['mark_width'] && $image->height() > $water['mark_height']) {
if ($water['mark_type'] == 'text') {
$ttf = realpath('static/admin/font/webfont.ttf');
if (file_exists($ttf)) {
$size = $water['mark_size'] ? $water['mark_size'] : 30;
$color = $water['mark_color'] ?: '#000000';
if (!preg_match('/^#[0-9a-fA-F]{6}$/', $color)) {
$color = '#000000';
}
$transparency = intval((100 - $water['mark_diaphaneity']) * (127 / 100));
$color .= dechex($transparency);
$image->open($imgresource)->text($water['mark_text'], $ttf, $size,$color,$water['mark_sel'])->save($imgresource);
}
} else {
$water['mark_img'] = preg_replace('#^(/[/\w]+)?(/public/upload/|/upload/)#i', '$2', $water['mark_img']);
$waterPath = realpath(ltrim($water['mark_img'],"/"));
if (file_exists($waterPath)) {
$quality = $water['mark_diaphaneity'] ? $water['mark_quality'] : 80;
$waterTempPath = dirname($waterPath) . '/temp_' . basename($waterPath);
$image->open($waterPath)->save($waterTempPath, null, $quality);
$image->open($imgresource)->water($waterTempPath, $water['mark_sel'], $water['mark_diaphaneity'])->save($imgresource);
@unlink($waterTempPath);
}
}
} 最佳答案