How to give border to text in PHP GD library
Asked Answered
E

4

9

How can I give border to text using PHP GD library with multicolored text, where the text color is different from the border color.

As you can refer:

enter image description here

Erepsin answered 13/11, 2014 at 9:23 Comment(1)
Please show us what you've tried.Puto
E
4
// http://www.johnciacia.com/2010/01/04/using-php-and-gd-to-add-border-to-text/
 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);
Erepsin answered 13/11, 2014 at 10:59 Comment(0)
D
15

Use following function to add border to text

You can check the example output here http://wmh.github.io/hunbook/examples/gd-imagettftext.html

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);
}
Dearr answered 13/11, 2014 at 9:28 Comment(1)
I am working on GD library and little bit confused on a point of quality need your helpErepsin
C
10

You can use stil/gd-text class library. Code example:

<?php
require __DIR__.'/../vendor/autoload.php';

use GDText\Box;
use GDText\Color;

$im = imagecreatetruecolor(500, 500);
$backgroundColor = imagecolorallocate($im, 0, 18, 64);
imagefill($im, 0, 0, $backgroundColor);

$box = new Box($im);
$box->setFontFace(__DIR__.'/Elevant bold.ttf'); // http://www.dafont.com/elevant-by-pelash.font
$box->setFontSize(150);
$box->setFontColor(new Color(255, 255, 255));
$box->setBox(20, 20, 460, 460);
$box->setTextAlign('center', 'center');

$box->setStrokeColor(new Color(255, 75, 140)); // Set stroke color
$box->setStrokeSize(3); // Stroke size in pixels

$box->draw("Elevant"); // Text to draw

header("Content-type: image/png;");
imagepng($im, null, 9, PNG_ALL_FILTERS);

Demonstration:

demo

Cidevant answered 30/10, 2015 at 12:33 Comment(0)
E
4
// http://www.johnciacia.com/2010/01/04/using-php-and-gd-to-add-border-to-text/
 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);
Erepsin answered 13/11, 2014 at 10:59 Comment(0)
L
3

Improved function imagettfstroketext: rounded corners + more fast (especially on wide borders)

function imagettfstroketext(&$image, $size, $angle, $x, $y, &$textcolor, &$strokecolor, $fontfile, $text, $px) {
    for($c1 = ($x-$px); $c1 <= ($x+$px); $c1++) {
        $c2 = $y + round(sqrt($px*$px - ($x-$c1)*($x-$c1)));
        imagettftext($image, $size, $angle, $c1, $c2, $strokecolor, $fontfile, $text);
        $c3 = $y - round(sqrt($px*$px - ($x-$c1)*($x-$c1)));
        imagettftext($image, $size, $angle, $c1, $c3, $strokecolor, $fontfile, $text);
    }
   return imagettftext($image, $size, $angle, $x, $y, $textcolor, $fontfile, $text);
}
Lien answered 22/9, 2020 at 21:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.