PHP GD imagettftext测量

PHP GD imagettftext测量,php,text,gd,Php,Text,Gd,我正试图编写一个脚本,根据$\u GET参数中的文本、大小和字体生成PNG图像,但我不知道如何使图像的大小与文本完全匹配。我已经在使用imagettfbox: $widthPx = abs($ttfBox[2] - $ttfBox[0]); $heightPx = abs($ttfBox[1] - $ttfBox[7]); 这可能给了我正确的测量值,但当我画文本时,它有点超出范围。例如,如果我尝试使用arial.ttf绘制一个“a”,它至少会超出边界5个像素。有没有一种方法可以在不进行测试的情

我正试图编写一个脚本,根据$\u GET参数中的文本、大小和字体生成PNG图像,但我不知道如何使图像的大小与文本完全匹配。我已经在使用imagettfbox:

$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);

从边界框进行的计算处于禁用状态。这项工作:

<?php
/*-
 * $MirOS: www/mk/ttf2png,v 1.8 2016/11/02 16:16:26 tg Exp $
 *-
 * Copyright (c) 2009, 2016
 *  mirabilos <m@mirbsd.org>
 *
 * 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);

您看过手册中提供的示例了吗?它们可能会有所帮助:我已经看过了所有的示例,似乎我必须通过测试像素颜色来修剪图像,因为文本输出太不准确了。当我绘制文本时,每个文本都有一个不同的偏移量,因此它总是超出范围。是否可以发布您使用的代码,以便我们可以对其进行实验?我添加了代码,我尝试了不同的方法来处理文本的x和y坐标,没有太大的区别。也许最好尝试一种不同的方法,比如按像素颜色修剪图像。问题是GD没有使用“字体度量”,它给出字符“基线”的值和偏移量。Imagack确实使用了这些值,因此更精确。基本上,在GD中,您最终会遇到一些依赖于“字体系列”的“捏造”因素。让“imagick”在“windows”上工作“不太有趣”,我还没有管理它。@MarcielFonseca这是一个PNG,请将它保存到文件中,或通过
xloadimage stdin
或其他合适的图像查看器将其导入。Unix工具通常会将结果转储到stdout,以便能够通过管道用作过滤器。