web 2.0

How to use PHP & Create Text in Image

How to use PHP & Create Text in image The guideline/example scripts how to using PHP and GD library create text in image.

ShotDev Focus:
- PHP & GD Library (Create Text in Image)

Example 1 (Create Text in Image)

  1. <?  
  2. echo "<img src=MyResize/image.png>";  
  3. $images = ImageCreate(300,200);  
  4. $color = ImageColorAllocate($images,255,0,0);  
  5. $photo = ImageColorAllocate($images,0,0,0);  
  6. ImageRectangle($images, 0, 0, 299, 199, $photo);  
  7. ImageString($images, 5, 10, 20, "www.ShotDev.Com Version 2010"$photo);  
  8. ImageString($images, 5, 10, 40, "Community By @W_IN"$photo);  
  9. ImageStringUp($images, 5, 10, 190, "My Name is Win"$photo);  
  10. ImagePng($images,"MyResize/image.png");  
  11. ImageDestroy($images);  
  12. ?>  

PHP & GD (Create Text in Image)

Example 2 (Using Font)

  1. <?  
  2. echo "<img src=MyResize/image.png>";  
  3. $images = ImageCreate(300,200);  
  4. $color = ImageColorAllocate($images,255,0,0);  
  5. $photo = ImageColorAllocate($images,0,0,0);  
  6. ImageRectangle($images, 0, 0, 299, 199, $photo);  
  7.   
  8. $text = 'My Name Is Win';  
  9. $font = 'ANGSAZ.TTF';  
  10.   
  11. $white = ImageColorAllocate($images, 255, 255, 255);  
  12. $blue = ImageColorAllocate($images, 0, 0, 255);  
  13. ImagettfText($images, 20, 0, 11, 21, $blue$font$text);  
  14.   
  15. ImagePng($images,"MyResize/image.png");  
  16. ImageDestroy($images);  
  17. ?>  

PHP & GD (Create Text in Image)

Example 3 (Font and Text Center)

  1. <?php  
  2.   
  3. $text = "ShotDev.Com";  
  4. $font_size = 15;  
  5. $height = 200;  
  6. $width = 400;  
  7.   
  8. $im = ImageCreate($width$height);  
  9. $grey = ImageColorAllocate($im, 230, 230, 230);  
  10. $black = ImageColorAllocate($im, 0, 0, 0);  
  11.   
  12. $text_bbox = ImageTTFBBox($font_size, 0, "ANGSAZ.TTF"$text);  
  13. $image_centerx = $width / 2;  
  14. $image_centery = $height / 2;  
  15. $text_x = $image_centerx - round(($text_bbox[4]/2));  
  16. $text_y = $image_centery;  
  17.   
  18. ImageTTFText($im$font_size, 0, $text_x$text_y$black"ANGSAZ.TTF"$text);  
  19. ImagePng($im,"MyResize/image.png");  
  20. ImageDestroy ($im);  
  21.   
  22. echo "<img src=MyResize/image.png>";  
  23. ?>  

PHP & GD (Create Text in Image)

Example 4 (Check size image and write text)

  1. <?php  
  2. $height = 200;  
  3. $width = 200;  
  4. $fontsize = 25;  
  5. if (!isset($String))  
  6. $String = "Welcome To ShotDev.Com";  
  7. $im = imagecreate($width$height );  
  8. $blue = imagecolorallocate($im,0,0,255);  
  9. $green = imagecolorallocate($im,0,255,0);  
  10. $font = "ANGSAZ.TTF";  
  11. $textwidth = $width;  
  12. while (1){  
  13. $box = imageTTFbbox( $fontsize, 0, $font$String );  
  14. $textwidth =  abs$box[2] );  
  15. $textbodyheight = (abs($box[7]))-2;  
  16. if ( $textwidth < $width - 20 )  
  17. break;  
  18. $fontsize--;  
  19. }  
  20. $Xcenter = (int)($width/2 );  
  21. $Ycenter = (int)($height/2 );  
  22. imageTTFtext($im$fontsize, 0,(int) ($Xcenter-($textwidth/2)),(int)($Ycenter+(($textbodyheight)/2) ),  
  23. $green$font$String );  
  24. imagegif($im,"MyResize/image.png");  
  25. echo "<img src=MyResize/image.png>";  
  26. ?>  

PHP & GD (Create Text in Image)

Example 5

  1. <?php  
  2.   
  3. $text = "ShotDev.Com";  
  4. $font_size = 15;  
  5. $height = 200;  
  6. $width = 400;  
  7.   
  8. $im = ImageCreate($width$height);  
  9. $grey = ImageColorAllocate($im, 230, 230, 230);  
  10. $black = ImageColorAllocate($im, 0, 0, 0);  
  11.   
  12. $text_bbox = ImageTTFBBox($font_size, 0, "ANGSAZ.TTF"$text);  
  13. $image_centerx = $width / 2;  
  14. $image_centery = $height / 2;  
  15. $text_x = $width - ($width - 20);  
  16. $text_y = $height - 20;  
  17.   
  18. ImageTTFText($im$font_size, 0, $text_x$text_y$black"ANGSAZ.TTF"$text);  
  19. ImagePng($im,"MyResize/image.png");  
  20. ImageDestroy($im);  
  21.   
  22. echo "<img src=MyResize/image.png>";  
  23. ?>  

PHP & GD (Create Text in Image)

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.