RoninStretch Posted August 28, 2007 Share Posted August 28, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/67045-solved-watermark-placement-using-gd/ Share on other sites More sharing options...
tippy_102 Posted August 28, 2007 Share Posted August 28, 2007 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'])); Quote Link to comment https://forums.phpfreaks.com/topic/67045-solved-watermark-placement-using-gd/#findComment-336460 Share on other sites More sharing options...
RoninStretch Posted August 29, 2007 Author Share Posted August 29, 2007 Thanks thats helpful.. But one thing: How do i figure out the font size in pixels? imagettfbbox needs the size in pixels and on imagettftext im setting the size in points. grr. Quote Link to comment https://forums.phpfreaks.com/topic/67045-solved-watermark-placement-using-gd/#findComment-336962 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.