2009-12-20 121 views
5

下面的PHP代碼生成文本作爲動態創建的圖像,我將如何才能使圖像只與文本一樣大?謝謝。根據文字大小調整圖像大小

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

    $text='Test'; 

    $img = imageCreate(200,200); 

    imagecolorallocate($img, 255, 255, 255); 

    $textColor = imagecolorallocate($img, 0, 0, 0); 

    imagefttext($img, 15, 0, 0, 55, $textColor, 'bgtbt.ttf', $text); 

    imagejpeg($img); 

    imagedestroy($img); 
?> 

更新1:我發現樓主的例子這裏的答案 - Creating IMage from Text in PHP - how can I make multiline?

更新2:馬丁·蓋斯勒的版本也是行之有效

回答

6

當使用TrueType字體,可以使用imageftbbox函數獲取字體排版的邊界框與您的字體。邊界框給出從基點到由文本佔據的矩形中的四個角的偏移量。所以,如果你存儲的邊框在$bb,並使用imagefttext把文本在($x, $y),那麼角落都會有這些座標:

($x + $bb[6], $y + $bb[7])   ($x + $bb[4], $y + $bb[5]) 
          +-------+ 
          | Hello | 
          +-------+ 
($x + $bb[0], $y + $bb[1])   ($x + $bb[2], $y + $bb[3]) 

這告訴我們,我們需要的($x + $bb[2]) - ($x + $bb[6]) = $bb[2] - $bb[6]圖像的寬度和同樣的圖像高度$bb[3] - $bb[7]。該文本應然後在該圖片中的座標(-$bb[6], -$bb[7])被渲染,因爲我們希望有

(0, 0) = ($x + $bb[6], $y + $bb[7]) ==> $x = -$bb[6] and $y = -$bb[7] 

你可以嘗試一下這個代碼。把它放進一個名爲img.php文件,瀏覽到img.php?q=Hello測試:

<?php 
header("Content-type: image/png"); 

$q  = $_REQUEST['q']; 
$font = "Impact.ttf"; 
$size = 30; 
$bbox = imageftbbox($size, 0, $font, $q); 

$width = $bbox[2] - $bbox[6]; 
$height = $bbox[3] - $bbox[7]; 

$im = imagecreatetruecolor($width, $height); 
$green = imagecolorallocate($im, 60, 240, 60); 

imagefttext($im, $size, 0, -$bbox[6], -$bbox[7], $green, $font, $q); 
imagepng($im); 
imagedestroy($im); 
?> 

如果使用位圖字體代替,再看看imagefontwidthimagefontheight功能。

+0

我想使用TTF字體,我看了一些的imageftbbox頁面上的例子,但我似乎無法獲取文本的尺寸。 – usertest 2009-12-20 19:58:30

+0

是的,我很抱歉,我起初回答了錯誤的問題 - 現在我已經爲TrueType字體提供了代碼。 – 2009-12-20 20:21:08

+0

我試了一下代碼,發現錯誤「The image」test.php「無法顯示,因爲它包含錯誤。」 – usertest 2009-12-20 20:40:49

2

@Martin蓋斯勒的回答幾乎是正確的,但我無法讓我的文本完全適合圖像。我試過這個,完美的作品!

PHP Manual's User Contributed Notes

$text = "<?php echo \"hello, world\"; ?>"; 
$font = "./arial.ttf"; 
$size = "60"; 

$bbox = imagettfbbox($size, 0, $font, $text); 

$width = abs($bbox[2] - $bbox[0]); 
$height = abs($bbox[7] - $bbox[1]); 

$image = imagecreatetruecolor($width, $height); 

$bgcolor = imagecolorallocate($image, 255, 255, 255); 
$color = imagecolorallocate($image, 0, 0, 0); 

$x = $bbox[0] + ($width/2) - ($bbox[4]/2); 
$y = $bbox[1] + ($height/2) - ($bbox[5]/2); 

imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $bgcolor); 
imagettftext($image, $size, 0, $x, $y, $color, $font, $text); 

$last_pixel= imagecolorat($image, 0, 0); 

for ($j = 0; $j < $height; $j++) 
{ 
    for ($i = 0; $i < $width; $i++) 
    { 
     if (isset($blank_left) && $i >= $blank_left) 
     { 
      break; 
     } 

     if (imagecolorat($image, $i, $j) !== $last_pixel) 
     { 
      if (!isset($blank_top)) 
      { 
       $blank_top = $j; 
      } 
      $blank_left = $i; 
      break; 
     } 

     $last_pixel = imagecolorat($image, $i, $j); 
    } 
} 

$x -= $blank_left; 
$y -= $blank_top; 

imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $bgcolor); 
imagettftext($image, $size, 0, $x, $y, $color, $font, $text); 

header('Content-type: image/png'); 
imagepng($image); 
imagedestroy($image); 
+0

對不起,Windows和Linux似乎有不同的工作方式,並且沒有任何腳本可以在兩者中完美工作。 – 2010-02-23 04:51:43

相關問題