2015-02-07 56 views
0

我想寫一個腳本,從文本,大小和它獲得的$ _GET參數中的字體生成一個PNG圖像,但我不知道如何使圖像的大小完全符合文本。我已經在使用imagettfbox:PHP GD imagettftext測量

$widthPx = abs($ttfBox[2] - $ttfBox[0]); 
$heightPx = abs($ttfBox[1] - $ttfBox[7]); 

這可能會給我正確的測量結果,但是當我繪製文本時,它會出現一些超出範圍的情況。例如,如果我嘗試使用arial.ttf繪製一個「a」,它至少有5個像素越界。有沒有一種方法可以在沒有測試的情況下繪製完全適合圖像的任何字體的文本?

$text = $_GET["text"]; 

$cmToPixel = 15.0; 

$sizeCm = floatval($_GET["sizeCm"]); 
$sizePx = $cmToPixel * $sizeCm; 
$fontFile = "fonts/".pathinfo($_GET["font"])["filename"].".".pathinfo($_GET["font"])["extension"]; 

if(!file_exists($fontFile)){ 
    die; 
} 
$ttfBox = imagettfbbox($sizePx, 0, $fontFile, $text); 

$widthPx = abs($ttfBox[2] - $ttfBox[0]); 
$heightPx = abs($ttfBox[1] - $ttfBox[7]); 

$image = ImageCreate($widthPx, $heightPx); 

$x = $ttfBox[0] + (imagesx($image)-$ttfBox[4])/ 2 - 0; 
$y = $ttfBox[1] + (imagesy($image)/2) - ($ttfBox[5]/2) - 5; 

ImageRectangle($image,0,0,imagesx($image),imagesy($image), ImageColorAllocate($image,255,255,255)); 
imagettftext($image, $sizePx,0,$x,$y, ImageColorAllocate($image, 0, 0, 0), $fontFile, $text); 

header("Content-Type: image/png"); 
ImagePng($image); 
ImageDestroy($image); 
+0

您是否查看了本手冊提供的示例?他們可能會幫助:[imagettfbbox - 使用TrueType字體給文本的邊界框](http://php.net/manual/en/function.imagettfbbox.php) – 2015-02-08 02:53:14

+0

我已經看過所有的例子,它似乎我必須通過測試像素顏色來修剪圖像,因爲文本輸出太不準確了。當我繪製它時,每個文本都有不同的偏移量,所以它總是超出範圍。 – user1563232 2015-02-08 08:54:07

+0

是否可以發佈您使用的代碼,以便我們可以試用它? – 2015-02-08 09:00:51

回答

0

您從邊界框的計算已關閉。這工作:

<?php 
/*- 
* $MirOS: www/mk/ttf2png,v 1.8 2016/11/02 16:16:26 tg Exp $ 
*- 
* Copyright (c) 2009, 2016 
* mirabilos <[email protected]> 
* 
* Provided that these terms and disclaimer and all copyright notices 
* are retained or reproduced in an accompanying document, permission 
* is granted to deal in this work without restriction, including un- 
* limited rights to use, publicly perform, distribute, sell, modify, 
* merge, give away, or sublicence. 
* 
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to 
* the utmost extent permitted by applicable law, neither express nor 
* implied; without malicious intent or gross negligence. In no event 
* may a licensor, author or contributor be held liable for indirect, 
* direct, other damage, loss, or other issues arising in any way out 
* of dealing in the work, even if advised of the possibility of such 
* damage or existence of a defect, except proven that it results out 
* of said person's immediate fault when using the work as intended. 
*- 
* Syntax: 
* php ttf2png [text [size [/path/to/font.ttf]]] >out.png 
*/ 

if (!function_exists('gd_info')) 
    die("Install php5-gd first."); 
$gd = gd_info(); 
if ($gd["FreeType Support"] == false) 
    die("Compile php5-gd with FreeType 2 support."); 


$font = "/usr/src/www/files/FNT/GenI102.ttf"; 
$fontsize = 30; 
$text = "EINVAL"; 

if (isset($argv[1])) 
    $text = $argv[1]; 
if (isset($argv[2])) 
    $fontsize = $argv[2]; 
if (isset($argv[3])) 
    $font = $argv[3]; 


// Get bounding box 
$bbox = imageftbbox($fontsize, 0, $font, $text); 
// Transform coordinates into width+height and position 
$ascender = abs($bbox[7]); 
$descender = abs($bbox[1]); 
$size_w = abs($bbox[0]) + abs($bbox[2]); 
$size_h = $ascender + $descender; 
$x = -$bbox[0]; 
$y = $ascender; 

// Create image 
$im = imagecreatetruecolor($size_w, $size_h); 
// Allocate colours 
$bgcol = imagecolorallocate($im, 0x24, 0x24, 0x24); 
$fgcol = imagecolorallocate($im, 0xFF, 0xFF, 0xFF); 

// Fill image with background colour 
imagefilledrectangle($im, 0, 0, $size_w - 1, $size_h - 1, $bgcol); 
// Render text into image 
imagefttext($im, $fontsize, 0, $x, $y, $fgcol, $font, $text); 

// Convert true colour image (needed for above) to palette image 
imagetruecolortopalette($im, FALSE, 256); 

// Output created image 
imagepng($im, NULL, 9); 

exit(0); 

如果你寫在同一行多個字符串,需要計算總的高度和行偏移,它是所有最大所有伸的加上最大全部下伸,並$yimagefttext該行的調用類似於所有上行的最大值。

+0

我試過你的代碼,它輸出:http://prntscr.com/eo4gfx – 2017-03-24 22:58:50

+0

@MarcielFonseca這是一個PNG,保存到一個文件或管道它通過'xloadimage stdin'或其他合適的圖像瀏覽器。 Unix工具通常會將結果轉儲到stdout,以便能夠通過管道用作過濾器。 – mirabilos 2017-03-25 01:25:53