我已经看过如此多关于如何将文本对齐在图像中心的演示,但我希望文本中心对齐,使其看起来与CSS text-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");
最终结果应如下所示: