【问题标题】:Adjusting centered text font size based on image size. How?根据图像大小调整居中的文本字体大小。如何?
【发布时间】:2020-06-01 20:17:23
【问题描述】:

通过一些研究,我创建了以下代码来生成一个简单的图像,该图像的中心有一个文本字符串,显示该图像的分辨率。挑战在于,当您将 X 和 Y 尺寸更改为更大或更小的值时,字体大小保持不变。

我的问题是,当用户在图像大小之间进行选择时,什么是“增大”或“缩小”字体大小的好方法?

我的一个想法是计算预定区域,将它们添加到键值对数组中,其中值是给定区域范围的硬编码字体大小。

也许你有一个更简单的想法?

PS:我使用的字体可以在这里从谷歌字体下载:

Montserrat-SemiBold

输出如下所示:

还有代码:

<?php

  // We Declare our canvas width & height dimensions
  $myCanvasWidth  = 256;
  $myCanvasHeight = 256;

  // We Create an empty and dark canvas from these dimensions above
  $myCanvas = imagecreate($myCanvasWidth, $myCanvasHeight) or die("Can't create image!");
  // We Allocate a color to be used as the canvas background
  $colorIndigo = imagecolorallocate($myCanvas, 0x3F, 0x51, 0xB5);
  // We Apply color as canvas background
  imagefill($myCanvas, 0, 0, $colorIndigo);

  // We Allocate a color to be used with the canvas text
  $colorWhite = imagecolorallocate($myCanvas, 0xFF, 0xFF, 0xFF);

  // We Declare our TTF font path in Windows 10...
  $myFont = 'J:\Montserrat-SemiBold.ttf';
  // We set the font size
  $myFontSize = 16;
  // We set the text angle
  $myTextAngle = 0;

  // We Declare the text string to be drawn on canvas...
  $myText = $myCanvasWidth . ' x ' . $myCanvasHeight;

  // We Calculate and return the bounding box in pixels for the text string to be drawn on canvas...
  $myTextBoundingBox = imageftbbox($myFontSize, $myTextAngle, $myFont, $myText);

  // Get the text upper, lower, left and right corner bounds of our text bounding box...
  $lower_left_x  = $myTextBoundingBox[0]; 
  $lower_left_y  = $myTextBoundingBox[1];
  $lower_right_x = $myTextBoundingBox[2];
  $lower_right_y = $myTextBoundingBox[3];
  $upper_right_x = $myTextBoundingBox[4];
  $upper_right_y = $myTextBoundingBox[5];
  $upper_left_x  = $myTextBoundingBox[6];
  $upper_left_y  = $myTextBoundingBox[7];

  // Get Text Width and Height
  $myTextWidth  =  $lower_right_x - $lower_left_x; //or  $upper_right_x - $upper_left_x
  $myTextHeight = $lower_right_y - $upper_right_y; //or  $lower_left_y - $upper_left_y

  //Get the starting position for centering
  $start_x_offset = ($myCanvasWidth - $myTextWidth) / 2;
  $start_y_offset = (($myCanvasHeight - $myTextHeight) + $myFontSize * 2) / 2;

  // Write text to the image using TrueType fonts
  imagettftext($myCanvas, $myFontSize, $myTextAngle, $start_x_offset, $start_y_offset, $colorWhite, $myFont, $myText);

  // Draw a horizontal dashed line for reference only
  imagedashedline($myCanvas, 0, $myCanvasHeight/2, $myCanvasWidth, $myCanvasHeight/2, $colorWhite);
  // Draw a vertical dashed line for reference only
  imagedashedline($myCanvas, $myCanvasWidth/2, 0, $myCanvasWidth/2, $myCanvasHeight, $colorWhite);

  // We se the correct http header for png images...
  header('Content-Type: image/png');

  // We Output a PNG image to either the browser or a file
  imagepng($myCanvas);
  // Free any memory associated with myCanvas; image. 
  imagedestroy($myCanvas);

?>

【问题讨论】:

  • 试试$fontSize = 16;$myFontSize = $myCanvasWidth/$fontSize;。假设 x,y 尺寸为 $myCanvasWidth$myCanvasHeight。您可能需要稍微调整一下比例,但字体大小会因宽度大小而动态变化。
  • 戴尔,您的评论是“PHP”答案。它有效。

标签: php gd


【解决方案1】:

这是作为答案实施的上述评论。有趣的是,即使线条和字体看起来没有对齐,它们实际上是居中的。

中心的“x”造成不对齐的错觉使用等宽字体以实现更不必要的精确对齐的可能副作用..?!

再说一遍.. 等宽字体不是很好看...

调整大小输出示例:

<?php

  // We Declare our canvas width & height dimensions
  $myCanvasWidth  = 640;
  $myCanvasHeight = 360;

  // We Create an empty and dark canvas from these dimensions above
  $myCanvas = imagecreate($myCanvasWidth, $myCanvasHeight) or die("Can't create image!");
  // We Allocate a color to be used as the canvas background
  $colorIndigo = imagecolorallocate($myCanvas, 0x3F, 0x51, 0xB5);
  // We Apply color as canvas background
  imagefill($myCanvas, 0, 0, $colorIndigo);

  // We Allocate a color to be used with the canvas text
  $colorWhite = imagecolorallocate($myCanvas, 0xFF, 0xFF, 0xFF);

  // We Declare our TTF font path in Windows 10...
  $myFont = 'J:\Montserrat-SemiBold.ttf';

  // Static font seed value...
  $fontSize = 16;
  // We set the dynamic font size
  $myFontSize = $myCanvasWidth / $fontSize;
  // We set the text angle
  $myTextAngle = 0;

  // We Declare the text string to be drawn on canvas...
  $myText = $myCanvasWidth . ' x ' . $myCanvasHeight;

  // We Calculate and return the bounding box in pixels for the text string to be drawn on canvas...
  $myTextBoundingBox = imageftbbox($myFontSize, $myTextAngle, $myFont, $myText);

  // Get the text upper, lower, left and right corner bounds of our text bounding box...
  $lower_left_x  = $myTextBoundingBox[0]; 
  $lower_left_y  = $myTextBoundingBox[1];
  $lower_right_x = $myTextBoundingBox[2];
  $lower_right_y = $myTextBoundingBox[3];
  $upper_right_x = $myTextBoundingBox[4];
  $upper_right_y = $myTextBoundingBox[5];
  $upper_left_x  = $myTextBoundingBox[6];
  $upper_left_y  = $myTextBoundingBox[7];

  // Get Text Width and Height
  $myTextWidth  =  $lower_right_x - $lower_left_x; //or  $upper_right_x - $upper_left_x
  $myTextHeight = $lower_right_y - $upper_right_y; //or  $lower_left_y - $upper_left_y

  //Get the starting position for centering
  $start_x_offset = ($myCanvasWidth - $myTextWidth) / 2;
  $start_y_offset = (($myCanvasHeight - $myTextHeight) + $myFontSize * 2) / 2;

  // Write text to the image using TrueType fonts
  imagettftext($myCanvas, $myFontSize, $myTextAngle, $start_x_offset, $start_y_offset, $colorWhite, $myFont, $myText);

  // Draw a horizontal dashed line for reference only
  imagedashedline($myCanvas, 0, $myCanvasHeight/2, $myCanvasWidth, $myCanvasHeight/2, $colorWhite);
  // Draw a vertical dashed line for reference only
  imagedashedline($myCanvas, $myCanvasWidth/2, 0, $myCanvasWidth/2, $myCanvasHeight, $colorWhite);

  // We set the correct http header for png images...
  header('Content-Type: image/png');

  // We Output a PNG image to either the browser or a file
  imagepng($myCanvas);
  // Finally, we free any memory associated with myCanvas; the image. 
  imagedestroy($myCanvas);

?>

imageftbbox() 函数图示。

imageftbbox() 返回一个包含 8 个元素的数组,代表 四个点构成文本的边界框

【讨论】:

  • 如果您愿意,请随时取消我的编辑。只需一个完整的 PHP 示例即可。谢谢!
猜你喜欢
  • 2013-08-28
  • 1970-01-01
  • 2020-01-27
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
  • 2018-01-25
  • 2012-08-07
相关资源
最近更新 更多