Does anyone know a simple way to do letter spacing/kerning using imagettftext? I have my script working just as I need now, but I could really do with the generated text having the CSS style
letter-spacing: -0.01em;
so it matches the standard text on the page, but I don't see any way to do this easily. I did find the following thread relating to this, but I've tried to fit the answers into my code and none of them had the desired effect.
php imagettftext letter spacing
My current code is as follows
<?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 = 83;
$bbox = imageftbbox($size2, 0, $fontBold, $textToUse);
$width = 1020;
$height = 110;
$im = imagecreatetruecolor($width, $height);
$x = ($width - ($bbox[4] - $bbox [0])) / 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);
$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, $x, -$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($im);