php中的水印在所有图片上都不统一

时间:2016-03-14 06:59:29

标签: php image watermark

我正尝试在watermark中以编程方式在图片上创建PHP。图片由users上传,可能具有不同的尺寸。我在创建watermark时所做的是:

$stamp = imagecreatefrompng('stamp381x387.png');
$ext = substr(strrchr($_GET['src'], '.'), 1);

if ($ext == 'png') {
    $im = imagecreatefrompng($_GET['src']);
} else if ($ext == 'jpg') {
    $im = imagecreatefromjpeg($_GET['src']);
}

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 0;
$marge_bottom = 0;
$sx = imagesx($stamp); //width
$sy = imagesy($stamp); //height
// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, (imagesx($im) - $sx ) / 2, (imagesy($im) - $sy) / 2, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory

if ($ext == 'png') {
    header('Content-type: image/png');
    imagejpeg($im);
    imagedestroy($im);
} else if ($ext == 'jpg') {
    header('Content-type: image/jpeg');
    imagejpeg($im);
    imagedestroy($im);
}

现在,它会在图像上创建水印,但这些水印并不均匀。见附图。 enter image description here

enter image description here

如何在所有images上统一?有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

这是使用PHP在图像上添加水印的一个很好的示例 - http://php.net/manual/en/image.examples-watermark.php