1、图片格式:
常见图片格式,包括:gif,jpg/jpeg,png等。
区别:
1)、gif 图片压缩率高,但是只能显示256色,可能造成颜色的丢失,可以显示动画。
2)、jpg/jpeg 图片压缩率高(有损压缩),可以用较小的文件来显示。
3)、png 该格式综合了gif和jpg的优势,缺点是不能显示动画。
2、PHP绘图实例
<?php
//绘图技术 基本步骤 前提:在php.ini文件中启用gd库
//创建画布 默认背景是黑色的
$img=imagecreatetruecolor(400,300);
//绘制各种图形
//创建一个颜色
$background = imagecolorallocate($img, 255, 0, 0);
//画圆
//imageellipse($img,30,30,50,50,$background);
//椭圆
//imageellipse($img,30,30,50,30,$background);
//画直线
//imageline($img,0,0,400,300,$background);
//画矩形
//imagerectangle ($img, 50 , 20 , 100 , 40 , $background);
//填充矩形
//imagefilledrectangle ($img, 50 , 20 , 100 , 40 , $background);
//画弧线
//imagearc($img, 100, 100, 150, 150, 180, 270, $background);
//画扇型 IMG_ARC_CHORD直线连接了起始和结束点 IMG_ARC_PIE
//imagefilledarc($img, 100, 100, 150, 150, 180, 270, $background,IMG_ARC_PIE);
//拷贝图片到画布
/*$scrImg=imagecreatefromgif('http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif');
$scrImgInfo=getimagesize('http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif');
imagecopy ($img,$scrImg,10,10,0,0,$scrImgInfo[0],$scrImgInfo[1]);
*/
//imagecopy ($img,$scrImg,10,10,0,0,270,129);
//写字
//imagestring ($img , 5 , 20 , 20 , "hello,world", $background );
//写中文
$str="PHP绘画技术";
imagettftext ($img , 30 , 0 , 50 ,50, $background , "MSYHBD.TTF" , $str);
//输出图像到网页(或者另存为)
header("content-type: image/png");
imagepng($img);
//销毁该图片(释放内存)
imagedestroy($img);
?>PHP 操作xml编程的例子,对词库的练习,学习对xml文件的增、删、改、查的操作。
1、wordView.php
<head>
<meta http-equiv="Content-type" content="text/html;charset=utf-8">
</head>
<body>
<form action="/blog_article/wordProcess.html" method="post">
<input type="text" name="enword">
<input type="hidden" name="type" value="query">
<input type="submit" value="查询">
</form>
<span>添加单词</span>
<form action="/blog_article/wordProcess.html" method="post">
英文:<input type="text" name="enword"><br>
中文:<input type="text" name="zhword"><br>
<!--<input type="hidden" name="type" value="insert">
<input type="hidden" name="type" value="update"> -->
<input type="submit" name="type" value="添加">
<input type="submit" name="type" value="修改">
</form>
<form action="/blog_article/wordProcess.html" method="post">
<input type="text" name="word">
<input type="hidden" name="type" value="delete">
<input type="submit" value="删除">
</form>
</body>
</html>
2、wordProcess.php
<?php
//接收类型 看看用户做什么(查询、添加....)
$type=$_REQUEST['type'];
//echo $type;
//exit();
//创建xml文档对象
$doc=new DOMDocument();
$doc->load("words.xml");
//进行判断
if($type=="query"){
//获取用户输入的值
$enword=$_REQUEST['enword'];
//判断是否进入查询
$isEnter=false;
//获取所有单词节点
$words=$doc->getElementsByTagName("word");
//遍历单词节点
for($i=0;$i<$words->length;$i++){
$word_node=$words->item($i);
//获取不同的语种
$en_word=getNodeVal($word_node,"en");
$zh_word=getNodeVal($word_node,"zh");
//查询
if($enword==$en_word){
$isEnter=true;
echo $enword."的中文意思是:".getNodeVal($word_node,"zh");
echo "<br/><a href='/blog_article/wordView.html'>返回继续查询</a>";
}else if($enword==$zh_word){
$isEnter=true;
echo $enword."的英文意思是:".getNodeVal($word_node,"en");
echo "<br/><a href='/blog_article/wordView.html'>返回继续查询</a>";
}
}
if(!$isEnter){
echo "无法查询";
echo "<br/><a href='/blog_article/wordView.html'>返回继续查询</a>";
}
}else if($type=="添加"){
//接收
$enword=$_REQUEST['enword'];
$zhword=$_REQUEST['zhword'];
if(!empty($enword)&&!empty($zhword)){
//获取根节点
$root=$doc->getElementsByTagName("words")->item(0);
$word=$doc->createElement("word");
$en=$doc->createElement("en",$enword);
$zh=$doc->createElement("zh",$zhword);
//进行挂载
$root->appendChild($word);
$word->appendChild($en);
$word->appendChild($zh);
//保存xml文件
$doc->save("words.xml");
echo "添加成功<br/><a href='/blog_article/wordView.html'>返回继续操作</a>";
}else{
echo "请输入单词";
echo "<br/><a href='/blog_article/wordView.html'>返回继续操作</a>";
exit();
}
}else if($type=="delete"){
$word=$_REQUEST['word'];
//获取所有单词节点
$words=$doc->getElementsByTagName("word");
$isEnter=false;
//遍历单词节点
for($i=0;$i<$words->length;$i++){
$word_node=$words->item($i);
//获取不同的语种
$en_word=getNodeVal($word_node,"en");
$zh_word=getNodeVal($word_node,"zh");
//查询
if($word==$en_word || $word==$zh_word){
$isEnter=true;
//找到父节点
$word_node->parentNode->removeChild($word_node);
$doc->save("words.xml");
echo "删除成功<br/><a href='/blog_article/wordView.html'>返回继续操作</a>";
}
}
if(!$isEnter){
echo "操作失败";
echo "<br/><a href='/blog_article/wordView.html'>返回继续操作</a>";
}
}else if($type="修改"){
//接收
$enword=$_REQUEST['enword'];
$zhword=$_REQUEST['zhword'];
if(!empty($enword)&&!empty($zhword)){
//获取所有单词节点
$words=$doc->getElementsByTagName("word");
//遍历单词节点
$isEnter=false;
for($i=0;$i<$words->length;$i++){
$word_node=$words->item($i);
//获取不同的语种
$en_word=getNodeVal($word_node,"en");
$zh_word=getNodeVal($word_node,"zh");
//查询
if($enword==$en_word && $zhword!=$zh_word){
//修改中文
$isEnter=true;
//获取zh节点
$zh=$word_node->getElementsByTagName("zh")->item(0);
$zh->nodeValue=$zhword;
$doc->save("words.xml");
echo "修改成功";
echo "<br/><a href='/blog_article/wordView.html'>返回继续操作</a>";
}else if($enword!=$en_word && $zhword==$zh_word){
//修改因为
$isEnter=true;
$en=$word_node->getElementsByTagName("en")->item(0);
$en->nodeValue=$enword;
$doc->save("words.xml");
echo "修改成功";
echo "<br/><a href='/blog_article/wordView.html'>返回继续操作</a>";
}
}
if(!$isEnter){
echo "没有做任何修改";
echo "<br/><a href='/blog_article/wordView.html'>返回继续操作</a>";
}
}else{
echo "请输入需要修改的单词";
echo "<br/><a href='/blog_article/wordView.html'>返回继续操作</a>";
exit();
}
}
//获取节点的文本值
function getNodeVal(&$MyNode,$tagName){
return $MyNode->getElementsByTagName($tagName)->item(0)->nodeValue;
}
?>
3、words.xml
<words><word><en>boy</en><zh>男孩</zh></word><word><en>girl</en><zh>女孩</zh></word><word><en>fire</en><zh>火</zh></word><word><en>word</en><zh>词库</zh></word></words>
1,用户注册页面 register.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户注册---www.</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<!--
<link rel="stylesheet" type="text/css" href="/blog_article/styles.css">
-->
<script type="text/javascript">
//创建ajax引擎
function getXMLHttpRequest(){
var xmlHttp;
if(window.ActiveXObject){
// Internet Explorer
xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
}else{
// Firefox, Opera 8.0+, Safari
xmlHttp= new XMLHttpRequest();
}
return xmlHttp;
}
//验证用户
var xmlHttpRequest;
function checkUser(){
xmlHttpRequest=getXMLHttpRequest();
//清除缓存 get 方式提交
/* var url="/ajax/registerProcess.php?time="+new Date()+"&username="+$("username").value;
//var url="/ajax/registerProcess.php?username="+$("username").value;
xmlHttpRequest.open("get",url,true);
//触发器
xmlHttpRequest.onreadystatechange=check;
xmlHttpRequest.send(null);
*/
//post 方式提交
var url="/ajax/registerProcess.php";
var data="username="+$("username").value;
xmlHttpRequest.open("post",url,true);
//post请求需要加入这句
xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//触发器
xmlHttpRequest.onreadystatechange=check;
xmlHttpRequest.send(data);
}
//回调函数
function check(){
if(xmlHttpRequest.readyState==4){
if(xmlHttpRequest.status=='200'){
//window.alert("OK");
//window.alert("返回用户名"+xmlHttpRequest.responseText);
//$("myres").value=xmlHttpRequest.responseText;
//返回是个对象
/* //处理xml数据
//window.alert(xmlHttpRequest.responseXML);
var mes=xmlHttpRequest.responseXML.getElementsByTagName("mes");
var mes_val=mes[0].childNodes[0].nodeValue;
$("myres").value=mes_val;
*/
//处理json数据 使用eval()函数把返回的字符串转成对应的对象
var mes=xmlHttpRequest.responseText;
var mes_obj=eval("("+mes+")");
//window.alert(mes_obj);
$("myres").value=mes_obj.res;
}
}
}
//获取dom对象
function $(id){
return document.getElementById(id);
}
</script>
</head>
<body>
<form action="/blog_article/.html" method="post">
用户名字:<input type="text" name="username1" id="username"><input type="button" onclick="checkUser();" value="验证用户名">
<input type="text" id="myres">
<br/>
用户密码:<input type="password" name="password"><br>
电子邮件:<input type="text" name="email"><br/>
<input type="submit" value="用户注册">
</form>
</body>
</html>
2,接收用户注册信息 registerProcess.php
<?php
//告诉浏览器返回的数据是xml格式
//header("Content-Type: text/xml;charset=utf-8");
//告诉浏览器返回的数据是文本格式
header("Content-Type: text/html;charset=utf-8");
//告诉浏览器不要缓存数据
header("Cache-Control: no-cache");
//接收
//$username=$_GET['username'];
$username=$_POST['username'];
$info="";
if($username=="test1"){
//echo "用户已注册";
//$info.="<res><mes>用户名已注册</mes></res>";
$info='{"res":"用户名已注册"}';
}else{
//echo "可以注册";
//$info.="<res><mes>可以注册</mes></res>";
$info='{"res":"可以注册"}';
}
echo $info;
?>
演示效果图: