[英]Using PHP imageftbbox - adjust image size to font size output?
我正在使用 PHP imageftbbox 創建一些日期,這些日期被計算並呈現為圖像。
他們目前渲染到一個圖像塊,所以我的字體大小在很大程度上與創建更高分辨率的結果無關。 我遇到的問題是,如果我有例如結果提供了兩個不同的日期(比如二月和三月),那么字體大小在我的結果頁面中顯示的不同,因為顯然它正在擴展三月日期以填充該框。
如何使大小適用於實際吐出的字體大小 - 例如,無論創建的內容長度如何,我都可以擁有相同大小的字體 output?
<?php
include 'deliverytimes.php';
$date = new DateTime();
$now = date("Y-m-d H:i:s");
$h = date("H:i:s");
$days = explode(",", $businessDaysToAdd);
if (count($days) > 1) {
$two_weekdays_later_1 = strtotime(date("Y-m-d H:i:s", strtotime($now)) . " +" . $days[0] . " weekdays $h");
$date_1 = new DateTime("@$two_weekdays_later_1");
$formattedDeliveryDate_1 = $date_1->format('jS M');
$formattedDeliveryDate_3 = $date_1->format('jS \o\f F');
$two_weekdays_later_2 = strtotime(date("Y-m-d H:i:s", strtotime($now)) . " +" . $days[1] . " weekdays $h");
$date_2 = new DateTime("@$two_weekdays_later_2");
$formattedDeliveryDate_2 = $date_2->format('jS M.');
$formattedDeliveryDate_4 = $date_2->format('jS \o\f F');
$formattedDeliveryDate1 = $formattedDeliveryDate_3;
$formattedDeliveryDate2 = $formattedDeliveryDate_4;
$formattedDeliveryDate = "If ordered today we estimate delivery to be approximately between " . $formattedDeliveryDate_1 . " and " . $formattedDeliveryDate_2;
} else {
$h = date("H:i:s");
$two_weekdays_later = strtotime(date("Y-m-d H:i:s", strtotime($now)) . " +" . $businessDaysToAdd . " weekdays $h");
$date = new DateTime("@$two_weekdays_later");
$formattedDeliveryDate = "If ordered today we estimate delivery approximately by " . $date->format('l, jS M.');
}
$defaultOutput = 'main';
$textMobile = isset($_REQUEST['mobile']) ? $_REQUEST['mobile'] : $defaultOutput;
switch($textMobile) {
case "main":
$textToUse = $formattedDeliveryDate;
break;
case "p1":
$textToUse = $formattedDeliveryDate1;
break;
case "p2":
$textToUse = $formattedDeliveryDate2;
break;
}
// Path to our font file
$font = './Inter-SemiBold.ttf';
$fontBold = './Inter-Bold.ttf';
$size = 24;
$size2 = 82;
$spacing = 0;
$bbox = imageftbbox($size2, 0, $fontBold, $textToUse);
$width = $bbox[2] - $bbox[6];
$height = $bbox[3] - $bbox[7];
$im = imagecreatetruecolor($width, $height);
$xcoord = ($width - $bbox[4]) / 2;
imagealphablending($im, false);
imagesavealpha($im, true);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$grey = imagecolorallocate($im, 161, 161, 168);
$trans = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefilledrectangle($im, 0, 0, $width, $height, $trans);
//$red = imagecolorallocate($im, 239, 73, 52);
$defaultTextColour = 'white';
$textColour = isset($_REQUEST['colour']) ? $_REQUEST['colour'] : $defaultTextColour;
switch($textColour) {
case "white":
$textColourUse = $white;
break;
case "black":
$textColourUse = $black;
break;
case "grey":
$textColourUse = $grey;
break;
}
// Write it
imagettftext($im, $size2, 0, -$bbox[6], -$bbox[7], $textColourUse, $fontBold, $textToUse);
// Output to browser
header('Content-Type: image/png');
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
imagepng($im);
imagedestroy($i'm);
我的 CSS 只是
.phpimage2 {max-width:90%; display: block; margin:10px auto 5px auto}
從我所看到的,給定您的代碼,您生成的圖像的寬度取決於$textToUse
變量。 我嘗試了您的代碼,這就是我得到的(原始大小的一半):
我最好的猜測是您在另一個頁面中使用這些圖像,您已經固定了這些圖像的顯示寬度。 由於March
圖像的寬度小於February
圖像,因此它將被拉伸到與February
圖像相同的寬度。 您在頁面上看到的結果可能是這樣的:
但是,這只是一個猜測,因為您問題中的代碼不允許我們實際重現您的問題。 另請參閱:如何創建最小的、可重現的示例。
解決方案是什么? 您可以使用 CSS 來縮放圖像: transform: scale(0.5);
,這將允許您將其減小到固定的字體大小。 這可能不適用於響應式頁面,其中邊界框的大小可能變化很大。 這完全取決於圖像是如何嵌入到頁面中的,而您還沒有告訴我們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.