php - Unable to horizontally center 'm' with GD2 -
my goal draw horizontally centered m
. therefore calculate width of letter, substract value total width , divide 2. result should distance left (or equally right).
however, 'm' misplaced. noticed fonts may not trigger problematic behavior. note script correctly works other latin characters.
arial:
bitstream vera sans:
<?php $totalwidth = 100; $totalheight = 100; $font = 'arial.ttf'; $img = imagecreatetruecolor($totalwidth, $totalheight); $red = imagecolorallocate($img, 255, 0, 0); $fontsize = 100; $bbox = imagettfbbox($fontsize, 0, $font, 'm'); $width = max($bbox[2], $bbox[4]) - max($bbox[0], $bbox[6]); $centeredx = ($totalwidth - $width) / 2; imagettftext($img, 100, 0, $centeredx, 100, $red, $font, 'm'); imagepng($img, 'testcase.png'); imagedestroy($img);
there small space left of each letter, , different each letter. on php.net wrote solution this: http://www.php.net/manual/en/function.imagettfbbox.php#97357
you need adjust code little bit.
$totalwidth = 100; $totalheight = 100; $font = 'arial.ttf'; // change letter see different letters $letter = "m"; $img = imagecreatetruecolor($totalwidth, $totalheight); $red = imagecolorallocate($img, 255, 0, 0); $fontsize = 100; $bbox = calculatetextbox($fontsize, 0, $font, $letter); $centeredx = (($totalwidth - $bbox['width']) / 2); // here left coordinate subtracted (+ negative value) centeredx imagettftext($img, 100, 0, $centeredx + $bbox['left'], 100, $red, $font, $letter); header('content-type: image/png'); imagepng($img); imagedestroy($img);
Comments
Post a Comment