Jump to content

PHP Create Image With Dynamic Text - Fonts Question


gfX

Recommended Posts

Hi guys,

 

Here is what I have so far:

http://www.autoshopgarage.com/new-era/generate.php

 

What I am trying to do is instead of having Line 1 and Line 2, I want to just have one big textarea so I don't have to limit the user so much.  I did it this way because I want to have two versions of text, the light version and the bold version. 

 

Is it possible to have two different fonts with just one textarea of text?  Right now I have two functions:

ImageTTFText($image, $fontSize, $fontRotation, 435, 80, $color, $font1, wordwrap($first, 18, "\n", true));

ImageTTFText($image, $fontSize, $fontRotation, 485,120, $color_black, $font2, wordwrap($last, 18, "\n", true));

 

So I would limit that to just 1, and it would automatically wordwrap but what would I put for the $font variable?  Is this even possible?  I was thinking of just using BBCode so they could type in [b*]Bold Text[/b*] and it would use the Bold version of the font if it sees that.

 

Any help is appreciated.

 

Thanks!

 

Here is the full code:

<?php

$first = $_GET['first'];

$last = $_GET['last'];

$font_color = $_GET['color'];

 

header("Content-type: image/png");

$image = imagecreatefrompng ( "banner_blank.png" );

$color_black = imagecolorallocate($image, 0, 0, 0);

$color_red = imagecolorallocate($image, 255, 0, 0);

 

if($font_color == "Red") {

$color = $color_red;

}

elseif($font_color == "Black") {

$color = $color_black;

}

 

$font1 = 'HelveticaNeueLTStd-BdEx.ttf';

$font2 = 'HelveticaNeueLTStd-LtEx.ttf';

$fontSize = "21";

ImageTTFText($image, $fontSize, $fontRotation, 435, 80, $color, $font1, wordwrap($first, 18, "\n", true));

ImageTTFText($image, $fontSize, $fontRotation, 485,120, $color_black, $font2, wordwrap($last, 18, "\n", true));

imagepng ( $image );

imagedestroy ( $image );

?>

Link to comment
Share on other sites

OK I am getting close!  After reading on Stackoverflow here is what I have now:

http://www.autoshopgarage.com/new-era/test2.php?text=testing+%3Cb%3Escript%3C/b%3E%20this%20is%20so%20cool%20i%20love%20it

 

That does what I want, the only problem with this is now I can't get the wordwrap to work!  It should wrap to the next line, but I cannot seem to get it.

 

Here is the new PHP code:

<?php
header('Content-type: image/png');

$im = imagecreatefrompng ( "banner_blank.png" );

$color = imagecolorallocate($image, 0, 0, 0);

$tmp = $_GET['text'];

$words = explode(" ", $tmp);

$x = array(0,0,230); // DUMMY ARRAY WITH X POSITION FOR FIRST WORD

$addSpace = 0;

foreach($words as $word)
{
    if($addSpace) $word = " ".$word; // IF WORD IS NOT THE FIRST ONE, THEN ADD SPACE

    if(stristr($word, "<b>"))
    {
        $font = 'HelveticaNeueLTStd-BdEx.ttf'; // BOLD FONT
	$replace = str_replace(array("<b>","</b>"), "", $word);
        $x = imagettftext($im, 20, 0, $x[2], 20, $color, $font, $replace);
    }
    else
    {
        $font = 'HelveticaNeueLTStd-LtEx.ttf'; // NORMAL FONT
        $x = imagettftext($im, 20, 0, $x[2], 20, $color, $font, $word);
    }

    $addSpace = 1;
}

imagepng($im);

imagedestroy($im);

?>

 

Anyone have any ideas?

 

Thanks!

Link to comment
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.