2016-08-05 124 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); 
?>