When using PHP for image processing, you often need to add text to the picture. To make the text look more beautiful, horizontal centering is a common and important operation. This article will introduce how to use PHP's imagefontwidth() function, combined with other related functions, to achieve the horizontal centering alignment effect of text in the picture.
imagefontwidth() is a function in the PHP GD library that gets the width of a single character under a specified font size. Its basic usage is as follows:
int imagefontwidth ( int $font )
$font is the font size, with values ranging from 1 to 5.
The return value is the width (pixel) of a single character at that font size.
After understanding the width of a single character, we can calculate the width of the entire piece of text based on the length of the string, thereby calculating the horizontally centered starting x coordinate.
Create a picture.
Sets the font size and text content.
Calculate the pixel width of the text: character width × number of characters.
Calculate the starting x coordinate when centered: image width minus text width and divide by 2.
Use the imagestring() function to draw text on the image.
Output or save the picture.
Here is a complete example showing how to use imagefontwidth() to achieve horizontal centered alignment of text.
<?php
// Create a wide sheet 400px,high 100px Real color pictures
$width = 400;
$height = 100;
$image = imagecreatetruecolor($width, $height);
// Define the color
$bg_color = imagecolorallocate($image, 255, 255, 255); // White background
$text_color = imagecolorallocate($image, 0, 0, 0); // Black text
// Fill background color
imagefilledrectangle($image, 0, 0, $width, $height, $bg_color);
// Text content and font size
$text = "Welcome to visit m66.net";
$font = 5; // Font size,scope1-5
// Get single character width
$char_width = imagefontwidth($font);
// Calculate the width of the entire text
$text_width = $char_width * strlen($text);
// Calculate the beginning of textxcoordinate,Achieve the center level
$x = ($width - $text_width) / 2;
// ycoordinate,Center vertically
$char_height = imagefontheight($font);
$y = ($height - $char_height) / 2;
// Write text on the picture
imagestring($image, $font, $x, $y, $text, $text_color);
// Output pictures to browser
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
?>
In this code:
imagefontwidth($font) returns the width (pixels) of a single character when the font size is 5.
Use strlen($text) to calculate the number of string characters.
The product is the total width of the text.
Use the image width to subtract the text width and divide by 2 to calculate the centered starting point x .
This way the text can be displayed horizontally.
imagefontwidth() is one of the key functions to achieve text-level centering. Through it, combining the string length and image size, it is easy to calculate the starting drawing position of the text to ensure that the text is displayed in the center. Combined with imagefontheight(), you can achieve more accurate vertical centering.