我已经看过很多关于如何让文本在图像中心对齐的演示,但我希望让文本中心对齐,以便它看起来与 CSStext-align:center
或 photoshop 的中心对齐相同。
我怎样才能做到这一点imagettftext
?
到目前为止,这是我的代码:
$new_image = imagecreatefromjpeg(get_template_directory()."/images/homepage/blackboard.jpg"); // Load up the blackboard image.
$white = imagecolorallocate($new_image, 255, 255, 255); // Create a white colour
$font_path = get_template_directory()."/fonts/PermanentMarker.ttf"; // Set font
$text = get_field("blackboard_message", $page->ID); // Create text object
$text_length = 19;
$text = wordwrap($text, $text_length, "\n", true);
imagettftext($new_image, 16, 0, 60, 185, $white, $font_path, $text); // Add text to image.
$mask = imagecreatefrompng(get_template_directory()."/images/homepage/blackboard-mask.png"); // Load the mask
imagesavealpha($mask, false); // Do not save full alpha channels
imagealphablending($mask, false); // Disable alpha blending
imagecopy($new_image, $mask, 55, 160, 0, 0, 224, 285); // Copy mask on top of blackboard image.
imagejpeg($new_image, get_template_directory()."/images/homepage/blackboard/blackboard-message-$modified_unix_timestamp.jpg");
最终结果应如下所示: