Jump to content

Recommended Posts

Using GD library how to make the text underline.

 

I have used imagettftext ( resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text ) this function for write text to the image. I could not make underline text on image.

 

Please any one help me. Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/53237-gd-library-underline/
Share on other sites

I found a few artciles that may help you out... I've looked into it and I'm not sure how to do it either just yet, if I did learn though, it would probably be from these articles:

 

http://us.php.net/gd (Search for "Underline")

and

http://www.nyphp.org/content/presentations/GDintro/gd0.php (Search for line) - Perhaps you can combine GD Functions... I think that's what a php.net article implies is doable.

 

This right here may be able to help some too...

http://www.phpfreaks.com/tutorials/52/0.php

 

Link to comment
https://forums.phpfreaks.com/topic/53237-gd-library-underline/#findComment-263079
Share on other sites

Thank for your reply. I found the solution.

 

You given articles links was very useful.

 

This is the example code

require_once ('Image_Toolbox.class.php');

$text="Underline.%0D________";

$text=urldecode($text);

$img = new Image_Toolbox('cardaa.jpg');

$img->addText($text, 'C:/WINDOWS/Fonts/ARIAL.ttf', '18', '#000000', '50', '100',$angle = 0);

$img ->addImage('cardaa.jpg');

$img->output();

 

Thanks.

 

 

Link to comment
https://forums.phpfreaks.com/topic/53237-gd-library-underline/#findComment-263701
Share on other sites

  • 4 years later...

This script Works :) ... I had spent alot of time on internet and collected different ideas and generate an algorithm that will underline text written on an image. Please have fun with this and post any improvement. i will be glad to see that.

<?php

// Create the image

$im = imagecreatetruecolor(399, 30);

 

// Create some colors

$white = imagecolorallocate($im, 255, 255, 255);

$black = imagecolorallocate($im, 0, 0, 0);

imagefilledrectangle($im, 0, 0, 399, 29, $white);

 

// The text to draw

 

$text = "Muhammad Aqeel Mirza";

 

// Replace path by your own font path

$font = 'arial.ttf'; // Place this file in your code directory or if font is in font directory set its path with font name as $font=$path.'arial.ttf';

 

 

// Add the text

$x=10;

$y=20;

$font_size=12;

$angle=0;

$total_width=0;

 

// getting total width of all the characters in the input string.

for($i=0; $i<strlen($text); $i++)

{

$dimensions = imagettfbbox($font_size, $angle, $font, substr($text,$i,1));

$total_width+=($dimensions[2]);

 

 

 

}

echo "<pre>";

// getting total length of the string as  a whole and compare both widths.

$dimensions = imagettfbbox($font_size, $angle, $font, $text);

echo "Dimension of full string=".$dimensions[2]."<br/>";

 

echo "Total width calcuated by algorithm=".$total_width."<br/>";

$difference=$dimensions[2]-$total_width;

 

echo "Difference=".$difference;

 

imagettftext($im, $font_size, $angle, $x, $y, $black, $font, $text);

$x2=$x+$total_width+$difference+2;

//echo $total_width;

echo "<pre/>";

imageline( $im , $x , $y+4 , $x2 , $y+4 , $black );

 

 

 

// Using imagepng() results in clearer text compared with imagejpeg()

imagepng($im,"underline.png");

echo "<img src='underline.png'/>";

imagedestroy($im);

die();

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/53237-gd-library-underline/#findComment-1247196
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.