Tạo text image load font
Trang 1Tạo text image load font
Hướng dẫn và code mẫu làm một image chứa text load font
view plainprint?
1 <?php
2
3 $text = "Hello! PHP ";
4 $width = 200;
5 $height = 30;
6 $font = "Tahoma.ttf"; // Font file, phai load đúng tên, trên nền *nix phân biệt chữ hoa và chữ thường
7 $font_size = 12;
8 $top = 20;
9 $html_font_color = "#000000";
10 $html_bg_color = "#ffffff";
11 header("Content-type: image/jpeg");
12
13 $im = imagecreate($width,$height);
14
15 // Lay hex color cua Font
16 $red_font = hexdec(substr( $html_font_color, 1 , 2));
17 $green_font = hexdec(substr( $html_font_color, 3 , 2));
18 $blue_font = hexdec(substr( $html_font_color, 5 , 2));
19
20 // Lay hex color cua Background
21 $red_bg = hexdec(substr( $html_bg_color, 1 , 2));
22 $green_bg = hexdec(substr( $html_bg_color, 3 , 2));
23 $blue_bg = hexdec(substr( $html_bg_color, 5 , 2));
24
25 $bg_color = imagecolorallocate($im, $red_bg,$green_bg , $blue_bg);
26 $text_color = imagecolorallocate($im,$red_font,$green_font,$blue_font);
27
28 imagettftext($im, $font_size, 0, 0, $top, $text_color,$font,$text);
29 imagepng($im);
30 imagedestroy($im);
31 ?>