2013-06-18 1 views
0

나는 이것으로 내 머리카락을 찢어 버리고 여기에 많은 쓸모없는 해결책을 시도했다.GD Lib로 텍스트 추가

이미지에 텍스트를 추가하려고하지만 배경 이미지가 모두 보이기 때문에 여기에서 잘못하고있는 것이 눈에 띄지 않습니다. adcance

<? 
header('Content-Type: image/jpeg'); 

$fbid = $_POST['fbid']; 
$background_img = $_POST['background']; 
$message = $_POST['text']; 
$ts = $_POST['ts']; 

$filename = $fbid . "-" . $ts . ".jpg"; 

$image_canvas = imagecreatetruecolor(640,400); 

$background = imagecreatefromjpeg($background_img); 
$overlay = imagecreatefrompng("../images/image-overlay.png"); 

imagecopyresampled($background, $overlay, 0, 0, 0, 0, imagesx($overlay), imagesy($overlay), imagesx($overlay), imagesy($overlay)); 

imagefilledrectangle($image_canvas, 0,0,150,30, $background); 

$white = imagecolorallocate($background, 255, 255, 255); 

imagettftext($image_canvas, 25, 0, 50, 50, $white, "arial.TTF", $message); 

imagejpeg($background,"../created/" . $filename, 100); 

imagedestroy($background); 
+0

arial에 대한 경로입니다 .TFTF, 전체 경로를 시도해 보셨습니까? – somedev

+0

arial.ttf는이 예제 스크립트의 스크립트와 같은 폴더에 있습니다. –

+0

'$ filename'이 (가) 정의되지 않았습니다 ... –

답변

0

에서

덕분에 당신은 캔버스를 놓치고있어. imageCreateTrueColor으로 빌드를 시작하십시오.

$imageCanvas = imageCreateTrueColor($width, $height); 
//your code 
$background = imagecreatefromjpeg($background_img); 
//more of your code 
imagefilledrectangle($imageCanvas, 0, 0, 150, 30, $background); 
//now do the same for the text only us imag 
imagettftext($imageCanvas, 25, 0, 50, 50, $white, "arial.TTF", $message); 

$ imageCanvas의 jpeg 및 텍스트 요소를 병합합니다.

+0

여전히 나를 위해 작동하지 않습니다. 내 질문에 전체 스크립트를 그대로 표시하도록 코드를 업데이트했습니다. –

0

이것 좀보세요. 작동하며 페이지 링크는 아래에 있습니다. 원래 게시물을 기반으로, 나는 이것이 당신이 찾고있는 것이라고 믿습니다.

/* first composite the canvas with the background */ 
$background_img="../img/adfuba_october.png"; 
$compositeString = "composite.png"; 

list($width,$height) = getimagesize($background_img); 
$image_canvas = imagecreatetruecolor($width,$height); 
$background = imagecreatefrompng($background_img); 
imagecopyresampled($image_canvas,$background,0,0,0,0,$width,$height,$width,$height); 

/* now add the text */ 
$fontPath = "path/to/your/fontFile/ARIAL.TTF"; 
$fontSize = 24; 
$percent = 0.25; 
$txt_x = abs($width*$percent); 
$txt_y = abs($height*$percent); 
$color = "008844"; 
$message = "This is User Text"; 
imageTTFtext($image_canvas, $fontSize, 0, $txt_y, $txt_y, $color, $fontPath, $message); 

/* now generate the file */ 
imagepng($image_canvas, $compositeString, 0) or die("error saving png"); 

?> 
<p>This is a composite image:<br><img src="<?php echo $compositeString;?>"></p> 

합성 이미지 here을 볼 수 있습니다. 몇 가지 사항을 명심하십시오. 트루 타입 글꼴 파일이 스크립트와 같은 디렉토리에있는 경우에도 트루 타입 글꼴의 경로는 절대적이어야합니다.

또한 캔버스가 배경 개체이므로 이미지 또는 텍스트를 캔버스 위에 겹쳐서 표시합니다.

마지막으로, 계층 적 요소는 폼 종속 캔버스에서 텍스트로 나타납니다. 의미 캔버스 -> 배경 -> 다른 그래픽 -> 다음 텍스트. 그렇지 않으면 앞에 렌더링하려는 요소를 은폐 할 수 있습니다. 희망이 도움이됩니다.

+0

내 게시물에 추가하시오. 나는 $ _POST [ 'background']가 문자열이고 이미 서버에 저장된 이미지 파일의 경로/이름을 포함하고 있다고 가정합니다. $ _POST [ 'background']가 이미지 파일이면 [파일 업로드 프로세스] (http://www.php.net/manual/en/features.file-upload.post- method.php)을 사용하여 새 이미지 파일을 작성하십시오. – rwhite35

관련 문제