2016-08-05 6 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-ヘッダー(Content-type:image/png)を追加すると、マニュアルを読んだことがあります。エラーメッセージが表示されます。画像を追加しないと、画像にウォーターマークが表示されずに移動します。 –

+0

オリジナルのコードの下に試したコードとDaveの正確なエラーメッセージを投稿できますか?これはあなたの質問ではもちろん;-)私は今の私のdev pcではないが、私はそこに着くときに私はそれを見てみましょう。あなたはまた、以下の答えを与えられました、あなたはそれを試しましたか? –

答えて

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); 
?> 
関連する問題