0

Ok straight to the point. I have a new project I'm working on and I have never had the chance to work with GD before. I have worked with PHP for around 3 years so I'm not new to it just never had a reason until now to explore these features.

So my code is:

<?php
header("Content-type: image/png");
$string = $_POST['userinput'];;

$im = imagecreatefrompng("images/tshirt.png");
$x = 175;
$y = 240;

$font = 'arial.ttf';
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);   //white background
$textColor = imagecolorallocate ($im, 0, 0, 0);   //black text
imagettftext($im, 32, 0, $x, $y, $textColor, $font, $string);
imagepng ($im); 
imagedestroy($im);


?>

Now I know $x and $ y are what control the coordinates, but I need the code to be able to resize and center depending on what someone types in. There is a limit of 15 characters that will be placed on this. The application is for a custom tshirt website. I appreciate any help.

I have tried a few things already to no avail so I'm hoping someone can shed some light on what I'm not seeing.

Thanks, Justin

  • watch [this](http://stackoverflow.com/questions/7393319/resize-images-with-php) –  Oct 18 '11 at 14:31

1 Answers1

0

You can use imagettfbox() to do calculate the size of the resulting text. That'll let you do the centering calculations to generate coordinates for the actual text drawing you do with imagettftext().

Marc B
  • 356,200
  • 43
  • 426
  • 500