2011-08-03 27 views
6

¿Alguien tiene una función que dibuja una cadena ttf (imagettftext) con espaciado de letras especificado?php imagettftext espaciado entre letras

No puedo encontrar ninguna función GD incorporada, así que creo que debe hacerse letra por letra añadiendo un ancho constante.

Quizás alguien ya tenga esa función :)

ps. la mejor fuente será arial.ttf

Respuesta

20
function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0) 
{   
    if ($spacing == 0) 
    { 
     imagettftext($image, $size, $angle, $x, $y, $color, $font, $text); 
    } 
    else 
    { 
     $temp_x = $x; 
     for ($i = 0; $i < strlen($text); $i++) 
     { 
      $bbox = imagettftext($image, $size, $angle, $temp_x, $y, $color, $font, $text[$i]); 
      $temp_x += $spacing + ($bbox[2] - $bbox[0]); 
     } 
    } 
} 

y la llamada:

imagettftextSp($image, 30, 0, 30, 30, $black, 'arial.ttf', $text, 23); 

Función el orden de los parámetros cumple con el orden estándar de los parámetros de imagenttftext, y el último parámetro es el parámetro $ spacing opcional. Si no se establece o el valor pasado es 0, el espaciado entre caracteres/letras no se establece.

+0

reemplace $ text [$ i] con mb_substr ($ text, $ i, 1) para solucionar los problemas con los caracteres mulitibyte – Juergen

0

GD no admite interletraje, por lo que tendrá que hacerlo manualmente. Personalmente, escribí una función que escribiría cada letra por separado. Yo no lo encuentro en este momento, pero es algo a lo largo de las líneas de:

function drawText(&$image, $text, $fgColor, $font, $fgColor, 
        $fontSize = 14, $kerning = 0, $x = 0, $y = 0) { 
    $letters = explode('', $text); 

    foreach ($letters as $n => $letter) { 
     $bbox = imagettftext($image, $fontSize, 0, $x, $y, $fgColor, $font, $letter); 
     $x += $bbox[2] + $kerning; 
    } 
} 
-1

Pruebe esta función:

$image = imagecreatetruecolor(500,200); 
$text = "Text to print"; 
$text_color=imagecolorallocate($image,255,255,255); 
$font_size = 18; 
$space = 8; 
$font = "path_to_font/arial.ttf"; 
$x=20; 
$y=20; 
for ($i = 0; $i <strlen($text); $i++){ 
    $arr = imagettftext ($image, $font_size,0, $x, $y, $text_color, $font, $text{$i}); 
    $x = $arr[4]+$space; 
} 
imagejpeg($image); 
destroyimage($image); 
10

Sé que esto fue respondido hace un tiempo, pero necesitaba una solución que tuviera espacio entre letras y mantuviera las desviaciones angulares.

he modificado el código del Radzi de lograr esto:

function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0) 
{   
    if ($spacing == 0) 
    { 
     imagettftext($image, $size, $angle, $x, $y, $color, $font, $text); 
    } 
    else 
    { 
     $temp_x = $x; 
     $temp_y = $y; 
     for ($i = 0; $i < strlen($text); $i++) 
     { 
      imagettftext($image, $size, $angle, $temp_x, $temp_y, $color, $font, $text[$i]); 
      $bbox = imagettfbbox($size, 0, $font, $text[$i]); 
      $temp_x += cos(deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0])); 
      $temp_y -= sin(deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0])); 
     } 
    } 
} 
+1

Me alegra ver que devolvió a la comunidad de Stack Overflow con esta respuesta. Buen espectáculo +1 – Fluffeh

5

sólo para completar la respuesta de pidalia (que es el mejor) para evitar algunos problemas con carbón especial (como "E" o "A")

static function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0) { 
    if ($spacing == 0) { 
     imagettftext($image, $size, $angle, $x, $y, $color, $font, $text); 
    } else { 
     $temp_x = $x; 
     $temp_y = $y; 
     //to avoid special char problems 
     $char_array = preg_split('//u',$text, -1, PREG_SPLIT_NO_EMPTY); 
     foreach($char_array as $char) { 
      imagettftext($image, $size, $angle, $temp_x, $temp_y, $color, $font, $char); 
      $bbox = imagettfbbox($size, 0, $font, $char); 
      $temp_x += cos(deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0])); 
      $temp_y -= sin(deg2rad($angle)) * ($spacing + ($bbox[2] - $bbox[0])); 
     } 
    } 
} 
Cuestiones relacionadas