2016-08-05 92 views
-2

因此,我有代碼從一個目錄中將所有文件加以壓縮,並將它們水印並放入另一個目錄中。我想知道如何使用文本標記我的圖像,我嘗試過使用imagettftext(),但沒有運氣。如何爲文本加水印圖像?

這裏是工作的代碼

<?php 
//Source folder where all images are placed 
$source="myimages"; 

//Destination folder where all images with watermark will be copied 
$destination="donewatermarks"; 

//Creating an image object of watermark image 
$watermark=imagecreatefrompng("watermark.png"); 

//Margin of watermark from right and bottom of the main image 
$margin_right=10; 
$margin_bottom=10; 

//Height ($sy) and Width ($sx) of watermark image 
$sx=imagesx($watermark); 
$sy=imagesy($watermark); 

//Get list of images in source folder 
$images=array_diff(scandir($source), array('..', '.')); 

foreach($images as $image){ 
//Create image object of main image 
$img=imagecreatefromjpeg($source.'/'.$image); 

//Copying watermark image into the main image 
imagecopy($img, $watermark, imagesx($img) - $sx - $margin_right, 
imagesy($img) - $sy - $margin_bottom, 0, 0, $sx, $sy); 

//Saving the merged image into the destination folder 
imagejpeg($img, $destination.'/'.$image,100); 

//Destroying the main image object 
imagedestroy($img); 
} 

//Destroying watermark image object 
imagedestroy($watermark); 

?> 

提前感謝!

這裏是我試過的代碼目前返回一個錯誤

<?php 
//Source folder where all images are placed 
$source="watermarkitems"; 

//Destination folder where all images with watermark will be copied 
$destination="donewatermarks"; 

//Creating an image object of watermark image 
$watermark=imagecreatefrompng("watermark.png"); 

//Margin of watermark from right and bottom of the main image 
$margin_right=10; 
$margin_bottom=10; 

//Height ($sy) and Width ($sx) of watermark image 
$sx=imagesx($watermark); 
$sy=imagesy($watermark); 

$text = 'Testing...'; 

//Get list of images in source folder 
$images=array_diff(scandir($source), array('..', '.')); 

foreach($images as $image){ 
//Create image object of main image 
$img=imagecreatefromjpeg($source.'/'.$image); 

// Add the text 
imagettftext($img, 20, 0, 10, 20, $black, $font, $text); 

//Saving the merged image into the destination folder 
imagejpeg($img, $destination.'/'.$image,100); 

header('Content-Type: image/jpg'); 

//Destroying the main image object 
imagedestroy($img); 
} 

//Destroying watermark image object 
imagedestroy($watermark); 

?> 
+1

*以下是工作代碼* - 您的嘗試失敗了怎麼辦?和它有什麼問題?完全通過手冊? http://php.net/manual/en/function.imagettftext.php –

+0

@ Fred-ii-是的,我一直通過手冊,如果我添加標題(內容類型:圖像/ PNG);我收到一條錯誤消息,如果我不添加圖片,圖片會移動而沒有任何水印 –

+0

您可以在您的原始代碼和準確的錯誤消息下發布代碼嗎?這在你的問題當然;-)我現在不在我的開發電腦,但我會看到它,當我到達那裏。你也在下面給出了答案,你試過了嗎? –

回答

0

此代碼運行對我來說,你可以用這個代碼的嘗試。

<?php 
$watermark = imagecreatefrompng('watermark.png'); 
$imageURL = imagecreatefrompng('bg.png'); 
$watermarkX = imagesx($watermark); 
$watermarkY = imagesy($watermark); 
imagecopy($imageURL, $watermark, imagesx($imageURL)/5, imagesy($imageURL)/5, 0, 0, $watermarkX, $watermarkY); 
header('Content-type: image/png'); 
imagepng($imageURL); 
imagedestroy($imageURL); 
?>