Jump to content

Recommended Posts

I'm trying to place my text in the center of an image using imagettftext().

 

I've been searching around but can't seem to find anything on text only on images which aren't helping.

 

imagettftext() returns an array with 8 elements making up the location of the text's bounding box. But this confuses me cause i have to use imagettftext to write on the image.. Getting the size of the bounding box afterwards isn't much help really?

 

So yeah can anyone help with the theory of this? My guess would be that i have to figure out the size of the text first.. Then work out where it should be placed on the image. And then use imagettftext with the co-ords I find.

 

Here's my code for my image maker anyways.

 

<?php
// Get the text sent with the url
$text = $_GET['text'];

// Create an image from our original
$image = imageCreateFromJPEG('images/top.jpg');

// Set the font path
$font = "decoder.ttf";

// Set a colour to use
$black = imagecolorallocate($image, 0, 0, 0);

// Calculate where to place our text.
/// ???

// Write on the image
$textsize = imagettftext($image, 14, 0, 27, 15, $black, $font, $text); // ignore current placement values.

// Set the header to recieve our image
Header('Content-type: image/png');

// send the image
imagepng($image);

// Clean up
imagedestroy($image);

?>

 

Thanks in advance for any help.

Link to comment
https://forums.phpfreaks.com/topic/67045-solved-watermark-placement-using-gd/
Share on other sites

I use imagettfbbox to get the size of the text, like so....

 

$size = imagettfbbox($font_size, 0, $font, strtoupper($_POST['hp_serial']));
$xsize = abs($size[0]) + abs($size[2]);
$left = round(($hp_imagesize[0] - $xsize) / 2);
imagettftext($hp_image, $font_size, 0, $left, 625, $white, $font, strtoupper($_POST['hp_serial']));

 

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.