首页 > 解决方案 > 使用 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;
        }
    }
}

标签: phptextuploadwatermark

解决方案


推荐阅读