【问题标题】:Watermark text position bottom right水印文字位置右下角
【发布时间】:2016-06-24 11:58:20
【问题描述】:

我正在使用以下代码设置水印,但它设置水印到中心,我想设置它的右下角。

<?php
function watermarkImage($fileget, $watermarktext, $saveto) 
    { 
        list($width, $height) = getimagesize($fileget);
        $image_p = imagecreatetruecolor($width, $height);
        $size = getimagesize($fileget);
        $dest_x = $size[0] - $width - 5;  
        $dest_y = $size[1] - $height - 5;
        $image = imagecreatefromjpeg($fileget);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height); 
        $white = imagecolorallocate($image_p, 255, 255, 255);
        $font = FONT_PATH;
        $font_size = 15;
        $bbox = imagettfbbox($font_size, 0, $font, $watermarktext);
        $x = $bbox[0] + (imagesx($image) / 2) - ($bbox[4] / 2) - 25;
        $y = $bbox[1] + (imagesy($image) / 2) - ($bbox[5] / 2) - 5;
        imagettftext($image_p, $font_size, 0, $x, $y, $white, $font, $watermarktext);
        if ($saveto<>'') {
            imagejpeg ($image_p, $saveto, 100); 
        } else {
            header('Content-Type: image/jpeg');
            imagejpeg($image_p, null, 100);
        };
        imagedestroy($image); 
        imagedestroy($image_p); 
    }
?>

我指的是这个答案:answer-1

如何将位置设置为右下角方向?

【问题讨论】:

    标签: php css watermark imagettftext placement


    【解决方案1】:

    不要将xy 位置除以2。这就是为什么它在中间。在以下几行:

    $x = $bbox[0] + (imagesx($image) / 2) - ($bbox[4] / 2) - 25;
    $y = $bbox[1] + (imagesy($image) / 2) - ($bbox[5] / 2) - 5;
    

    这应该可行,因为位置将在右下角:

    $x = $bbox[0] + (imagesx($image)) - ($bbox[4] / 2) - 25;
    $y = $bbox[1] + (imagesy($image)) - ($bbox[5] / 2) - 5;
    

    编辑右下角:

     $x = $bbox[0] + (imagesx($image)) - ($bbox[4] / 2) - 130;
     $y = $bbox[1] + (imagesy($image)) - ($bbox[5] / 2) - 30;
    

    【讨论】:

    • 好的。让我试试这个。
    • 图片内部?您可以增加 25 和 5 的值来改变位置。
    • 我希望这能提供一些指导。剩下的就靠你了:)
    猜你喜欢
    • 2015-07-11
    • 2012-04-02
    • 2021-01-18
    • 2020-05-01
    • 2015-05-04
    • 2015-05-07
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多