Jump to content

Text Placement On Image In GD Library


mouse77e

Recommended Posts

I thought I had this sorted but obviously not. The idea was to centre the text, in this cast Christopher in the centre of the badge, In stead of centring the text in the middle of the badge the text starts as of the middle of the badge.

 

The Code:

<?php
$image = ImageCreateFromPNG("nametag.png");
$color = imagecolorallocate($image, 0x255, 0x255, 0x255);
$colorShadow = imagecolorallocate($image, 0x66, 0x66, 0x66);
$font = 'font1.ttf';
$fontSize = "24";
$fontRotation = "0";
$str = "Christopher";

$width = 180;
$height = 144;

$font_width = ImageFontWidth($font);
$font_height = ImageFontHeight($font);

/* Text Width */
$text_width = $font_width * strlen($text);

/* Position to align in center */
$position_center = ceil(($width - $text_width) / 2);

/* Text Height */
$text_height = $font_height;

/* Position to align in abs middle */
$position_middle = ceil(($height - $text_height) / 2 + 30);

/* Top Level */
ImageTTFText($image, $fontSize, $fontRotation,  $position_center, $position_middle, $color, $font, $str);

header("Content-Type: image/PNG");
ImagePng ($image);
imagedestroy($image);
?>

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/190524-text-placement-on-image-in-gd-library/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.