2013-08-15 2 views
0

textearea에서 뉴스 레터를위한 텍스트 이미지를 만들고 싶습니다. 그래서 1 줄 이상이됩니다. imagettfbbox를 사용하여 (모든 줄에서) 총 너비와 높이를 계산 한 다음 imagettftext를 사용하여 이미지를 쓰려고 생각했습니다. 문제는 글꼴 크기가 많을수록 남은 패딩이 텍스트에 "imagettftext"를 추가한다는 것입니다. imagettfbbox는 그 패딩에 대해 아무것도 말하지 않습니다. returing 배열 값 [0]과 [1]은 모두 ALLWAYS = -1입니다.상자 크기의 텍스트 가져 오기 및 이미지 텍스트 만들기

감사합니다.

답변

0

GD 또는 ImageMagick으로이 작업을 수행 할 수 있습니다. GD를 사용하여 기본 예제를 게시합니다.

<?php 
// Set the content-type 
header('Content-Type: image/png'); 

// Create the image 
$im = imagecreatetruecolor(400, 30); 

// Create some colors 
$white = imagecolorallocate($im, 255, 255, 255); 
$grey = imagecolorallocate($im, 128, 128, 128); 
$black = imagecolorallocate($im, 0, 0, 0); 
imagefilledrectangle($im, 0, 0, 399, 29, $white); 

// The text to draw 
$text = 'A simple text string'; 
// Replace path by your own font path 
$font = 'tahoma.ttf'; 

// Add some shadow to the text 
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text); 

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

// Using imagepng() results in clearer text compared with imagejpeg() 
imagepng($im); 
imagedestroy($im); 
?> 

이제이 문자열의 서식을 지정하기 위해 strlen()을 사용하여 개별 문자열의 길이를 반환하고 필요에 따라 함수에 전달할 수 있습니다. 더 많은 스트림 라이닝 방식의 경우 ImageMagick이 TextAlignment 등을 지원하기 때문에 ImageMagick을 사용하는 것이 좋습니다.

<?php 

define("LEFT", 1); 
define("CENTER", 2); 
define("RIGHT", 3); 

$w = 400; 
$h = 200; 
$gradient = new Imagick(); 
$gradient->newPseudoImage($w, $h, "gradient:red-black"); 

$draw = new ImagickDraw(); 
$draw->setFontSize(12); 
$draw->setFillColor(new ImagickPixel("#ffffff")); 

$draw->setTextAlignment(LEFT); 
$draw->annotation(150, 30, "Hello World1!"); 
$draw->setTextAlignment(CENTER); 
$draw->annotation(150, 50, "Hello World2!"); 
$draw->setTextAlignment(RIGHT); 
$draw->annotation(150, 70, "Hello World3!"); 

$draw->setFillColor(new ImagickPixel("#0000aa")); 
$x1 = 150; 
$x2 = 150; 
$y1 = 0; 
$y2 = 200; 
$draw->rectangle($x1, $y1, $x2, $y2); 

$gradient->drawImage($draw); 

$gradient->setImageFormat("png"); 
header("Content-Type: image/png"); 
echo $gradient; 
?> 

이 정보가 도움이되기를 바랍니다. 나는 당신이 가질 수있는 질문에 대해 자세히 설명 할 수있다.

+0

답장을 보내 주셔서 감사합니다. 이미지의 크기를 알 수 없으므로 이미지의 크기를 텍스트 크기로 정의하고 싶습니다. "imagettfbbox"로이 작업을 수행 한 다음 PHP 설명서를 참조하여 이미지 크기를 설정하십시오. 하지만 .. imagettftext는 글꼴 크기에 따라 텍스트에 이상한 패딩을 추가하고 있습니다. 수정하는 유일한 방법은 수동으로하는 것 같습니다. –