2017-01-08 4 views
2

画像にテキストを書いてその画像をブラウザに表示しようとしています。私はコードの下で試しましたが、正しく動作していません。以下のコードは空の1つの正方形(画像なし)を示しています。誰が私がここで何をしているのか間違っていると言うことができます画像にテキストを印刷できません

<?php 
header('Content-type: image/jpeg'); 
$jpg_image = imagecreatefromjpeg('images/travel.jpg'); 
$white = imagecolorallocate($jpg_image, 255, 255, 255); 
$text = "This is a sunset!"; 
imagettftext($jpg_image, 25, 0, 75, 300, $white, $text); 
imagejpeg($jpg_image); 
imagedestroy($jpg_image); 

?>

答えて

2

あなたは

にすぎだから変更をフォントファイルのパスを渡す必要がDOC http://www.php.net/manual/en/function.imagettftext.php で述べたように、あなたのコード内の問題は、このライン

imagettftext($jpg_image, 25, 0, 75, 300, $white, $text); 

です線

imagettftext($jpg_image, 15, 0, 15, 15, $white, 'Roboto-Bold.ttf',$text); 

とフォルダーにフォントファイルを置くと動作します。

あなたはフォントファイルを与えたくない場合は

imagestring($jpg_image,15,15,15,$text,$white); 

でライン

imagettftext($jpg_image, 25, 0, 75, 300, $white, $text); 

を置き換えるあなたのコードでは、これはあなたのために働くだろう

<?php 
header('Content-type: image/jpeg'); 
$jpg_image = imagecreatefromjpeg('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQf66Ol6NAa6sdNhDJT0z1fVfTmjPjxAHkopPwExZ9AqHSqNzHP'); 
$white = imagecolorallocate($jpg_image, 255, 255, 255); 
$text = "This is a sunset!"; 
imagestring($jpg_image,15,15,15,$text,$white); 
imagejpeg($jpg_image); 
imagedestroy($jpg_image); 
?> 

になります

+0

私はimagettextext($ jpg_image、15、 0、15、15、$ white、 'Roboto-Bold.ttf'、$ text); imagestring($ jpg_image、15,15,15、$ text、$ white)で。それでも動作していません。同じ出力を得る。 – Archana

+0

あなたのフォルダに 'images/travel.jpg'があります。私は私の答えを編集しました –

+0

あなたの助けをありがとう。上記のコードは私のために働いています。しかし、私が画像フォルダから画像を提供したいのであれば、どうすればよいですか? – Archana

関連する問題