php - 使用 PHP 上传图像时添加文本水印不起作用
问题描述
我正在开发 php web 应用程序,我想在上传多个图像时添加文本水印?
addTextWatermark
上传多张图片时功能不工作或执行?只是将图像上传到动态创建的文件夹中?
PHP 代码
function addTextWatermark($src, $watermark, $save=NULL) {
list($width, $height) = getimagesize($src);
$image_color = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($src);
imagecopyresampled($image_color, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$txtcolor = imagecolorallocate($image_color, 255, 255, 255);
$font = 'MONOFONT.ttf';
$font_size = 50;
imagettftext($image_color, $font_size, 0, 50, 150, $txtcolor, $font, $watermark);
if ($save<>'') {
imagejpeg ($image_color, $save, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_color, null, 100);
}
imagedestroy($image);
imagedestroy($image_color);
}
$data = explode(".", $_FILES["upload_file"]["name"]);
$extension = $data[1];
$new_file_name = rand() . '.' . $extension;
$path = $_POST["hidden_folder_name"] . '/' . $new_file_name;
foreach($_FILES['upload_file']['name'] as $key=>$val){
$file_path = $path.$_FILES['upload_file']['name'][$key];
$filename = $_FILES['upload_file']['name'][$key];
if(is_uploaded_file($_FILES['upload_file']['tmp_name'][$key])) {
if(move_uploaded_file($_FILES['upload_file']['tmp_name'][$key],$file_path)){
$watermark = "PHPZAG"; // Add your own water mark here
addTextWatermark($filename, $watermark, $filename);
$upload_images[] = $file_path;
}
}
}
解决方案
推荐阅读
- postgresql - 如何从 SQL 中的字符串/varchar 列中提取月份
- java - 多模块 Maven 项目 - 在 jar 中包含测试 bean
- jquery - 我使用 jQuery 将页脚放置在页面底部,但仍然显示一些空白
- java - Scala 类型不匹配,无法解决问题
- r - 在 R 中使用公式
- javascript - 如何使用 Javascript 或 JQuery 访问 Aria 组合框的值
- linux - 如何使用 git 登录邮件发送补丁
- python - Python - 将对象作为类参数传递时出现AttributeError
- django - ModuleNotFoundError:没有名为“sorl”的模块
- python-3.x - BFS 准备兔子逃生