Created
December 14, 2017 12:47
-
-
Save tosinamuda/ab575f9d37865a5efff04309e2195293 to your computer and use it in GitHub Desktop.
How to center a text on an image using php gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get image dimensions | |
$image_width = imagesx($image); | |
$image_height = imagesy($image); | |
$text_bound = imageftbbox($font_size, $angle, $font, $text); | |
//Get the text upper, lower, left and right corner bounds | |
$lower_left_x = $text_bound[0]; | |
$lower_left_y = $text_bound[1]; | |
$lower_right_x = $text_bound[2]; | |
$lower_right_y = $text_bound[3]; | |
$upper_right_x = $text_bound[4]; | |
$upper_right_y = $text_bound[5]; | |
$upper_left_x = $text_bound[6]; | |
$upper_left_y = $text_bound[7]; | |
//Get Text Width and text height | |
$text_width = $lower_right_x - $lower_left_x; //or $upper_right_x - $upper_left_x | |
$text_height = $lower_right_y - $upper_right_y; //or $lower_left_y - $upper_left_y | |
//Get the starting position for centering | |
$start_x_offset = ($image_width - $text_width) / 2; | |
$start_y_offset = ($image_height - $text_height) / 2; | |
// Add text to image | |
imagettftext($image, $font_size, $angle, $start_x_offset, $start_y_offset, $color, $font, $text); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment