2012-02-23 107 views
0

我有水印文字的代碼到圖像放置圖像水印

if($type==".jpg" or $type==".jpeg"){ 
    $im = imagecreatefromjpeg($uploaddir.$randnum); 
}elseif($type==".gif"){ 
    $im = imagecreatefromgif($uploaddir.$randnum); 
}else{ 
    $im = imagecreatefrompng($uploaddir.$randnum); 
} 

$imagesize = getimagesize($uploaddir.$randnum); 

$x_offset = 7; 
$y_offset = 8; 

$textcolor = imagecolorallocate($im, 0xCC, 0xCC, 0xCC); 
$textcolor2 = imagecolorallocate($im, 0x00, 0x00, 0x00); 
imagestring($im, 5, $x_offset, $y_offset, strtoupper($_POST['code']), $textcolor2); 
if($type==".jpg" or $type==".jpeg"){ 
    imagejpeg($im,$uploaddir.$randnum,100); 
}elseif($type==".gif"){ 
    imagegif($im,$uploaddir.$randnum,100); 
}else{ 
    imagepng($im,$uploaddir.$randnum,8);  
} 

上面的代碼打印在左上角的水印......但我想它要對在底部寫對。

任何幫助傢伙

問候

回答

0

嘗試使用這個地方你正在使用imagestring()

$font_size = 5; 
$margin = 7; 
$text_width = imagefontwidth($font_size)*strlen($_POST['code']); 
$text_height = imagefontheight($font_size); //assuming it's one line 
imagestring($im, $font_size, $imagesize[0] - $text_width - $margin, $imagesize[1] - $text_height - $margin, strtoupper($_POST['code']), $textcolor2); 

變化$margin$font_size滿足您的需求。

2

這是實際的地方水印行:

imagestring($im, 5, $x_offset, $y_offset, strtoupper($_POST['code']), $textcolor2); 

水印的水平位置將取決於你所設定$x_offset是。現在是7,這意味着左邊7個像素。

爲了得到它爲7個像素組成的權利,將其設置爲整個圖像的寬度,減去(7 +水印的寬度)

找到水印的寬度與imagettfbbox

設置垂直位置的原理是相同的。

0

如果你想將它移至底部,只是改變你的y偏移是圖像的底部,的「8」,而不是(這可能是靠近頂部):

$y_offset = $imagesize['height'] - 7; 
+0

沒有親愛的不工作 – 2012-02-23 22:34:33

+0

當你這樣做時發生了什麼?水印有多高?它需要是圖像的大小減去水印的大小。我只是猜測水印的高度爲'7'。 – cegfault 2012-02-23 22:38:23

+0

水印是文字而不是圖像親愛的 – 2012-02-23 22:52:33