如何在PHPLaravel中添加不同位置的水印图片?

How do I add the watermark image with different positions in PHP Laravel?

您好,我想添加 水印图像 ,位置不同,例如

top-left,
top-right,
bottom-left,
bottom-right,
center

Admin select setting 选项卡中的上述位置之一。我正在使用 Orakuploader 插件,在该插件中,水印出现在上传图像的 center 中。

这里是水印图片的代码,用于center位置:

function addWatermark($watermark, $imageDirectory, $imageName, $x = 0, $y = 0)
{
    if(file_exists($watermark))
    {
        $marge_right  = 0;
        $marge_bottom = 0;

        $stamp = imagecreatefrompng($watermark);

        $image_extension = @end(explode(".", $imageName));
        switch($image_extension)
        {
            case "jpg":
                $im = imagecreatefromjpeg("$imageDirectory/$imageName");
                break;
            case "jpeg":
                $im = imagecreatefromjpeg("$imageDirectory/$imageName");
                break;
            case "png":
                $im = imagecreatefrompng("$imageDirectory/$imageName");
                break;
        }

        $imageSize = getimagesize("$imageDirectory/$imageName");
        $watermark_o_width = imagesx($stamp);
        $watermark_o_height = imagesy($stamp);

        $newWatermarkWidth = $imageSize[0];
        $newWatermarkHeight = $watermark_o_height * $newWatermarkWidth / $watermark_o_width;


        if((int)$x <= 0)
            $x = $imageSize[0]/2 - $newWatermarkWidth/2;
        if((int)$y <= 0)
            $y = $imageSize[1]/2 - $newWatermarkHeight/2;

        imagecopyresized($im, $stamp, $x, $y, 0, 0, $newWatermarkWidth, $newWatermarkHeight, imagesx($stamp), imagesy($stamp));

        switch($image_extension)
        {
            case "jpg":
                header('Content-type: image/jpeg');
                imagejpeg($im, "$imageDirectory/$imageName", 100);
                break;
            case "jpeg":
                header('Content-type: image/jpeg');
                imagejpeg($im, "$imageDirectory/$imageName", 100);
                break;
            case "png":
                header('Content-type: image/png');
                imagepng($im, "$imageDirectory/$imageName");
                break;
        }
    }
}

如何在其余位置(top-lefttop-rightbottom-leftbottom-right)添加水印?

非常感谢您的帮助。谢谢

我建议您为此目的使用干预图像库。它具有强大的表现力 API

不同位置加水印可以参考此页

http://image.intervention.io/api/insert

你应该得到这样的结果

$img = Image::make('public/foo.jpg');
$watermark = Image::make('public/watermark.png');
$img->insert('public/watermark.png', 'bottom-right', 10, 10);

嘿老兄,尝试用 imagecopyresized 函数替换此 代码 。 我希望它能为你工作。 谢谢

$sx = imagesx($stamp);
$sy = imagesy($stamp);

// top-left
imagecopy($im, $stamp, -45, -5, 0, 0, imagesx($stamp), imagesy($stamp));

// top-right
imagecopy($im, $stamp, imagesx($im) - $sx + 45, -5, 0, 0, imagesx($stamp), imagesy($stamp));

// bottom-left
imagecopy($im, $stamp, -45, imagesy($im) - $sy + 5, 0, 0, imagesx($stamp), imagesy($stamp));

// bottom-right
imagecopy($im, $stamp, imagesx($im) - $sx + 45, imagesy($im) - $sy + 5, 0, 0, imagesx($stamp), imagesy($stamp));

// center
imagecopy($im, $stamp, (imagesx($im) - $sx)/2, (imagesy($im) - $sy)/2, 0, 0, imagesx($stamp), imagesy($stamp));