Hey
I have a simple function to center text in an image. It’s centered horizontally but not vertically. This is my code for centering vertically:
$max_text_box_height = max(array($text_box_d[1],$text_box_d[3],$text_box_d[5],$text_box_d[7]));
$min_text_box_height = min(array($text_box_d[1],$text_box_d[3],$text_box_d[5],$text_box_d[7]));
$text_box_height = abs($max_text_box_height) - abs($min_text_box_height);
This has been driving me crazy for a full day I have tried nearly all the combinations of coordinates with and without absolute with no avail.
Thanks in advance
Regards
Michael
Sorry missed the main function:
[B]$text_box_d = imagettfbbox('30', 0, 'arial.ttf', 'hello world');[/B]
$max_text_box_height = max(array($text_box_d[1],$text_box_d[3],$text_box_d[5],$text_box_d[7]));
$min_text_box_height = min(array($text_box_d[1],$text_box_d[3],$text_box_d[5],$text_box_d[7]));
$text_box_height = abs($max_text_box_height) - abs($min_text_box_height);
When I have the height, I use the height from the image height and divide by 2 to center the text vertically. The problem is calculating the height of the outbounding box correctly.
This is some code I used a few years ago which worked for me:
<?php
$canvas = imagecreate( 200, 100 );
$black = imagecolorallocate( $canvas, 0, 0, 0 );
$white = imagecolorallocate( $canvas, 255, 255, 255 );
imagefilledrectangle( $canvas, 9, 9, 189, 89, $white );
$font = "verdana.ttf";
$text = "Title";
$size = "30";
$box = imageftbbox( $size, 0, $font, $text );
$x = (200 - ($box[2] - $box[0])) / 2;
$y = (100 - ($box[1] - $box[7])) / 2;
$y -= $box[7];
imageTTFText( $canvas, $size, 0, $x, $y, $black, $font, $text );
imagejpeg( $canvas, "Label_GD.jpg" );
ImageDestroy( $canvas );
?>
Rubble:
This is some code I used a few years ago which worked for me:
<?php
$canvas = imagecreate( 200, 100 );
$black = imagecolorallocate( $canvas, 0, 0, 0 );
$white = imagecolorallocate( $canvas, 255, 255, 255 );
imagefilledrectangle( $canvas, 9, 9, 189, 89, $white );
$font = "verdana.ttf";
$text = "Title";
$size = "30";
$box = imageftbbox( $size, 0, $font, $text );
$x = (200 - ($box[2] - $box[0])) / 2;
$y = (100 - ($box[1] - $box[7])) / 2;
$y -= $box[7];
imageTTFText( $canvas, $size, 0, $x, $y, $black, $font, $text );
imagejpeg( $canvas, "Label_GD.jpg" );
ImageDestroy( $canvas );
?>
Hey mate
Although imagerectangle would be a good fix, it wouldn’t work in my situation as the image is actually a picture so having text with borders wouldn’t look good.
If I could have the borders of the rectangle to be transparent, it would work. If the image had a solid color background, I could give the borders the same color and so they would blend together but that’s not my case.
Is there a way to hide the borders of the imagerectangle or make them transparent?
Thanks