VII. Hình ảnh
VII.1. Yêu cầu phần mềm
VII.2. Tạo và vẽ ảnh
VII.3. Vẽ Text
VII.4. Co dãn ảnh
VII.1. Yêu cầu phần mềm
Với PHP bạn có thể sử dụng các hàm graphic để v
ra các hình ảnh và lưu trữ dưới dạng file (GIF, PNG,
JPG)
Để thực hiện được chức năng này, bạn cần phải cài
đặt bộ thư viện GD (http://www.boutell.com/gd/ )
Để biết thư viện GD đã được cài đặt hay chưa, có
thể sử dụng hàm phpinfo() hoặc hàm gd_info()
Đối với bộ PHP full (v5.0) có đầy đủ thư viện PEAR
đi kèm, để chạy GD bạn chỉ việc thiết lập trong file
php.ini: extension=php_gd2.dll
VII.2. Tạo và vảnh
<!-- file: caro.php -->
<?php
$im = imagecreate(30, 30);
$red = imagecolorallocate($im, 0xff, 0x00, 0x00);
$blue = imagecolorallocate($im, 0x00,0x00,0xff);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);
imagefilledrectangle($im, 0, 0, 29, 29, $black);
imagefilledrectangle($im, 0,0,15,15,$red);
imagefilledrectangle($im, 15,15,29,29,$blue);
imagejpeg($im);
?>
<!-- file: 1.html -->
<html>
Day la caro <img src="caro.php"/>
</html>
<?php
$im = imagecreate(30, 30);
$red = imagecolorallocate($im, 0xff, 0x00, 0x00);
$blue = imagecolorallocate($im, 0x00,0x00,0xff);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);
imagefilledrectangle($im, 0, 0, 29, 29, $black);
imagefilledrectangle($im, 0,0,15,15,$red);
imagefilledrectangle($im, 15,15,29,29,$blue);
imagejpeg($im, "c:/test.jpg", 100);
?>
<html>
Day cung la caro: <img src = "c:/test.jpg"/>
</html>
VII.2. Tạo và vảnh (2)
Các hàm thường sử dụng
resource imagecreate ( int x_size, int y_size ): Tạo ảnh với
kích thước x_size * y_size, ảnh ko phải truecolor
imagecolorallocate ( resource image, int red, int green, int
blue ): Sử dụng để định nghĩa 1 màu sẽ xuất hiện trong ảnh.
Đối với ảnh sử dụng palette, lần gọi đầu tiên sẽ cho màu
màu nền của ảnh
resource imagecreatetruecolor ( int x_size, int y_size ):
Tạo ảnh truecolor
bool imagejpeg ( resource image [, string filename [, int
quality]] )
bool imagepng ( resource image [, string filename] )
bool imagegif ( resource image [, string filename] )
VII.2. Tạo và vảnh (3)
ImageSetPixel(image, x, y, color);
ImageLine(image, start_x, start_ y, end_x, end_ y, color);
ImageDashedLine(image, start_x, start_ y, end_x, end_ y,
color);
ImageRectangle(image, tlx, tly, brx, bry, color);
ImageFilledRectangle(image, tlx, tly, brx, bry, color);
ImagePolygon(image, points, number, color);
ImageFilledPolygon(image, points, number, color);
ImageArc(image, center_x, center_ y, width, height, start, end,
color);
ImageFill(image, x, y, color);
ImageFillToBorder(image, x, y, border_color, color);