imagettftext

rernanded

Erfahrenes Mitglied
Hallo

ist es möglich bei Nutzung von imagettftext den Buchstabenabstand und den Zeilenabstand anzugeben?

PHP:
$text = "wow 1234567890 wow 1234567890 wow 1234567890";
$font = "ttfs/lato/Lato-Black.ttf"; 
// imagettftext(IMAGE, FONT SIZE, ANGLE, X, Y, COLOR, FONT, TEXT)
imagettftext($img, 30, 10, 100, 130, $white, $font, wordwrap($text, 13, "\n", true));

Moni
 
@basti Guter Tipp, hatte ich wohl übersehen. Einige andere Posts hatte ich probiert. Klappte nicht.
Vielversprechend ist diese Sache aus der Seite die Du gelinkt hattest.
Nur wie sieht der Call aus und müsste es in der 2. function beim foreach nicht anstatt ($text) besser ($testStr) lauten?

MONI

PHP:
<?php

function getBBoxW($bBox)
{
  return $bBox[2] - $bBox[0];
}

function imagettftextSpacing($image, $size, $x, $y, $color, $font, $text, $spacing = 0)
{
    $testStr = 'test';
    $testW   = getBBoxW(imagettfbbox($size, 0, $font, $testStr));
    foreach (mb_str_split($text) as $char)
    {
        $fullBox = imagettfbbox($size, 0, $font, $char . $testStr);
        imagettftext($image, $size, 0, $x - $fullBox[0], $y, $color, $font, $char);
        $x += $spacing + getBBoxW($fullBox) - $testW;
    }
}

$image = imagecreate(900, 300);

$black = imagecolorallocate($img, 0, 0, 0);
$blue = imagecolorallocate($img, 0, 0, 255);

$font = "ttfs/lato/Lato-Black.ttf";

imagefilledrectangle($image, 0, 0, 900, 300, $blue);

Call???

header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
 
Da kann ich dir nicht wirklich helfen.
Habe mal gerade vesucht dein Code zu testen , doch ich kriege da nur einen großen blauen Kasten raus;
Das du deine Funktion aufrufen mußt ist ja klar.
Ich habe die Zeile gelöscht
Code:
$testStr = 'test';
und beim aufrufen.
Psydo Code
Code:
imagettftextSpacing($image, 50 ,19, 39, $black, $font, 'Dein Text');
wieder rein geschrieben.
Oben bei function muß das $text auch angepasst werden in $textStr.

Ich kann da nicht weiter helfen.
Ich bekomme es nicht hin und weiß nicht mal wie es aussehen soll.
Man sollte sich auch fragen ob das Bild malen mit Php sinnvoll ist und richtige Bilder nicht besser sind.
 
Bei mir funktioniert es mit diesem PHP:
Code:
<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);

function getBBoxW($bBox)
{
    return $bBox[2] - $bBox[0];
}

function imagettftextSpacing($image, $size, $x, $y, $color, $font, $text, $spacing = 0)
{
    $testW = getBBoxW(imagettfbbox($size, 0, $font, $text));
    // foreach (mb_str_split($text) as $char) {
    foreach (str_split($text) as $char) {
        $fullBox = imagettfbbox($size, 0, $font, $char . $text);
        imagettftext($image, $size, 0, $x - $fullBox[0], $y, $color, $font, $char);
        $x += $spacing + getBBoxW($fullBox) - $testW;
    }
}

// $image = imagecreatetruecolor(900, 300);
$image = imagecreate(900, 300);

$black = imagecolorallocate($image, 0, 0, 0);
$blue = imagecolorallocate($image, 0, 0, 255);
$white = imagecolorallocate($image, 255, 255, 255);

$font = __DIR__ . "/fonts/Skia.ttf";

imagefilledrectangle($image, 0, 0, 900, 300, $blue);

imagettftextSpacing(
    $image, // das mit imagecreate erzeugte Bild
    20, // die Groesse der Schrift
    100, // x-Position des Textes
    50, // y-Position des Textes
    $white, // Farbe des Textes
    $font, // Fontfile
    'Mein Testtext', // der Text, der gezeichnet werden soll
    10 // der Zwischenraum
);

header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
Fallstricke:
 
Zuletzt bearbeitet:

Neue Beiträge

Zurück