Jump to content

Recommended Posts

I am using PHP's image creation functions to create dynamic titles on the fly with a corporate logo.  Now there are 2 versions of the said logo, a regular ttf and an extra bold ttf.

 

I have found it easy to use one or the other.  But what I REALLY want to do is have the first word in one font and the second word in another.  But as I dont know how long the first word will be....I dont know how to space the second word.

 

Now there has to be someone out there who has done this or knows how to calculate the spacing.

 

Heres my code for the single font.

 

<?php
$title="The Main Heading";
$imgfile= 'blank.png';
$outfile=@ImageCreateFromPNG($imgfile);
$white=imagecolorallocate($outfile,255,255,255);
$grey=imagecolorallocate($outfile,128,128,128);

$font1='regular.TTF';
$font2='extra-bold.TTF';

imagettftext ($outfile, 20,0,11,31,$grey,$font1,$title);


imagepng ($outfile);
imagedestroy($outfile);

?>

Link to comment
https://forums.phpfreaks.com/topic/63817-create-image-with-two-fonts/
Share on other sites

thanks for that.  This will indeed display 2 fonts, but the issue is not the font as much as the spacing on the x-axis.

 

The words are dynamically generated from a DB

 

THEREFORE i would need to calculate the distance created by word one.  Gets very complicated when you consider that each letter can take up a different spacing.

 

To recap.

 

I am looking to replace H1 tags with an dynamic image. AND use 2 fonts AND remove the blank space from between the words.

 

SO

 

"Primary Heading"

becomes

PrimaryHeading

 

oh...and I need to either caluculate the total image size and then create a imge.png with dynamic sizes or I need to be able to alighn the whole lot right.

 

So nothing too taxing...lol

 

C'mon you freaks...someone out there must want to crack this challenge with me.

 

 

 

 

 

 

What you want to do is not complicated - there are MANY examples of this as code and tutorials.  Have you searched these forums? I believe this was discussed a few months ago.

 

 

Here's some reading for you - http://us2.php.net/manual/en/ref.image.php

It includes the following:

imagefontwidth — Get font width

imageftbbox — Give the bounding box of a text using fonts via freetype2

imagefttext — Write text to the image using fonts using FreeType 2

 

 

 

 

sample

 

::dualfont.php::

<?php
$light = 'c:/windows/fonts/bauhausl.ttf';
$heavy = 'c:/windows/fonts/bauhaush.ttf';

$txt = isset($_GET['text']) ? $_GET['text'] : 'ultimate driving machine';

$ta = explode(' ', $txt);
/**
* alternate the fonts and calc widths
*/
$k = count($ta);
$widths = array();
for ($w=0; $w < $k; $w++)
{
    $word = $ta[$w];
    $font = $w % 2 ? $heavy : $light;
    $bb = imagettfbbox(24, 0, $font, $word);
    $widths[$w] = abs($bb[2]-$bb[0]);
}
$totalWidth = array_sum($widths);

$im = imagecreate ($totalWidth+20, 50);
$bg = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$txtcol = imagecolorallocate($im, 0x22,0xAA,0x22);
imagerectangle($im,0,0,$totalWidth+19, 49, $txtcol);

/**
* write the text 
*/
$x = 10;
$y = 35;
for ($w=0; $w < $k; $w++)
{
    $word = $ta[$w];
    $font = $w % 2 ? $heavy : $light;
    imagettftext($im, 24, 0, $x, $y, $txtcol,$font,$word);
    $x += $widths[$w];
}

header("content-type: image/png");
imagepng($im);
imagedestroy($im);

?> 

 

Usage

 

<img src="dualfont.php?text=vorsprung durch technik">

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.