简体   繁体   English

PHP图片上传脚本功能如何添加Text或Logo水印?

[英]How to add Text or Logo watermark to PHP photo upload script function?

Thank you for reading my question.感谢您阅读我的问题。 I am struggling with an image upload function of a script which I bought through CC.我正在努力使用我通过 CC 购买的脚本的图像上传功能。 I am terrible with PHP hence only doing trial and errors to add watermark on bottom-right of the uploaded image.我对 PHP 很糟糕,因此只做试验和错误来在上传图像的右下角添加水印。 The script itself works as far as the image uploading goes.就图像上传而言,脚本本身有效。 I only get errors when I try to include the lines for adding watermark... I am pasting the upload script itself without my bad watermark code lines...当我尝试包含添加水印的行时,我只会遇到错误......我正在粘贴上传脚本本身而没有我的坏水印代码行......

$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);
    }
}

Thank you in advance先感谢您

EDIT:编辑:

$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]);

you have to give Public/Absolute Path for Font arial.ttf .你必须为字体 arial.ttf提供公共/绝对路径。

Solution is tested in Laravel 5.8.解决方案在 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM