Ver Mensaje Individual
  #2 (permalink)  
Antiguo 12/02/2005, 04:24
Zerjillo
 
Fecha de Ingreso: febrero-2005
Mensajes: 396
Antigüedad: 19 años, 1 mes
Puntos: 1
Creo que el que pregunta lo está haciendo para usarlo con la funcion de pintar una cadena dentro de una imagen, no con HTML.

La idea es que no hay una función que lo haga directamente. Yo en su día tenia que hacer algo parecido y creé una función para hacer eso. De hecho te la paso más abajo. Como podrás ver la función no solo te permite hacer lo que tu quieres, sino que además permite que las letras tengan una "sombrita". Por otro lado, también permite mezclar dos tipos de letra distintos (originalmente era para poder meter negrita, pero si en vez de pasar como parametro "fontBoldName" la fuente en negrita pones otro tipo de letra funcionará bien. La función básicamente lo que hace es ir comprobando la longitud de la línea si añades una nueva palabra, y si te pasas del ancho que se especifica supone que tiene uqe hacer un salto de línea. Espero que te sea útil.

Un saludo

Zerjillo

Código PHP:
/*
Extended version of the previous funtion. It allows to print multiple line text horizontally centered around a certain coordinate. It can mix regular / bold style. The text in double brackets [[...]] will be printed in bold text.

Parameters:
  $image: The image where the string will be printed
  $string: The string to be printed
  $x: The text will be horizontally centered on this coordinate
  $y: Vertical coordinate for the first line of the text
  $maxWidth: Maximum with for a line (in pixels)
  $lineHeight: Line height (kinda space between lines)
  $fontName: Name of the "regular" font
  $fontBoldName: Name of the "bold" font
  $fontSize: The font size
  $color: The color of the printed message
  $shadowColor: The color for the shadow of the message

*/

function drawStringWithShadow2($image$string$x$y$maxWidth$lineHeight$fontName$fontBoldName$fontSize$color$shadowColor) {

  
$currentFont $fontName;

// Tokenizig message words
  
$words explode(" "$string);
  for (
$i $i count($words) ; $i++) {
    
$words[$i] = $words[$i] . " ";
  }

  
$currentFont $fontName;
  
$firstWordOfLine 0;
  
$lastWordOfLine 0;
  
$actualWidth 0;
  
$wordSize 0;

  
$initLineFont $fontName;

// While there are words to be printed
  
while ($lastWordOfLine count($words)) {

// Compute line width

    
while ( ($actualWidth $wordSize $maxWidth) && ($lastWordOfLine count($words)) ) {
      
$actualWidth += $wordSize;
      
$currentWord $words[$lastWordOfLine];

// If we must change the font into a bold one
      
if (strcmp(substr($currentWord02), "[[") == 0) {
        
$currentWord substr($currentWord2);
        
$currentFont $fontBoldName;
      }

// If bold font ends
      
$changeFontToOriginal false;
      if (
strcmp(substr($currentWordstrlen($currentWord) - 3), "]] ") == 0) {
        
$currentWord substr($currentWord0strlen($currentWord) - 3) . " ";
        
$changeFontToOriginal true;
      }

// Calculate word width
      
list($lx,$ly,$rx,$ry) = imagettfbbox($fontSize0$currentFont$currentWord);
      
$wordSize $rx $lx;
      
$lastWordOfLine++;

      if (
$changeFontToOriginal) {
        
$currentFont $fontName;
      }
    }

// Readjusting last word of line (if neccesary)
    
if ($lastWordOfLine != count($words)) {
      
$lastWordOfLine--;
    } else {
      
$actualWidth += $wordSize;
    }

// Begin printing line
    
$currentFont $initLineFont;
    
$xx $x $actualWidth 2;  // Initial horizontal coordinate

    
for ($i $firstWordOfLine $i $lastWordOfLine $i++) {
      
$currentWord $words[$i];

// If we must change the font into a bold one
      
if (strcmp(substr($currentWord02), "[[") == 0) {
        
$currentWord substr($currentWord2);
        
$currentFont $fontBoldName;
      }

// If bold font ends
      
$changeFontToOriginal false;
      if (
strcmp(substr($currentWordstrlen($currentWord) - 3), "]] ") == 0) {
        
$currentWord substr($currentWord0strlen($currentWord) - 3) . " ";
        
$changeFontToOriginal true;
      }

      list(
$lx,$ly,$rx,$ry) = imagettfbbox($fontSize0$currentFont$currentWord);
      
$wordSize $rx $lx;

// Print word
      
imagettftext($image$fontSize0$xx 3$y 3$shadowColor$currentFont$currentWord);
      
imagettftext($image$fontSize0$xx$y$color$currentFont$currentWord);
      
$xx += $wordSize;

      if (
$changeFontToOriginal) {
        
$currentFont $fontName;
      }
    }

    
$y += $lineHeight;  // Next line
    
$firstWordOfLine $lastWordOfLine;
    
$actualWidth 0;
    
$wordSize 0;
    
$initLineFont $currentFont;  // save current font
  
}