Ver Mensaje Individual
  #13 (permalink)  
Antiguo 26/06/2011, 09:24
konvulsion
 
Fecha de Ingreso: abril-2011
Mensajes: 39
Antigüedad: 13 años
Puntos: 1
Respuesta: Código para meter texto en imagen

Código PHP:
Ver original
  1. <?php
  2. // texto a convertir
  3. if( isset($_POST['string']).($_POST['string2']) && !empty($_POST['string']).($_POST['string2'])){
  4. $string = trim($_POST['string']);
  5. $string2 = trim($_POST['string2']);
  6.  
  7. // tamaño de la imagen
  8. $im = imagecreate(500, 300);
  9.  
  10. // fondo blanco y texto azul
  11. $bg = imagecolorallocate($im, 0, 0, 0);
  12. $textcolor = imagecolorallocate($im, 255, 0, 0);
  13. $textcolor2 = imagecolorallocate($im, 255, 255, 255);
  14.  
  15. // escribimos el texto en la parte superior-izquierda
  16. $xCentrado = (imagesx($im) - $cajaTexto[3]) / 2;
  17. $yCentrado = (imagesy($im) - $cajaTexto[2]) / 1.2;
  18. $xCentrado2 = (imagesx($im) - $cajaTexto2[3]) / 2;
  19. $yCentrado2 = (imagesy($im) - $cajaTexto2[2]) / 1.1;
  20. imagettftext($im,25,0,$xCentrado,$yCentrado,$textcolor,"times.ttf",$string);
  21. imagettftext($im,10,0,$xCentrado2,$yCentrado2,$textcolor2,"tahoma.ttf",$string2);
  22.  
  23. // mostrar la imagen
  24. header("Content-type: image/png");
  25. imagepng($im);
  26.  
  27.  
  28. }else{ ?>
  29. <form name="form1" method="post" action="">
  30. <input type="text" style="text-align:center" name="string" value="Título" onfocus="if (this.value=='Título') this.value=''" onblur="if (this.value=='') this.value='Título'" size="20" onKeyUp="limita(this,100);" onKeyDown="limita(this,100);">
  31. <p>
  32. <input type="text" name="string2" style="text-align:center" value="Subtítulo" onfocus="if (this.value=='Subtítulo') this.value=''" onblur="if (this.value=='') this.value='Subtítulo'" size="20" onKeyUp="limita(this,400);" onKeyDown="limita(this,400);"></textarea>
  33. <p>
  34. <input type="submit" name="Submit" value="Enviar">
  35. </form>
  36. <?php } ?>

Esto es lo que hago para que el texto me quede centrado... Lo malo es que no me acaba de funcionar con el GET (que al final le he puesto POST porque terminaré pasándolo por post). Sí que me funciona si lo hago de esta manera, pero sin enviar por URL:

Código PHP:
Ver original
  1. <?php
  2. header("Content-type: image/png");
  3. $im = imagecreatetruecolor(500, 300);
  4. $color = imagecolorallocate($im, 255, 0, 0);
  5. $color2 = imagecolorallocate($im, 255, 255, 255);
  6. $fuente = "times.ttf";
  7. $fuente2 = "tahoma.ttf";
  8. $cajaTexto = imagettfbbox(25, 0, $fuente, "Título");
  9. $cajaTexto2 = imagettfbbox(10, 0, $fuente2, "Subtítulo");
  10. $xCentrado = (imagesx($im) - $cajaTexto[2]) / 2;
  11. $yCentrado = (imagesy($im) - $cajaTexto[2]) / 1;
  12. $xCentrado2 = (imagesx($im) - $cajaTexto2[2]) / 2;
  13. $yCentrado2 = (imagesy($im) - $cajaTexto2[2]) / 1;
  14. imagettftext($im, 25, 0, $xCentrado, $yCentrado, $color, $fuente, "Título");
  15. imagettftext($im, 10, 0, $xCentrado2, $yCentrado2, $color2, $fuente2, "Subtítulo");
  16. imagepng($im);
  17. ?>

He intentado adaptarlo lo mejor que he podido, pero no hay manera. ¿Acaso ahora tengo que aplicar la función de diferente manera?