Center text in php ImageCreate

  image, imagecreatefrompng, php

I want to clean up this dirty code. But the problem is I want the text to vertical center in the squares.
The text can change but the center point for the text needs to be the same.
If it’t possible I want to break lines after a space but then the text needs to be horizontal centered / aligned.

$A1 = "FREDDY KREUGER";
$A2 = "JIGSAW";
$A3 = "MICHAEL MYERS";
$A4 = "THE BABADOOK";
$A5 = "NORMAN BATES";
$A6 = "HANNIBAL LECTER";
$A7 = "PETER VINCENT";
$A8 = "LEATHERFACE";
$A9 = "DRACULA";
$A10 = "RIPLEY";
$A11 = "VAN HELSING";
$A12 = "FRANKENSTEIN'S MONSTER";
$A13 = "SADAKO";
$A14 = "PINHEAD";
$A15 = "PENNYWISE";
$A16 = "JASON VOORHEES";

$my_img = imageCreateFromPng("bingo_background_1.png");
imagealphablending($my_img, true);
imagesavealpha($my_img, true);

    $text_color = imagecolorallocate($my_img, 255, 255, 255);
    
    $font = "Hey Comic.ttf";
    $size = "30";
    
    $box1 = imageftbbox($size, 0, $font, $A1);
    $x1 = $box1[0] + 80;
    $y1 = $box1[1] + 250;
    
    $box2 = imageftbbox($size, 0, $font, $A2);
    $x2 = $box2[0] + 390;
    $y2 = $box2[1] + 250;
    
    $box3 = imageftbbox($size, 0, $font, $A3);
    $x3 = $box3[0] + 680;
    $y3 = $box3[1] + 250;
    
    $box4 = imageftbbox($size, 0, $font, $A4);
    $x4 = $box4[0] + 970;
    $y4 = $box4[1] + 250;
    
    $box5 = imageftbbox($size, 0, $font, $A5);
    $x5 = $box5[0] + 80;
    $y5 = $box5[1] + 550;
    
    $box6 = imageftbbox($size, 0, $font, $A6);
    $x6 = $box6[0] + 390;
    $y6 = $box6[1] + 550;
    
    $box7 = imageftbbox($size, 0, $font, $A7);
    $x7 = $box7[0] + 680;
    $y7 = $box7[1] + 550;
    
    $box8 = imageftbbox($size, 0, $font, $A8);
    $x8 = $box8[0] + 970;
    $y8 = $box8[1] + 550;
    
    $box9 = imageftbbox($size, 0, $font, $A9);
    $x9 = $box9[0] + 80;
    $y9 = $box9[1] + 860;
    
    $box10 = imageftbbox($size, 0, $font, $A10);
    $x10 = $box10[0] + 390;
    $y10 = $box10[1] + 860;
    
    $box11 = imageftbbox($size, 0, $font, $A11);
    $x11 = $box11[0] + 680;
    $y11 = $box11[1] + 860;
    
    $box12 = imageftbbox($size, 0, $font, $A12);
    $x12 = $box12[0] + 960;
    $y12 = $box12[1] + 860;
    
    $box13 = imageftbbox($size, 0, $font, $A13);
    $x13 = $box13[0] + 80;
    $y13 = $box13[1] + 1160;
    
    $box14 = imageftbbox($size, 0, $font, $A14);
    $x14 = $box14[0] + 390;
    $y14 = $box14[1] + 1160;
    
    $box11 = imageftbbox($size, 0, $font, $A15);
    $x15 = $box15[0] + 680;
    $y15 = $box15[1] + 1160;
    
    $box16 = imageftbbox($size, 0, $font, $A16);
    $x16 = $box16[0] + 970;
    $y16 = $box16[1] + 1160;
    
    imagettftext($my_img, $size, 0, $x, $y, $text_color, $font, $title);
    imagettftext($my_img, $size, 0, $x1, $y1, $text_color, $font, $A1);
    imagettftext($my_img, $size, 0, $x2, $y2, $text_color, $font, $A2);
    imagettftext($my_img, $size, 0, $x3, $y3, $text_color, $font, $A3);
    imagettftext($my_img, $size, 0, $x4, $y4, $text_color, $font, $A4);
    imagettftext($my_img, $size, 0, $x5, $y5, $text_color, $font, $A5);
    imagettftext($my_img, $size, 0, $x6, $y6, $text_color, $font, $A6);
    imagettftext($my_img, $size, 0, $x7, $y7, $text_color, $font, $A7);
    imagettftext($my_img, $size, 0, $x8, $y8, $text_color, $font, $A8);
    imagettftext($my_img, $size, 0, $x9, $y9, $text_color, $font, $A9);
    imagettftext($my_img, $size, 0, $x10, $y10, $text_color, $font, $A10);
    imagettftext($my_img, $size, 0, $x11, $y11, $text_color, $font, $A11);
    imagettftext($my_img, $size, 0, $x12, $y12, $text_color, $font, $A12);
    imagettftext($my_img, $size, 0, $x13, $y13, $text_color, $font, $A13);
    imagettftext($my_img, $size, 0, $x14, $y14, $text_color, $font, $A14);
    imagettftext($my_img, $size, 0, $x15, $y15, $text_color, $font, $A15);
    imagettftext($my_img, $size, 0, $x16, $y16, $text_color, $font, $A16);
    
    $save = "bingocard.png";
    imagepng($my_img, $save);

The result I have right now is

enter image description here

Source: Ask PHP

LEAVE A COMMENT