note 128726 added to function.imagettfbbox
| From: | kuchen at kekse dot biz | Date: | Wed, 12 Jul 2023 16:34:50 +0000 |
| Subject: | note 128726 added to function.imagettfbbox | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220686@lists.php.net to get a copy of this message | ||
I think
abs() is generally the wrong way handling the alignment!
My function to align horizontally and vertically works perfectly now. It's
the following code (where my image gets the full size, so if 42px is
argued with, the resulting image will be that size):
$measure = function() use(&$_text, &$_options)
{
$m = imagettfbbox($_options['pt'], 0, $_options['font'], $_text);
$minX = (float)round(min($m[0], $m[2], $m[4], $m[6]), 2);
$maxX = (float)round(max($m[0], $m[2], $m[4], $m[6]), 2);
$minY = (float)round(min($m[1], $m[3], $m[5], $m[7]), 2);
$maxY = (float)round(max($m[1], $m[3], $m[5], $m[7]), 2);
$height = $_options['px'];
$calculatedHeight = ($maxY - $minY);
$diffHeight = ($height - $calculatedHeight);
$top = $height - $maxY - ($diffHeight / 2);//$top = (($diffHeight - $minY) - ($diffHeight) /
2);
$width = ($maxX - $minX);
$left = -$minX / 2;
$_options['width'] = (float)round($width, 2);
$_options['height'] = (float)round($height, 2);
$_options['left'] = (float)round($left, 2);
$_options['top'] = (float)round($top, 2);
$_options['measure'] = array(
'minX' => $minX,
'minY' => $minY,
'maxX' => $maxX,
'maxY' => $maxY
);
return array(
'width' => &$_options['width'],
'height' => &$_options['height'],
'left' => &$_options['left'],
'top' => &$_options['top'],
'measure' => &$_options['measure']
);
};
// Used in my count.php script, see https://github.com/kekse1/count.php/blob/master/php/count.php
for the full code).
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.159.168 (proxied: 2a02:3102:8e20:8a0:1a31:bfff:fe6a:5fcf)
----
Manual Page -- https://php.net/manual/en/function.imagettfbbox.php
Edit -- https://main.php.net/note/edit/128726
Del: integrated -- https://main.php.net/note/delete/128726/integrated
Del: useless -- https://main.php.net/note/delete/128726/useless
Del: bad code -- https://main.php.net/note/delete/128726/bad+code
Del: spam -- https://main.php.net/note/delete/128726/spam
Del: non-english -- https://main.php.net/note/delete/128726/non-english
Del: in docs -- https://main.php.net/note/delete/128726/in+docs
Del: other reasons-- https://main.php.net/note/delete/128726
Reject -- https://main.php.net/note/reject/128726
Search -- https://main.php.net/manage/user-notes.php