2014-01-20 22 views
1

我有一個圖像,我試圖使用文本水印圖像。在PHP中使用文本水印圖像

我已經完成了代碼部分,但沒有看到圖像看到水印文本。

這裏是下面的代碼:

$imagetobewatermark="images/muggu.png"; 
list($width,$height)=getimagesize($imagetobewatermark); 
$imagetobewatermark=imagecreatetruecolor($width,$height); 
$mark=imagecreatefrompng($imagetobewatermark); 
imagecopy($imagetobewatermark,$mark,0,0,0,0,$width,$height); 
$wartermarktext="Muggu"; 
$font="../font/century gothic.ttf"; 
$fontsize="15"; 
$white = imagecolorallocate($imagetobewatermark, 255, 255, 255); 
imagettftext($imagetobewatermark, $fontsize, 0, 20, 10, $white, $font, $watermarktext); 
header("Content-type:image/png"); 
imagepng($imagetobewatermark); 
imagedestroy($imagetobewatermark); 

告訴我,如果我錯了。

謝謝。

+2

爲什麼你不使用像干預/圖像包? – Aristona

+0

啊,錯字!我更新了我的答案... – Raad

回答

4

的一個問題,我可以看到立竿見影的是,$imagetobewatermark變量開始了作爲一個字符串,然後成爲一個新的空白圖像對象(而不是現有的圖像),並在隨後創建的mark圖像對象,它不會工作,因爲$imagetobewatermark不再是一個字符串。

嘗試:

$imagetobewatermark=imagecreatefrompng("images/muggu.png"); 
$watermarktext="Muggu"; 
$font="../font/century gothic.ttf"; 
$fontsize="15"; 
$white = imagecolorallocate($imagetobewatermark, 255, 255, 255); 
imagettftext($imagetobewatermark, $fontsize, 0, 20, 10, $white, $font, $watermarktext); 
header("Content-type:image/png"); 
imagepng($imagetobewatermark); 
imagedestroy($imagetobewatermark); 

編輯: 我沒有注意到在你的文本變量$wartermarktext一個錯字,這應該是$watermarktext。 糾正這一點,它應該工作。

+0

我在本地桌面上試過上面的代碼,但沒有看到輸出。我已將圖像保存到本地桌面,但沒有看到水印。你可以幫我嗎? – Swetha

+0

是的。現在我可以看到變化。謝謝你Raad。 – Swetha

+0

我沒有得到任何輸出......有人可以幫忙嗎? :( – Dimitar

1

你有一些拼寫錯誤,並沒有使用資源。這裏更正:

$imagetobewatermark = "muggu.png"; 
list ($width, $height) = getimagesize($imagetobewatermark); 
$res = imagecreatetruecolor($width, $height); 

$mark = imagecreatefrompng($imagetobewatermark); 
//make sure here to use the ressource, not the filepath 
imagecopy($res, $mark, 0, 0, 0, 0, $width, $height); 

$watermarktext = "Muggu"; 
//$font = "../font/century gothic.ttf"; 
//I copied it to my local test folder 
$font = "GOTHIC.TTF"; 
$fontsize = "15"; 
//make sure here to use the ressource, not the filepath 
$white = imagecolorallocate($res, 255, 255, 255); 
//make sure here to use the ressource, not the filepath 
imagettftext($res, $fontsize, 0, 20, 10, $white, $font, $watermarktext); 

header("Content-type:image/png"); 
//make sure here to use the ressource, not the filepath 
imagepng($res); 
//make sure here to use the ressource, not the filepath 
imagedestroy($res); 
+0

此外,您還可以測試不同的字體大小,放置水印的圖像邊框的更多邊距,然後嘗試另一種顏色。只是爲了調試。本地它與上面的代碼很好地工作。 – Ben

+0

我已經在我的本地桌面上嘗試了上面的代碼,但沒有看到輸出。我已將圖像保存到本地桌面,但沒有看到水印。我也嘗試過不同的字體大小。你可以幫我嗎? – Swetha

+0

您是否將TTF文件複製到正確的位置?你的形象(尺寸)有多大?也許你把文字放在圖像之外? – Ben

0

試試這個。

$imagetobewatermark = "images/muggu.png"; 
$watermarktext = "Muggu"; 

list($width, $height) = getimagesize($imagetobewatermark); 

$image_p = imagecreatetruecolor($width, $height); 
$image = imagecreatefrompng($imagetobewatermark); 
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height); 

$font = "../font/century gothic.ttf"; 
$font_size = 15; 

$white = imagecolorallocate($image_p, 255, 255, 255); 
imagettftext($image_p, $font_size, 0, 20, 20, $white, $font, $watermarktext); 

header("Content-Type: image/png"); 
imagepng($image_p, null, 0); 
imagedestroy($image); 
imagedestroy($image_p); 
+0

我已經嘗試了上述代碼在我的本地桌面,但沒有看到輸出。我已將圖像保存到本地桌面,但未保存圖像。你可以幫我嗎? – Swetha

0

試試這個代碼,它可以幫助你提供圖像的水印。檢查Example here

<?php 
    header('Content-type: image/jpeg'); //SET THE FORMATE OF THE IMAGE 
    $jpg_image = imagecreatefromjpeg('images.jpg'); //GIVE LINK FOR SPECIFIED IMAGE 
    $color = imagecolorallocate($jpg_image, 500, 500, 500); //GIVE COLOR TO WATERMARK TEXT 
    $font_location = 'BROADW.TTF'; //WATERMARK FONT 
    $text = "http://www.sanwebtutorials.blogspot.in/"; //WATERMARK TEXT 
    $x=200; //X CO-ORDINATE 
    $y=800; //Y CO-ORDINATE 
    $size=21; //SIZE OF TEXT 
    imagettftext($jpg_image,$size, 0, $x, $y, $color, $font_location, $text); //PRINT TEXT ON IMAGE 
    imagejpeg($jpg_image); //SEND IMAGE TO BROWSER 
    imagedestroy($jpg_image); //DESTROY THE MEMORY 
?>