2016-06-24 52 views
1

我使用下面的代码来设置水印,但它将水印设置为中心,我想将它设置在右下角位置。水印文本位置右下角

<?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

如何设定的位置上到右下的方向吗?

回答

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; 

EDIT对于右下:

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

确定。让我试试这个。 –

+0

它太tooooo底部和右邮戳 –

+0

图像内?您可以增加25和5的值以更改位置。 – Phiter