Saturday, July 7, 2012

dynamic image with Php

ကြ်န္ေတာ္တို႔သံုးမယ္႔ image တစ္ခုဖန္တီးဖို႔အတြက္
php ရဲ႕function ၅ မ်ိဳးကိုသံုးပါမယ္

1. imagecreate()                http://www.php.net/manual/en/function.imagecreate.php
2. imagecolorallocate()        http://www.php.net/manual/en/function.imagecolorallocate.php
3. imagestring()               http://www.php.net/manual/en/function.imagestring.php
4. header()                       http://www.php.net/manual/en/function.header.php
5. imagepng()                    http://www.php.net/manual/en/function.imagepng.php

function ေတြေလ႔လာျပီးရင္ စလိုက္ရေအာင္
ပထမဆံုုး image ရဲ႕ width and height ကိုသတ္မွတ္ရေအာင္ဗ်ာ

$width=500;
$height=60;

ဟုတ္ပါတယ္ ကၽြန္ေတာ္တို႔ imagae size ရဲ႕ width and height ကိုသတ္မွတ္လိုက္ပါျပီ
image ကိုဖန္တီးဖို႔အတြက္ imagecreate() function ကိုအသံုးခ်လို္က္ရေအာင္

$image=imagecreate($width,$height);

imagecreate() က width and height ဆိုတဲ႔ parameter နွစ္ခုကိုလက္ခံထားပါတယ္
1 parameter ကေတာ႔ Image ရဲ႕ width ေနာ္
2 parameter ကေတာ႔ Image ရဲ႕ height ျဖစ္ပါတယ္

ဒါဆိုရင္ေနာက္ထပ္ image ရဲ႕ background color ကိုထပ္ျပီးသတ္္မွတ္ရေအာင္ဗ်ာ

$background = imagecolorallocate($image, 87, 87, 87);

imagecolorallocate() ကေတာ႔ parameter ၄ ခု လက္ခံထားပါတယ္
ပထမ parameter  ကေတာ႔ imagecreate() function ကဖန္တီးထားတဲ႔ image resource ပါ
2/3/4 parameter ကေတာ႔ RGB color ျဖစ္ပါတယ္

ေနာက္တစ္ဆင္႔ကေတာ႔ image ထဲမွာ ကၽြန္ေတာ္တို႔ေရးခ်င္တဲ႔ စာေလးေတြထည္ေပးရေအာင္ဗ်ာ

$text = "Welcome From cyberoot.blogspot.com";
$text_color = imagecolorallocate($image, 176, 176, 176);
imagestring($image, 10, '10', '10', $text, $text_color);

$text_color က imagecolorallocate() function နဲ႔ define လုပ္ထားပါတယ္
imagestring() function ကေတာ႔ parameter 6 ခုကိုလက္ခံထားပါတယ္
1 parameter ကေတာ႔ imagecreate() က ဖန္တီးထားတဲ႔ resource ပါ
2 parameter ကေတာ႔ font size ပါ
3 parameter ကေတာ႔ Left
4 parameter ကေတာ႔ Top
5 parameter ကေတာ႔ text ကိုထပ္ထည္႔တာပါ
6 parameter ကေတာ႔ text color ပါ

ကဲ႔ ေနာက္ဆံုးအဆင္တစ္ခုေတာ႔ေရာက္ျပီ

header ("Content-type: image/png");
imagepng($image);

header()   မွာေတာ႔ Content-Type က PNG ျဖစ္တယ္လို႔သတ္မွတ္ပါတယ္
imagepng() ကေတာ႔ output ထုတ္ေပးဖို႔ပါ
imagepng() ကေတာ႔ parameter ၄ ခုလက္ခံပါတယ္
1 parameter ကေတာ႔ imagecreate() က ဖန္တီးလိုက္တဲ႔ image source ပါ
2 parameter ကေတာ႔ မထည္႔လည္းရပါတယ္ image ရဲ႕ file name & path ကို အသံုးျပဳဖို႔အတြက္ထားပါတယ္
3 parameter ကလည္း မထည္႔လည္းရပါတယ္ image quality ကိုေဖာ္ျပတာပါ 0 to 9 ျဖစ္ပါတယ္
4 parameter  ကေတာ႔ filter ပါ. image size ကို reduce လုပ္ဖို႔ခြင္႔ျပဳတာပါ
ျပီးျပီဗ်ာ
source code အျပည္႔စံုကေတာ႔
<?php

$width = 450;
$height = 70;

$image = imagecreate($width, $height);
$background = imagecolorallocate($image, 87, 87, 87);

$text = "Welcome From cyberoot.blogspot.com";
$text_color = imagecolorallocate($image, 176, 176, 176);
imagestring($image, 10, '10', '10', $text, $text_color);

header ("Content-type: image/png");
imagepng($image);

?>
Thanks for reading

No comments:

Post a Comment

Thanks for your comments
Welcome from cyberoot