Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Image (GD) > PHP Create text in image



Clound SSD Virtual Server

PHP Create text in image

PHP Create text image ทดสอบการสร้างรูปภาพและเขียนข้อความลงในรูปภาพ และการกำหนดรูปแบบของ Font(Create text in images)

PHP & GD



Sample 1 เป็นการเขียนข้อความลงในรูปภาพ

<?php
echo "<img src=MyResize/image.png>";
$images = ImageCreate(300,200);
$color = ImageColorAllocate($images,255,0,0);
$photo = ImageColorAllocate($images,0,0,0);
ImageRectangle($images, 0, 0, 299, 199, $photo);
ImageString($images, 5, 10, 20, "www.ThaiCreate.Com Version 2009", $photo);
ImageString($images, 5, 10, 40, "Community By @W_IN", $photo);
ImageStringUp($images, 5, 10, 190, "My Name is Win", $photo);
ImagePng($images,"MyResize/image.png");
ImageDestroy($images);
?>


PHP GD


Sample 2 เป็นการเรียกใช้งาน Font จากภายนอกครับ โดย Font จะต้องอยู่ใน Path ด้วย

<?php
echo "<img src=MyResize/image.png>";
$images = ImageCreate(300,200);
$color = ImageColorAllocate($images,255,0,0);
$photo = ImageColorAllocate($images,0,0,0);
ImageRectangle($images, 0, 0, 299, 199, $photo);

$text = 'My Name Is Win';
$font = 'ANGSAZ.TTF';

$white = ImageColorAllocate($images, 255, 255, 255);
$blue = ImageColorAllocate($images, 0, 0, 255);
ImagettfText($images, 20, 0, 11, 21, $blue, $font, $text);

ImagePng($images,"MyResize/image.png");
ImageDestroy($images);
?>



PHP GD








Sample 3 ตัวอย่างการจัดข้อความให้อยู่ตรงกลางของรูปภาพ

<?php
  
     $text = "ThaiCreate.Com";
     $font_size = 15;
     $height = 200;
     $width = 400;
   
     $im = ImageCreate($width, $height);
     $grey = ImageColorAllocate($im, 230, 230, 230);
     $black = ImageColorAllocate($im, 0, 0, 0);
   
     $text_bbox = ImageTTFBBox($font_size, 0, "ANGSAZ.TTF", $text);
     $image_centerx = $width / 2;
	 $image_centery = $height / 2;
     $text_x = $image_centerx - round(($text_bbox[4]/2));
	 $text_y = $image_centery;
   
     ImageTTFText($im, $font_size, 0, $text_x, $text_y, $black, "ANGSAZ.TTF", $text);
     ImagePng($im,"MyResize/image.png");
     ImageDestroy ($im);

	 echo "<img src=MyResize/image.png>";
?>


PHP GD


Sample 4 ตัวอย่างการจัดข้อความให้อยู่ตรงกลางและกำหนดให้ขนาดของ Font จะต้องไม่เกินขอบขนาดของรูปภาพ

<?php
$height = 200;
$width = 200;
$fontsize = 25;
if (!isset($String))
$String = "Welcome To ThaiCreate.Com";
$im = imagecreate($width, $height );
$blue = imagecolorallocate($im,0,0,255);
$green = imagecolorallocate($im,0,255,0);
$font = "ANGSAZ.TTF";
$textwidth = $width;
while (1){
    $box = imageTTFbbox( $fontsize, 0, $font, $String );
    $textwidth =  abs( $box[2] );
    $textbodyheight = (abs($box[7]))-2;
    if ( $textwidth < $width - 20 )
        break;
    $fontsize--;
}
$Xcenter = (int)($width/2 );
$Ycenter = (int)($height/2 );
imageTTFtext($im, $fontsize, 0,(int) ($Xcenter-($textwidth/2)),(int)($Ycenter+(($textbodyheight)/2) ),
             $green, $font, $String );
imagegif($im,"MyResize/image.png");
echo "<img src=MyResize/image.png>";
?>


PHP GD


Sample 5 ตัวอย่างการจัดข้อความให้อยู่ตำแหน่งริมซ้ายด้านล่าง

<?php
  
     $text = "ThaiCreate.Com";
     $font_size = 15;
     $height = 200;
     $width = 400;
   
     $im = ImageCreate($width, $height);
     $grey = ImageColorAllocate($im, 230, 230, 230);
     $black = ImageColorAllocate($im, 0, 0, 0);
   
     $text_bbox = ImageTTFBBox($font_size, 0, "ANGSAZ.TTF", $text);
     $image_centerx = $width / 2;
	 $image_centery = $height / 2;
     $text_x = $width - ($width - 20);
	 $text_y = $height - 20;
   
     ImageTTFText($im, $font_size, 0, $text_x, $text_y, $black, "ANGSAZ.TTF", $text);
     ImagePng($im,"MyResize/image.png");
     ImageDestroy($im);

	 echo "<img src=MyResize/image.png>";
?>


PHP GD


Link แนะนำ ที่ควรศึกษา




Reference : https://www.thaicreate.com/php-manual/ref.image.html


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2008-10-23 23:24:00 / 2017-03-15 10:46:00
  Download : Download  PHP Create text in image
 Sponsored Links / Related

 
PHP GD
Rating :

 
PHP Create Image
Rating :

 
PHP Write text in Image
Rating :

 
PHP Upload and write text in image
Rating :

 
PHP Add Logo/CopyRight to Image (Watermark)
Rating :

 
PHP Upload and Add Logo to image
Rating :

 
PHP Resize image
Rating :

 
PHP Upload and Resize Image
Rating :

 
PHP Resize Thumbnails Image and Insert to MySQL
Rating :

 
PHP Multiple Upload & Multiple Resize to MySQL
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่