PHP imagettftext partially bold
Asked Answered
H

4

5

I'm training my PHP skills to generate images. I need only one thing which doesn't work. I don't think it's possible on the way I want, but there should be another way. The code I already have is this:

<?php
$textSize = 10;
$text = addslashes("Wietse: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed malesuada aliquet dolor, vitae tristique lectus imperdiet nec. Pellentesque et.");
$maxWidth = 448;

//Create image
$im = imagecreate(468, 60);

// Black background and White text
$bg = imagecolorallocate($im, 0, 0, 0);
$textColor = imagecolorallocate($im, 255, 255, 255);

// Split in multiple lines
$words = explode(' ', $text);
$lines = array();
$line = "";
foreach ($words as $word) {
    $box = imagettfbbox($textSize, 0, './arial.ttf', $line . $word);
    $width = $box[4] - $box[0];
    if($width > $maxWidth) {
        $line = trim($line);
        $line .= "\n";
    }
    $line .= $word . ' ';
}

// Write the text in the image
imagettftext($im, $textSize, 0, 10, $textSize + 10, $textColor, './arial.ttf', $line); // write text to image

// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

Which results in this image:

Image

It works fine, but my name, Wietse, should be Bold. I don't think it's possible to do this on a simple way, but it should be possible. I can add another imagettftext() with a Bold Arial font, but the Lorum ipsum text should start next to the name and the second line should continue on the same X coordinates as the name. Like it is now. I also have another wish, but I think it's too difficult because this text can have any position. But if someone knows how to do this he's great. This is that the link (http://www.google.com) has an underline. I don't think I need to tell further information.

Thanks on purpose!

Hasp answered 13/7, 2012 at 14:42 Comment(0)
H
4

I've found a solution myself to get the name in bold. I have no desire to explain everything because I am the one who asked this question, so I just give the new code with the resulting image. Tomorrow I'll try to give an underline to the link. If anyone knows the answer to that I'll accept that answer. Note that I've changed the code a little more which isn't necessary to get the code working.

The code:

<?php
$username = 'WietsedeVries';
$textSize = 10;
$text = addslashes("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed malesuada aliquet dolor, vitae tristique http://www.google.com pellentesque et.");
$maxWidth = 448;

//Create image
$im = imagecreate(468, 60);

// Black background and White text
$bg = imagecolorallocate($im, 0, 0, 0);
$textColor = imagecolorallocate($im, 255, 255, 255);

// Write Bold Username in image
$usernameTotal = '@' . $username . ':';
imagettftext($im, $textSize, 0, 10, $textSize + 10, $textColor, './arialbd.ttf', $usernameTotal);

// Create white space with the width of the username
$usernameBox = imagettfbbox($textSize, 0, './arialbd.ttf', $usernameTotal);
$usernameWidth = $usernameBox[4] - $usernameBox[0];
$whiteSpace = '';
$whiteSpaceWidth = 0;
while($whiteSpaceWidth < $usernameWidth) {
    $whiteSpace .= ' ';
    $whiteSpaceBox = imagettfbbox($textSize, 0, './arial.ttf', $whiteSpace);
    $whiteSpaceWidth = $whiteSpaceBox[4] - $whiteSpaceBox[0];
}

// Split in multiple lines
$words = explode(' ', $text);
array_unshift($words, $whiteSpace);
$lines = array();
$line = "";
foreach ($words as $word) {
    $box = imagettfbbox($textSize, 0, './arial.ttf', $line . $word);
    $width = $box[4] - $box[0];
    if($width > $maxWidth) {
        $line = trim($line);
        $line .= "\n";
    }
    $line .= $word . ' ';
}

// Write the text in the image
imagettftext($im, $textSize, 0, 10, $textSize + 10, $textColor, './arial.ttf', $whiteSpace . ' ' . $line);

// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

This results in this image:

Image

Hasp answered 13/7, 2012 at 17:43 Comment(1)
If anyone else comes across this solution; the solution was to use a different .ttf file. Since itallic/bold/strike through/underline are actually completely different fonts, it's pretty easy to locate the bold/itallic version of your desired font. See how the OP uses imagettfbbox($textSize, 0, './arialbd.ttf', $usernameTotal); for bold and imagettfbbox($textSize, 0, './arial.ttf', $line . $word); for the rest of the text? 2 different .ttf filesCanaigre
M
9

In almost every font, this solution will work.

If you are creating this text:

imagettftext($jpg_image, 35, 0, $x, $y, $color, $font_path, $text2);

and if you want bold, you should add these:

imagettftext($jpg_image, 35, 0, $x, $y, $color, $font_path, $text2);
imagettftext($jpg_image, 35, 0, $x, $y+1, $color, $font_path, $text2);
imagettftext($jpg_image, 35, 0, $x, $y+2, $color, $font_path, $text2);
imagettftext($jpg_image, 35, 0, $x+1, $y, $color, $font_path, $text2);
imagettftext($jpg_image, 35, 0, $x+2, $y, $color, $font_path, $text2);
Mizuki answered 1/2, 2016 at 21:32 Comment(0)
H
4

I've found a solution myself to get the name in bold. I have no desire to explain everything because I am the one who asked this question, so I just give the new code with the resulting image. Tomorrow I'll try to give an underline to the link. If anyone knows the answer to that I'll accept that answer. Note that I've changed the code a little more which isn't necessary to get the code working.

The code:

<?php
$username = 'WietsedeVries';
$textSize = 10;
$text = addslashes("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed malesuada aliquet dolor, vitae tristique http://www.google.com pellentesque et.");
$maxWidth = 448;

//Create image
$im = imagecreate(468, 60);

// Black background and White text
$bg = imagecolorallocate($im, 0, 0, 0);
$textColor = imagecolorallocate($im, 255, 255, 255);

// Write Bold Username in image
$usernameTotal = '@' . $username . ':';
imagettftext($im, $textSize, 0, 10, $textSize + 10, $textColor, './arialbd.ttf', $usernameTotal);

// Create white space with the width of the username
$usernameBox = imagettfbbox($textSize, 0, './arialbd.ttf', $usernameTotal);
$usernameWidth = $usernameBox[4] - $usernameBox[0];
$whiteSpace = '';
$whiteSpaceWidth = 0;
while($whiteSpaceWidth < $usernameWidth) {
    $whiteSpace .= ' ';
    $whiteSpaceBox = imagettfbbox($textSize, 0, './arial.ttf', $whiteSpace);
    $whiteSpaceWidth = $whiteSpaceBox[4] - $whiteSpaceBox[0];
}

// Split in multiple lines
$words = explode(' ', $text);
array_unshift($words, $whiteSpace);
$lines = array();
$line = "";
foreach ($words as $word) {
    $box = imagettfbbox($textSize, 0, './arial.ttf', $line . $word);
    $width = $box[4] - $box[0];
    if($width > $maxWidth) {
        $line = trim($line);
        $line .= "\n";
    }
    $line .= $word . ' ';
}

// Write the text in the image
imagettftext($im, $textSize, 0, 10, $textSize + 10, $textColor, './arial.ttf', $whiteSpace . ' ' . $line);

// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

This results in this image:

Image

Hasp answered 13/7, 2012 at 17:43 Comment(1)
If anyone else comes across this solution; the solution was to use a different .ttf file. Since itallic/bold/strike through/underline are actually completely different fonts, it's pretty easy to locate the bold/itallic version of your desired font. See how the OP uses imagettfbbox($textSize, 0, './arialbd.ttf', $usernameTotal); for bold and imagettfbbox($textSize, 0, './arial.ttf', $line . $word); for the rest of the text? 2 different .ttf filesCanaigre
G
2

Try using outline / stroke. How, please check on this code.

function imagettfstroketext(&$image, $size, $angle, $x, $y, &$textcolor, &$strokecolor, $fontfile, $text, $px) {
    for($c1 = ($x-abs($px)); $c1 <= ($x+abs($px)); $c1++)
        for($c2 = ($y-abs($px)); $c2 <= ($y+abs($px)); $c2++)
            $bg = imagettftext($image, $size, $angle, $c1, $c2, $strokecolor, $fontfile, $text);
   return imagettftext($image, $size, $angle, $x, $y, $textcolor, $fontfile, $text);
}

$font_color = imagecolorallocate($im, 255, 255, 255);
$stroke_color = imagecolorallocate($im, 0, 0, 0);
imagettfstroketext($im, 60, 10, 300, 130, $font_color, $stroke_color, "wqy-microhei.ttc", "简体繁體", 2);

result : http://wmh.github.io/hunbook/_static/examples/gd-imagettftext/003.jpg

go to : http://wmh.github.io/hunbook/examples/gd-imagettftext.html

Gainer answered 10/10, 2017 at 8:31 Comment(0)
R
0

For a best visualization, use -$textColor; this will show a better view of the text.

Rohrer answered 23/1, 2013 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.