Jump to content

Recommended Posts

Is see on php.net for image that this will place the text centered on the image:
[code]
$px    = (imagesx($im) - 7.5 * strlen($string)) / 2;
[/code]

However i need to center it on a certain area in the image not the image iself. When doing a pdf you can set the XY for the boundingbox not sure if this can be done in GD as that would do the trick. Any suggestions?????
Link to comment
https://forums.phpfreaks.com/topic/4265-solved-gd-help/#findComment-15509
Share on other sites

If you want to centre in a certain area then if X is the centre point of the area, the start of the text string ($px) will be at (X - textwidth/2)

This will taka text string, split it into lines of approx 25 charcters, and centre the lines of text in the image

[code]<?php

$font = "C:/windows/fonts/arial.ttf";

$text = $_GET['text'];

$im = imagecreate (200, 80);

$black = imagecolorallocate($im, 0x00, 0x00, 0x00);
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);

$text_array = explode ("\n", wordwrap($text, 25));

$image_width = imagesx($im);
$y = 5;
foreach ($text_array as $line) {
         $box = imagettfbbox(12,0,$font,$line);
         $text_width = $box[2] - $box[0];
               // calc pos to centre the text
         $x = ($image_width - $text_width) / 2;
         $y += 20;

         imagettftext($im, 12, 0, $x, $y, $white, $font, $line);

}

header("content-type: image/png");
imagepng($im);
imagedestroy($im);
?>[/code]

Save it as "centreText.php" and place image on page with

<IMG src='centreText.php?text='Now+is+the+time+for+all+good+men+to+come+to+the+aid+of+the+party'>
Link to comment
https://forums.phpfreaks.com/topic/4265-solved-gd-help/#findComment-15584
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.