Guest User

Untitled

a guest
Dec 16th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. // Get image dimensions
  2. $image_width = imagesx($image);
  3. $image_height = imagesy($image);
  4.  
  5. $text_bound = imageftbbox($font_size, $angle, $font, $text);
  6.  
  7. //Get the text upper, lower, left and right corner bounds
  8. $lower_left_x = $text_bound[0];
  9. $lower_left_y = $text_bound[1];
  10. $lower_right_x = $text_bound[2];
  11. $lower_right_y = $text_bound[3];
  12. $upper_right_x = $text_bound[4];
  13. $upper_right_y = $text_bound[5];
  14. $upper_left_x = $text_bound[6];
  15. $upper_left_y = $text_bound[7];
  16.  
  17.  
  18. //Get Text Width and text height
  19. $text_width = $lower_right_x - $lower_left_x; //or $upper_right_x - $upper_left_x
  20. $text_height = $lower_right_y - $upper_right_y; //or $lower_left_y - $upper_left_y
  21.  
  22. //Get the starting position for centering
  23. $start_x_offset = ($image_width - $text_width) / 2;
  24. $start_y_offset = ($image_height - $text_height) / 2;
  25.  
  26. // Add text to image
  27. imagettftext($image, $font_size, $angle, $start_x_offset, $start_y_offset, $color, $font, $text);
Add Comment
Please, Sign In to add comment