willpower Posted August 8, 2007 Share Posted August 8, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/63817-create-image-with-two-fonts/ Share on other sites More sharing options...
marcus Posted August 8, 2007 Share Posted August 8, 2007 http://us2.php.net/imagestring try that, you could then just position them afterwards Quote Link to comment https://forums.phpfreaks.com/topic/63817-create-image-with-two-fonts/#findComment-318050 Share on other sites More sharing options...
willpower Posted August 8, 2007 Author Share Posted August 8, 2007 thanks for the reply. try what exactly....maybe im two tired, but i aint seeing the example there that would help me. I really dont want to have to create two images and join them. Any advances...? Quote Link to comment https://forums.phpfreaks.com/topic/63817-create-image-with-two-fonts/#findComment-318068 Share on other sites More sharing options...
willpower Posted August 8, 2007 Author Share Posted August 8, 2007 * a little bump* Quote Link to comment https://forums.phpfreaks.com/topic/63817-create-image-with-two-fonts/#findComment-318142 Share on other sites More sharing options...
tippy_102 Posted August 8, 2007 Share Posted August 8, 2007 imagestring ( resource $image, int $font, int $x, int $y, string $string, int $color ) Use this for each line with 2 different fonts. Quote Link to comment https://forums.phpfreaks.com/topic/63817-create-image-with-two-fonts/#findComment-318203 Share on other sites More sharing options...
willpower Posted August 8, 2007 Author Share Posted August 8, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/63817-create-image-with-two-fonts/#findComment-318340 Share on other sites More sharing options...
tippy_102 Posted August 8, 2007 Share Posted August 8, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/63817-create-image-with-two-fonts/#findComment-318609 Share on other sites More sharing options...
Barand Posted August 8, 2007 Share Posted August 8, 2007 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"> Quote Link to comment https://forums.phpfreaks.com/topic/63817-create-image-with-two-fonts/#findComment-318718 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.