水印集中而不是右上角

时间:2015-03-06 10:07:11

标签: php image-processing gd watermark

我正在使用一个php函数,在图像的右上角创建一个水印。

以下是代码:

<?php
function watermarking($source, $watermark, $save = NULL, $width = null, $height = null) {
    $watermark = @imagecreatefrompng($watermark)
        or exit("Impossible d'ouvrir le fichier (watermark).");

    imageAlphaBlending($watermark, false);
    imageSaveAlpha($watermark, true);

    $imageString = @file_get_contents($source)
        or exit("Impossible d'ouvrir le fichier (image).");
    $image = @imagecreatefromstring($imageString)
        or exit("Format de fichier (image) inconnu.");

    $imageWidth = imageSX($image);
    $imageHeight = imageSY($image);

    if (!($width)) {
        $watermarkWidth = imageSX($watermark);
    } else {
        $watermarkWidth = $width;
    }

    if (!($height)) {
        $watermarkHeight = imageSY($watermark);
    } else {
        $watermarkHeight = $height;
    }

    $coordinateX = ($imageWidth - 15) - ($watermarkWidth);
    $coordinateY = ($imageHeight - 15) - ($watermarkHeight);

    imagecopy($image, $watermark, $coordinateX, $coordinateY, 0, 0, $watermarkWidth, $watermarkHeight);

    if (!($save)) {
        header('Content-Type: image/jpeg');
    }   

    imagejpeg ($image, $save, 100);

    imagedestroy($image);
    imagedestroy($watermark);

    if (!($save)) {
        exit;
    }
}
?>

此功能在右上方添加水印,但我想将其置于图像中心。

怎么做?

由于

1 个答案:

答案 0 :(得分:0)

更新坐标变量

$coordinateX = ($imageWidth - $watermarkWidth) / 2;
$coordinateY = ($imageHeight - $watermarkHeight) / 2;