Php ile yazıyı resme çevirmek

Yazan: xyz | Tarih 26 Eylül 2007 | Yorum  5 Yorum
BerbatKötüOrtaGüzelHarika 1 kez puan verildi, Ortalama: 1 (5 üzerinden)
Loading ... Loading ...

Merhaba arkadaşlar , bu konuda sizlere ile bir text i nasıl resim haline çevirceğimizi anlatıcam ..

Uygulamanın adı Text to öncelikle 3 adet dosyamız var ;

1.text2image.
2.image_functions.
3.comic.ttf

Text2image. dosyası işlemin gerçekleşip ekrana çıkartığı dosyadır , image_functions. dosyası ise ismindende anlaşıldığı gibi fonksiyonların olduğu dosya ve comic.ttf dosyası ise font dosyamız yani text in hangi font la yazılcağını belirliyoruz .

Text2image.

:
  1. <?
  2. define( 'FONT_DIR', './' );
  3. include( './image_functions.' );    
  4.  
  5. $text = " Deneme 123 , iyi sahurlar perfectgate ailesi .";
  6. $req_height = 1;
  7. $width = 400;    
  8.  
  9. $text = preg_replace( '/n/', ' ', $text ) ;
  10. // grab font
  11. $font = FONT_DIR . '/comic.ttf';    
  12.  
  13. // black and white here    
  14.  
  15. $ = imagecreate( $width, $req_height );
  16. $bg_color = ImageColorAllocate( $,  255, 255, 255 );
  17. $color = ImageColorAllocate( $,  0, 0, 0 );    
  18.  
  19. $textSize = 10;
  20. $angle = 0;
  21. $top = 10;
  22. $left = 0;
  23. $right = $width;
  24. // ImageTTFText( $, $size, $angle, $top, $left, $text_color, $font , $description );    
  25.  
  26. $height = imageprintWordWrapped(&$, $top, $left, $right, $font, $color, $text, $textSize, $halign="left") ;    
  27.  
  28. if( $height> $req_height )
  29. {
  30.     ImageDestroy( $ );
  31.     $ = imagecreate( $width, ( $height + ( 50 ) ));
  32.     $bg_color = ImageColorAllocate( $,  255, 255, 255 );
  33.     $color = ImageColorAllocate( $,  0, 0, 0 );
  34.     imageprintWordWrapped(&$, $top, $left, $right, $font, $color, $text, $textSize, $halign="left") ;    
  35.  
  36. }    
  37.  
  38. header( 'Content-type: /png' );
  39. ImagePNG( $ );    
  40.  
  41. ?>

Image_functions.

:
  1. <?    
  2.  
  3. //
  4. // ripped this from  http://us2..net/manual/en/function.imagettfbbox.
  5. //    
  6.  
  7. function imageprintWordWrapped(&$, $top, $left, $right, $font, $color, $text, $textSize, $halign="left")
  8. {
  9.     $maxWidth = $right - $left ;    //the trivial change
  10.     $words = explode(' ', strip_tags($text)); // split the text into an array of single words
  11.     $line = '';
  12.     while (count($words)> 0) {
  13.             $dimensions = imagettfbbox($textSize, 0, $font, $line.' '.$words[0]);
  14.             $lineWidth = $dimensions[2] - $dimensions[0]; // get the length of this line, if the word is to be included
  15.             if ($lineWidth> $maxWidth) { // if this makes the text wider that anticipated
  16.                 $lines[] = $line; // add the line to the others
  17.                 $line = ''; // empty it (the word will be added outside the loop)
  18.             }
  19.             $line .= ' '.$words[0]; // add the word to the current sentence
  20.             $words = array_slice($words, 1); // remove the word from the array
  21.     }
  22.     if ($line != '') { $lines[] = $line; } // add the last line to the others, if it isn't empty    
  23.  
  24. // aded some paddding in th eling height
  25.     $lineHeight = 1.5 * ( $dimensions[1] - $dimensions[7] ); // the height of a single line    
  26.  
  27.     $height = count($lines) * $lineHeight; // the height of all the lines total
  28.     // do the actual printing
  29.     $i = 1;
  30.     //print_R($widths);
  31.     foreach ($lines as $line) {
  32.             if($halign=="center") {
  33.             //figure out width of line
  34.             $dimensions = imagettfbbox($textSize, 0, $font, $line);
  35.             $lineWidth = $dimensions[2] - $dimensions[0];
  36.             //figure out where the center is.
  37.             $center=floor($maxWidth/2 + $left);
  38.             $leftStart=$center-$lineWidth/2;
  39.         } else if ($halign=="right") {
  40.             //figure out width of line
  41.             $dimensions = imagettfbbox($textSize, 0, $font, $line);
  42.             $lineWidth = $dimensions[2] - $dimensions[0];
  43.             $leftStart=$left+$maxWidth-$lineWidth;
  44.         } else {
  45.             $leftStart=$left;
  46.         }
  47.        imagettftext($, $textSize, 0, $leftStart, $top + $lineHeight * $i, $color, $font, $line);
  48.        $i++;
  49.    }
  50.    return $height;
  51. }    
  52.  
  53. ?>

Resmi PNG formatında kaydediyor.

Üst üste mesaj olarak algılanmasın ama düzenle botunu belirli bir süreden sonra yok oluyor , Kodun gerçek kaynağı burasımı değil ama ona benzer bir uygulama var burda ve büyük ihtimalle bu kodda classes den çıkmıştır .

: http://www.phpclasses.org/browse/package/2349.html

Yazdır Yazdır | 809 Görüntülenme | Kategori: PHP | Trackback  Geri İzleme
Etiketler  Etiketler: , , ,

Benzer Yazılar


Yorum Yap


(gerekli)

(gerekli,yayınlanmaz)




XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>