May 17, 2011

การ Ramdom Password แล้วนำมาสร้างเป็นรูปภาพการ Ramdom Password แล้วนำมาสร้างเป็นรูปภาพ

หลายครั้งเราจำเป็นต้องทำการ นำข้อมูลเล่านั้นมาสร้างเป็นรูปภาพ อาจจะง่ายต่อการแสดงผลและจัดเก็บ
เช่นการแสดง Password หรือแม้กระทั่ง Counter ต่าง



ลองมาดูตัวอย่างการสร้างรูปภาพด้วยการ Ramdom Password ครับ



<?php

// create a 100*30 image
$im = imagecreate(255, 50);


// white background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 233, 14, 91);




function random_password($len)
{
srand((double)microtime()*10000000);
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
$ret_str = "";
$num = strlen($chars);
for($i = 0; $i < $len; $i++)
{
$ret_str.= $chars[rand()%$num];
$ret_str.="";
}
return $ret_str;
}


// write the string at the top left
imagestring($im, 5, 0, 0, "ThaiCreate.Com Random Password", $textcolor);
imagestring($im, 5, 83,20, random_password(7), $textcolor);




// output the image
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>


ผลลัพธ์ที่ได้



No comments:

Post a Comment