Tải bản đầy đủ (.pdf) (14 trang)

Bài giảng PHP (Hypertext Preprocessing) - Chương 7: Hình ảnh

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.11 MB, 14 trang )

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 ( )
Để 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 -->
$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>
$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 là
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);


VII.3. Vẽ Text



$im = imagecreate(100,100);
$white = imagecolorallocate($im, 0xff, 0xff, 0xff);
$red = imagecolorallocate($im, 0xff, 0x00, 0x00);
imagestring($im, 1, 0, 0, "HUCE", $red);
imagestring($im, 2, 0, 10, "HUCE", $red);
imagestring($im, 3, 0, 20, "HUCE", $red);
imagestring($im, 4, 0, 30, "HUCE", $red);
imagestring($im, 5, 0, 45, "HUCE", $red);
imagestringup($im, 5, 50, 99, "University", $red);
imagegif($im);
?>


VII.3. Vẽ Text (2)
$im = imagecreate(100,100);
$white = imagecolorallocate($im, 0xff, 0xff, 0xff);
$red = imagecolorallocate($im, 0xff, 0x00, 0x00);
$blue = imagecolorallocate($im, 0x00, 0x00, 0xff);
imagettftext($im, 8, 30, 10, 100, $red,
"Times", "HUCE - 46pm1");
imagettftext($im, 10, -30, 10, 10,$blue,
"Arialni","HUCE- 46pm1");
imagegif($im);
?>


VII.4. Co dãn ảnh
$im = imagecreate(100,100);

$white = imagecolorallocate($im, 0xff, 0xff, 0xff);
$red = imagecolorallocate($im, 0xff, 0x00, 0x00);
$blue = imagecolorallocate($im, 0x00, 0x00, 0xff);
imagettftext($im, 8, 30, 10, 100, $red, "Times", "HUCE - 46pm1");
imagettftext($im, 10, -30, 10, 10, $blue, "Arialni", "HUCE - 46pm1");
$im2 = imagecreate(200, 200);
//imagecopyresized($im2, $im, 0, 0, 0, 0, 200, 200, 100, 100);
imagecopyresampled($im2, $im, 0, 0, 0, 0, 200, 200, 100, 100);
imagegif($im2);
?>


VII.5. Màu sắc
VII.5.1. True color & color index
VII.5.2. Kênh alpha


VII.5.1. True color & color index





Ảnh của bạn được tạo bởi các hàm imagecreate() – tạo ảnh sử
dụng colorindex trên palette và imagecreatetruecolor () – tạo
ảnh truecolor (24bit/pixel)
Từ GD2.0 trở lên, ta có thể làm việc với kênh alpha (xem phần
sau)
Để lấy màu của điểm ảnh







$color = ImageColorAt(image, x, y);
$values = ImageColorsForIndex(image, index);

Với ảnh truecolor, cấu trúc màu gồm: 7bit alpha, 8bit Red, 8bit
Green, 8bit Blue (từ trái qua phải). Có thể sử dụng công thức
sau


$a
$r
$g
$b

=
=
=
=

($col
($col
($col
($col

&
&

&
&

0x7F000000) >> 24;
0x00FF0000) >> 16;
0x0000FF00) >> 8;
0x000000FF);


VII.5.2. Kênh alpha



Kênh alpha trong GD chỉ gồm 7bit (giá trị từ 0 đến 127)
Để sử dụng kênh alpha sử dụng các hàm





ImageColorResolveAlpha()
ImageAlphaBlending()

$im = ImageCreateTrueColor(150,150);
$white = ImageColorAllocate($im,255,255,255);
ImageAlphaBlending($im, true);
ImageFilledRectangle($im,0,0,150,150,$white);
$red = ImageColorResolveAlpha($im,255,50,0,63);
ImageFilledEllipse($im,75,75,80,50,$red);

$gray = ImageColorResolveAlpha($im,70,70,70,63);
ImageAlphaBlending($im, true);
ImageFilledRectangle($im,60,60,120,120,$gray);
header('Content-Type: image/png');
ImagePNG($im);
?>


IX. XML
IX.1. Tạo văn bản XML
IX.2. Đọc XML


IX.1. Tạo văn bản XML


Cách đơn giản nhất để tạo văn bản XML là
đặt lại header của trang PHP




header('Content-Type: text/xml');

Sau đó đưa thông tin ra browser bằng các
hàm echo, print... như bình thường


IX.2. Đọc XML



function start_element($inParser, $inName, &$inAttributes)
{
var_dump($inName);
var_dump($inAttributes);
}
function end_element($inParser, $inName) {
var_dump($inName);
}
$xml = xml_parser_create();
xml_set_element_handler($xml, "start_element",
"end_element");
xml_parse($xml, "<xml>atr='attribute1'/></xml>");
?>



×