• 0

[PHP] GD assistance.


Question

I am attempting to pass 3 variables through the URL that are then placed on an image. They are labeled lone, ltwo, lthree there are lines 1,2, and 3 that will be displayed on the image.

I am able to get line 1 to display without any issues however everything i try to get the other two variables working does not work and results in an image being displayed that is contorted and rather messed up. Here is the code i have generated.

<?php
    $lone = $_GET['lone'];
	$ltwo = $_GET['ltwo'];
	$lthree = $_GET['lthree'];

	$font = 'font.ttf';
	if (!$size) $size = 13;
	$im = ImageCreateFromPNG('ball.png');
	// calculate position of text
	$tsize = ImageTTFBBox($size, 0, $font, $text);
	$x = 255;
	$y = 100;

	// draw text
	$black = ImageColorAllocate($im,0,0,0);
	ImageTTFText($im, $size, 0, $x, $y, $black, $font, $lone);
	header('Content-Type: image/png');
	ImagePNG($im);
?>

This is the information I am passing through the URL

/script.php?lone=Test Text 1&ltwo=Test Text 2&lthree=Test Text 3

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

I managed to get that portion to work correctly using the following code


<?php
    $lone = $_GET['lone'];
	$font = 'font.ttf';
	if (!$size) $size = 10;
	$im = ImageCreateFromPNG('ball.png');
	// calculate position of text
	$tsize = ImageTTFBBox($size, 0, $font, $text);
	$x = 222;
	$y = 100;

	$ltwo = $_GET['ltwo'];
	$font = 'font.ttf';
	if (!$size) $size = 10;
	$im = ImageCreateFromPNG('ball.png');
	// calculate position of text
	$tsize = ImageTTFBBox($size, 0, $font, $text);
	$a = 222;
	$b = 120;	

	$lthree = $_GET['lthree'];
	$font = 'font.ttf';
	if (!$size) $size = 10;
	$im = ImageCreateFromPNG('ball.png');
	// calculate position of text
	$tsize = ImageTTFBBox($size, 0, $font, $text);
	$c = 222;
	$d = 140;

	// draw text
	$black = ImageColorAllocate($im,0,0,0);
	ImageTTFText($im, $size, 0, $x, $y, $black, $font, $lone);
	ImageTTFText($im, $size, 0, $a, $b, $black, $font, $ltwo);
	ImageTTFText($im, $size, 0, $c, $d, $black, $font, $lthree);
	header('Content-Type: image/png');
	ImagePNG($im);
?>

My next question is as follows:

Is there a way to get the text to start Centered from the coordinates that i input? so if it is a 9 pixel wide image and i tell it coords x=5 that 5 pixels in would be the center of the text?

Link to comment
Share on other sites

  • 0

The imagettfbbox function you've called (although not using the result of. You're also calling it with $text instead of $lone, $ltwo, etc.) gives you the bounds of the text, but you'll need to calculate the center yourself.

Example:

// Bounding box of the text
$text_bounds = imagettfbbox($size, 0, $font, $lone);

// Text width and height
$text_width = $text_bounds[2] - $text_bounds[0];
$text_height = $text_bounds[1] - $text_bounds[5];

// Image dimensions
$image_width = imagesx($im);
$image_height = imagesy($im);

// Center the text horizontally and vertically
$x = ($image_width - $text_width) / 2;
$y = ($image_height - $text_height) / 2;

// Use the $x and $y to position the text.
imagettftext($im, $size, 0, $x, $y, $black, $font, $lone);

That will work for the first line only. To position the other lines you'll need to move them down by increasing the $y value.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.