如何在PHP照片上传脚本功能中添加文本或徽标水印?

时间:2018-07-12 07:53:00

标签: php image upload watermark

感谢您阅读我的问题。我在通过CC购买的脚本的图像上载功能方面苦苦挣扎。我对PHP感到很糟糕,因此只能做反复试验才能在上载图像的右下角添加水印。脚本本身可以进行图像上传。当我尝试添加添加水印的行时,我只会收到错误...我粘贴的上传脚本本身没有错误的水印代码行...

$width = IMAGE_MIN_WIDTH;
$height = IMAGE_MIN_HEIGHT;
$size_max = IMAGE_MAX_SIZE;


if (!@getimagesize($_FILES['Image1']['tmp_name']) == false) {
    if (!$add) {
        if ($obj->image != 'none') {
            $uploaded_image = 'none';
            unlink(IMAGE_UPLOAD_FOLDER . $obj->image);
        }
    }

    $array = getimagesize($_FILES['Image1']['tmp_name']);
    if (($_FILES['Image1']['size'] <= $size_max) && ($array['0'] >= $width) && ($array['1'] >= $height)) {

        $image = addslashes($_FILES['Image1']['tmp_name']);
        $uploaded_image = md5(basename($_FILES['Image1']['name'])) . md5(time()) . '.' . pathinfo($_FILES['Image1']['name'],
                PATHINFO_EXTENSION);
        $uploadfile = IMAGE_UPLOAD_FOLDER . $uploaded_image;
        $size = getimagesize($image);
        $src = imagecreatefromstring(file_get_contents($image));

        $dst = imagecreatetruecolor($size[0], $size[1]);
        imagecopyresampled($dst, $src, 0, 0, 0, 0, $size[0], $size[1], $size[0], $size[1]);

        imagedestroy($src);
        imagepng($dst, $uploadfile);
        imagedestroy($dst);
    }
}

提前谢谢

编辑:

$src = imagecreatefromstring(file_get_contents($image)); 
$stamp = imagecreatefrompng('img/logo-small.png'); 
$im = imagecreatefromjpeg($src); 
$marge_right = 10; 
$marge_bottom = 10; 
$sx = imagesx($stamp); 
$sy = imagesy($stamp); 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
imagepng($im, 'uploads/'.$new_file_name);
$dst = imagecreatetruecolor($size[0], $size[1]); 
imagecopyresampled($dst, $im, 0, 0, 0, 0, $size[0], $size[1], $size[0], $size[1]);

1 个答案:

答案 0 :(得分:0)

您必须提供字体arial.ttf 的公共/绝对路径。

解决方案已在Laravel 5.8中进行了测试。

function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile,$imgUrl) {
    list($width, $height) = getimagesize($SourceFile);
    $image_p = imagecreatetruecolor($width, $height);
    $image = imagecreatefromjpeg($SourceFile);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
   $black = imagecolorallocate($image_p, 0, 0, 0);
   $font = public_path('fonts/arial.ttf');
   $font_size = 8;
   imagettftext($image_p, $font_size, 0, 10, 20, $black,$font , $WaterMarkText);
   if ($DestinationFile <> '') {
    imagejpeg ($image_p, $DestinationFile, 100);
   } else {
    header('Content-Type: image/jpeg');
    imagejpeg($image_p, null, 100);
  };
imagedestroy($image);
imagedestroy($image_p);
return  $imgUrl;
}