Jump to content

[SOLVED] GD Help


tobeyt23

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

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.