php中强大的图象库:GD库,万人景仰的好东东。
要使用gd库,需要在php.ini里增加一行引用:extension=php_gd2.dll
然后重启apache。
配置好以后,可以搞个测试页 var_dump(gd_info());
如果,有数据输出,则可以表明GD库引用成功。
下面来看具体的例子:
1、图片上传页面 upload.html
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title>图片上传-www.</title>
</head>
<body>
<h1>文件上传(只允许上传jpg类型图片)</h1>
<form enctype="multipart/form-data" action="/blog_article/upload_img.html" method="post">
<input name="upfile" type="file"><br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
2、处理页面 upload_img.php
<?php
//上传图片保存地址
$uploadfile = "upfiles/".$_FILES['upfile']['name'];
//缩略图保存地址
$smallfile = "upfiles/small_".$_FILES['upfile']['name'];
if($_FILES['upfile']['type'] != "image/jpeg")
{
echo '文件类型错误';
}
else
{
move_uploaded_file($_FILES['upfile']['tmp_name'],$uploadfile); //上传文件
$dstW=200;//缩略图宽
$dstH=200;//缩略图高
$src_image=ImageCreateFromJPEG($uploadfile);
$srcW=ImageSX($src_image); //获得图片宽
$srcH=ImageSY($src_image); //获得图片高
$dst_image=ImageCreateTrueColor($dstW,$dstH);
ImageCopyResized($dst_image,$src_image,0,0,0,0,$dstW,$dstH,$srcW,$srcH);
ImageJpeg($dst_image,$smallfile);
echo '文件上传成功<br>';
echo "<img src='/blog_article/$smallfile/index.html' />";
}
?>
以上代码,比较简单,有兴趣的朋友,可以研究下php手册中的gd库函数。
您可能感兴趣的文章:
php 多图片上传的简单例子(图文)
又一个生成图片缩略图的函数
一个创建图片缩略图的函数
php图片加水印与上传图片加水印类
php为图片加中文水印的代码
php上传文件并添加文字与图片水印的代码
php 上传图片加水印且支持透明的代码
php上传图片功能的实现
一个php下载文件函数的简单应用实例,供初学的朋友参考。
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-cn">
<head>
<title>php下载文件函数</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="UTF-8" />
</head>
<form method="post">
<input name="url" size="20" />
<input name="submit" type="submit" />
<!-- <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />-->
</form>
<?php
set_time_limit(24*60*60);
if (!isset()($_POST['submit'])) die ();
$destination_folder = './down/'; // 文件夹保存下载文件。必须以斜杠结尾
$url = $_POST['url'];
$newfname = $destination_folder.basename($url);
$file = fopen($url, "rb");
if ($file) {
$newf = fopen($newfname, "wb");
if ($newf) while (!feof($file)) {
fwrite($newf, fread($file, 1024*8), 1024*8);
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
?>
php数据备份的代码,包括单表备份、整表备份、导入数据库,有需要的朋友,不妨参考下了。
1、单表备份
class Db
{
var $conn;
function Db($host="localhost",$user="root",$pass="root",$db="test")
{
if(!$this->conn=mysql_connect($host,$user,$pass))
die("can't connect to mysql sever");
mysql_select_db($db,$this->conn);
mysql_query("SET NAMES 'UTF-8'");
}
function execute($sql)
{
return mysql_query($sql,$this->conn);
}
function findCount($sql)
{
$result=$this->execute($sql);
return mysql_num_rows($result);
}
function findBySql($sql)
{
$array=array();
$result=mysql_query($sql);
$i=0;
while($row=mysql_fetch_assoc($result))
{
$array[$i]=$row;
$i++;
}
return $array;
}
//$con的几种情况
//空:返回全部记录
//array:eg. array('id'=>'1') 返回id=1的记录
//string :eg. 'id=1' 返回id=1的记录
function toExtJson($table,$start="0",$limit="10",$cons="")
{
$sql=$this->generateSql($table,$cons);
$totalNum=$this->findCount($sql);
$result=$this->findBySql($sql." LIMIT ".$start." ,".$limit);
$resultNum = count($result);//当前结果数
$str="";
$str.= "{";
$str.= "'totalCount':'$totalNum',";
$str.="'rows':";
$str.="[";
for($i=0;$i<$resultNum;$i++){
$str.="{";
$count=count($result[$i]);
$j=1;
foreach($result[$i] as $key=>$val)
{
if($j<$count)
{
$str.="'".$key."':'".$val."',";
}
elseif($j==$count)
{
$str.="'".$key."':'".$val."'";
}
$j++;
}
$str.="}";
if ($i != $resultNum-1) {
$str.= ",";
}
}
$str.="]";
$str.="}";
return $str;
}
function generateSql($table,$cons)
{
$sql="";//sql条件
$sql="select * from ".$table;
if($cons!="")
{
if(is_array($cons))
{
$k=0;
foreach($cons as $key=>$val)
{
if($k==0)
{
$sql.="where '";
$sql.=$key;
$sql.="'='";
$sql.=$val."'";
}else
{
$sql.="and '";
$sql.=$key;
$sql.="'='";
$sql.=$val."'";
}
$k++;
}
}else
{
$sql.=" where ".$cons;
}
}
return $sql;
}
function toExtXml($table,$start="0",$limit="10",$cons="")
{
$sql=$this->generateSql($table,$cons);
$totalNum=$this->findCount($sql);
$result=$this->findBySql($sql." LIMIT ".$start." ,".$limit);
$resultNum = count($result);//当前结果数
header("Content-Type: text/xml");
$xml="<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
$xml.="<xml>\n";
$xml.="\t<totalCount>".$totalNum."</totalCount>\n";
$xml.="\t<items>\n";
for($i=0;$i<$resultNum;$i++){
$xml.="\t\t<item>\n";
foreach($result[$i] as $key=>$val)
$xml.="\t\t\t<".$key.">".$val."</".$key.">\n";
$xml.="\t\t</item>\n";
}
$xml.="\t</items>\n";
$xml.="</xml>\n";
return $xml;
}
//输出word表格
function toWord($table,$mapping,$fileName)
{
header('Content-type: application/doc');
header('Content-Disposition: attachment; filename="'.$fileName.'.doc"');
echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="[url=http://www.w3.org/TR/REC-html40]http://www.w3.org/TR/REC-html40[/url]">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>'.$fileName.'</title>
</head>
<body>';
echo'<table border=1><tr>';
if(is_array($mapping))
{
foreach($mapping as $key=>$val)
echo'<td>'.$val.'</td>';
}
echo'</tr>';
$results=$this->findBySql('select * from '.$table);
foreach($results as $result)
{
echo'<tr>';
foreach($result as $key=>$val)
echo'<td>'.$val.'</td>';
echo'</tr>';
}
echo'</table>';
echo'</body>';
echo'</html>';
}
function toExcel($table,$mapping,$fileName)
{
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=".$fileName.".xls");
echo'<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="[url=http://www.w3.org/TR/REC-html40]http://www.w3.org/TR/REC-html40[/url]">
<head>
<meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
<meta http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name></x:Name>
<x:WorksheetOptions>
<x:DisplayGridlines/>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml><![endif]-->
</head>
<body link=blue vlink=purple leftmargin=0 topmargin=0>';
echo'<table width="100%" border="0" cellspacing="0" cellpadding="0">';
echo'<tr>';
if(is_array($mapping))
{
foreach($mapping as $key=>$val)
echo'<td>'.$val.'</td>';
}
echo'</tr>';
$results=$this->findBySql('select * from '.$table);
foreach($results as $result)
{
echo'<tr>';
foreach($result as $key=>$val)
echo'<td>'.$val.'</td>';
echo'</tr>';
}
echo'</table>';
echo'</body>';
echo'</html>';
}
function Backup($table)
{
if(is_array ($table))
{
$str="";
foreach($table as $tab)
$str.=$this->get_table_content($tab);
return $str;
}else{
return $this->get_table_content($table);
}
}
function Backuptofile($table,$file)
{
header("Content-disposition: filename=$file.sql");//所保存的文件名
header("Content-type: application/octetstream");
header("Pragma: no-cache");
header("Expires: 0");
if(is_array ($table))
{
$str="";
foreach($table as $tab)
$str.=$this->get_table_content($tab);
echo $str;
}else{
echo $this->get_table_content($table);
}
}
function Restore($table,$file="",$content="")
{
//排除file,content都为空或者都不为空的情况
if(($file==""&&$content=="")||($file!=""&&$content!=""))
echo"参数错误";
$this->truncate($table);
if($file!="")
{
if($this->RestoreFromFile($file))
return true;
else
return false;
}
if($content!="")
{
if($this->RestoreFromContent($content))
return true;
else
return false;
}
}
//清空表,以便恢复数据
function truncate($table)
{
if(is_array ($table))
{
$str="";
foreach($table as $tab)
$this->execute("TRUNCATE TABLE $tab");
}else{
$this->execute("TRUNCATE TABLE $table");
}
}
function get_table_content($table)
{
$results=$this->findBySql("select * from $table");
$temp = "";
$crlf="<br>";
foreach($results as $result)
{
/*(";
foreach($result as $key=>$val)
{
$schema_insert .= " `".$key."`,";
}
$schema_insert = ereg_replace(",$", "", $schema_insert);
$schema_insert .= ")
*/
$schema_insert = "INSERT INTO $table VALUES (";
foreach($result as $key=>$val)
{
if($val != "")
$schema_insert .= " '".addslashes($val)."',";
else
$schema_insert .= "NULL,";
}
$schema_insert = ereg_replace(",$", "", $schema_insert);
$schema_insert .= ");$crlf";
$temp = $temp.$schema_insert ;
}
return $temp;
}
function RestoreFromFile($file){
if (false !== ($fp = fopen($file, 'r'))) {
$sql_queries = trim(fread($fp, filesize($file)));
$this->splitMySqlFile($pieces, $sql_queries);
foreach ($pieces as $query) {
if(!$this->execute(trim($query)))
return false;
}
return true;
}
return false;
}
function RestoreFromContent($content)
{
$content = trim($content);
$this->splitMySqlFile($pieces, $content);
foreach ($pieces as $query) {
if(!$this->execute(trim($query)))
return false;
}
return true;
}
function splitMySqlFile(&$ret, $sql)
{
$sql= trim($sql);
$sql=split(';',$sql);
$arr=array();
foreach($sql as $sq)
{
if($sq!="");
$arr[]=$sq;
}
$ret=$arr;
return true;
}
}
$db=new db();
// 生成 word
//$map=array('No','Name','Email','Age');
//echo $db->toWord('test',$map,'档案');
// 生成 Excel
//$map=array('No','Name','Email','Age');
//echo $db->toExcel('test',$map,'档案');
// 生成 Xml
//echo $db->toExtXml('test',0,20);
// 生成 Json
//echo $db->toExtJson('test',0,20);
//备份
//echo $db->Backuptofile('test','backup');
?>
2、整表备份