吃饭的东西,不能忘了啊。。
<?php
/**
* 图片缩略图类,只可以处理'jpg','gif','png'格式
*
* 使用方法:
* $img = new Thumb('./upload/','./thumb/');
* //把当前目录下的a.jpg图片生成缩略图,图片名称为aaa.jpg,图片宽、高最大为100px
* $img->doimage('a.jpg','aaa.jpg',100);
*
*/
class Thumb {
private $bigimagepath;
private $smalllimagepath;
private $imagetype = array('jpg','jpeg','jpe','gif','png');
public function __construct($bigimagepath='./',$smallimagepath='./') {
$this->bigimagepath = $bigimagepath;
$this->smalllimagepath = $smallimagepath;
}
public function image($bigimagepath='./',$smallimagepath='./') {
$this->__construct($bigimagepath,$smallimagepath);
}
public function setbigimagepath($path){
$this->bigimagepath = $path;
}
public function setsmallimagepath($path) {
$this->smalllimagepath = $path;
}
public function doimage($big_filename,$small_filename,$length,$quality=75) {
$big_filename = $this->bigimagepath.$big_filename;
$res = $this->resizeImage($big_filename,$length);
if ($res==false) {
return false;
}
$small_filename = $this->smalllimagepath.$small_filename;
return $this->saveImage($res,$small_filename,$quality);
}
/**
* 根据文件扩展名取得图片的类型或false(表示不是图片)
*
* @param $filename 图片的文件名
*/
private function getImageType($filename) {
$file = pathinfo($filename);
$ext=strtolower($file['extension']);
if (in_array($ext,$this->imagetype))
return $ext;
else return false;
}
/**
* 从给定的文件名创建图片(给定的图片类型只能是jpg、gif或png格式的)
*
* @param $filename 图片文件名
* @return 图片资源
*/
private function imageCreate($filename) {
$type=$this->getImageType($filename);
$p=null; //图片资源
switch ($type) {
case 'jpg':
$p=@imagecreatefromjpeg($filename);
break;
case 'jpeg':
$p=@imagecreatefromjpeg($filename);
break;
case 'jpe':
$p=@imagecreatefromjpeg($filename);
break;
case 'gif':
$p=@imagecreatefromgif($filename);
break;
case 'png':
$p=@imagecreatefrompng($filename);
break;
default:
return false;
break;//只能处理jpg、gif或png格式的图片
}
return $p;
}
/**
* 把图片的长边缩小为length个像素长,短边等比缩小,只能处理jpg、gif、png格式的图片
*
* @param $filename 图片文件名
* @param $length 长边的像素数
*
* @return 图片资源
*/
private function resizeImage($filename,$length) {
$img = $this->imageCreate($filename); //打开源图片
if ($img==false) {
return false;
}
$width=imagesx($img); //宽
$height=imagesy($img); //高
//如果原图片的尺寸比预计的缩略图的尺寸还小,直接返回原图
if ($width<=$length && $height<=$length) {
return $img;
}
//等比缩小
if ($width>$height) {
$w=$length;
$h=($height/$width)*$length;
} else if($width<$height) {
$h=$length;
$w=($width/$height)*$length;
} else {
//正方形的图片
$w=$h=$length;
}
$p=imagecreatetruecolor($w,$h); //新建缩略图
imagecopyresampled($p,$img,0,0,0,0,$w,$h,$width,$height); //缩小
return $p; //返回图片资源
}
/**
* 将图片保存为文件
*
* @param $res 图片资源
* @param string $filename 将要保存的文件名
* @param int $quality 图片质量(0-100)只在生成jpg格式的图片时有效
*
* @return 成功,返回true,否则返回false
*/
private function saveImage($res,$filename,$quality=75) {
//图片类型
$type=self::getImageType($filename);
switch($type) {
case 'jpe':
return @imagejpeg($res,$filename,$quality);
break;
case 'jpeg':
return @imagejpeg($res,$filename,$quality);
break;
case 'jpg':
return @imagejpeg($res,$filename,$quality);
break;
case 'gif':
return @imagegif($res,$filename);
break;
case 'png':
return @imagepng($res,$filename);
break;
default:
return false;
break;
}
return true;
}
}
?>
其实吧,挺简单的
<?php
ob_end_clean(); //终止并清除php的缓冲输出
ob_start(); //其实这里可以没有这个,不过如果要操作cookie或者session还是加上
@header("Expires: -1");
@header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
@header("Pragma: no-cache");
@header("Content-type: text/html; charset=utf-8");
//Your code here...
?>
<?php
/**
* 检查数组是否全是空值
*/
function isEmptyArray($arr) {
if(is_array($arr)) {
foreach($arr as $v) {
if(isEmptyArray($v) == false) {
return false;
}
}
} else {
return empty($arr);
}
return true;
}
/**
* 去除头尾空格和转义
*/
function escape($str,$as = true) {
if(is_array($str)) {
foreach($str as $key=>$val) {
$str[$key] = $this->escape($val,$as);
}
} else {
if($as) {
$str = addslashes(trim($str));
} else {
$str = trim($str);
}
}
return $str;
}
/**
* 判断是否有中文字符
*/
function isChinese($word) {
return preg_match ('/[^x0-x7F]/', $word);
}
/**
* 判断是否是只有中文文字
*/
function isCn($word) {
return preg_match("/^[x{4e00}-x{9fa5}]+$/u", $word);
}
/**
* 用以代替file_get_contents
* 需要cURL支持,当连接超时则返回false,不会卡死php页面
*/
function an_get_contents($url, $second = 5) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
/**
* 转换至UTF-8编码
* 适用于大部分中文情况
*/
function convertToUTF8($str) {
$charset = mb_detect_encoding($str, array('ASCII','UTF-8','GB2312','GBK','BIG5'));
if ($charset!='UTF-8') {
$str = mb_convert_encoding($str,'UTF-8',$charset);
}
return $str
}
/**
* 生成指定范围内指定个数的不重复随机数
*/
function getRand($min, $max, $num) {
if($max-$min<$num) return false;
$res = null;
$arr = range($min,$max);
shuffle($arr);
$rand_keys = array_rand($arr, $num);
foreach($rand_keys as $v) {
$res[] = $arr[$v];
}
return $res;
}
function add_page_css($s) {
$rs = '';
if(!is_array($s)) {
$rs = '<link href="res/css/'.$s.'.css" rel="stylesheet" type="text/css" />';
} else {
foreach($s as $v) {
$rs .= '<link href="res/css/'.$v.'.css" rel="stylesheet" type="text/css" />';
}
}
return $rs;
}
function add_page_js($s) {
$rs = '';
if(!is_array($s)) {
$rs = '<script type="text/javascript" src="res/js/'.$s.'.js"></script>';
} else {
foreach($s as $v) {
$rs .= '<script type="text/javascript" src="res/js/'.$v.'.js"></script>';
}
}
return $rs;
}
?>
<?php
/**
* Mysqli类
*
* @author 废墟
* @version v1.0 2009-08-18
* @link http://anerg.cn/
*/
class db_mysqli {
protected $mysqli;
protected $sql;
protected $rs;
protected $query_num = 0;
protected $fetch_mode = MYSQLI_ASSOC;
protected $cache_dir = './cache/';
protected $cache_time = 1800;
public function __construct($dbhost, $dbuser, $dbpass, $dbname) {
$this->mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if(mysqli_connect_errno()) {
$this->mysqli = false;
echo '<h2>'.mysqli_connect_error().'</h2>';
die();
} else {
$this->mysqli->set_charset("utf8");
}
}
public function __destruct() {
$this->free();
$this->close();
}
protected function free() {
@$this->rs->free();
}
protected function close() {
$this->mysqli->close();
}
protected function fetch() {
return $this->rs->fetch_array($this->fetch_mode);
}
protected function getQuerySql($sql, $limit = null) {
if (@preg_match("/[0-9]+(,[ ]?[0-9]+)?/is", $limit) && !preg_match("/ LIMIT [0-9]+(,[ ]?[0-9]+)?$/is", $sql)) {
$sql .= " LIMIT " . $limit;
}
return $sql;
}
protected function get_cache($sql,$method) {
include_once './cache.php';//若框架中使用__autoload(),这里可以不用加载文件
$cache = new cache($this->cache_dir,$this->cache_time);
$cache_file = md5($sql.$method);
$res = $cache->get_cache($cache_file);
if(!$res) {
$res = $this->$method($sql);
$cache->set_cache($cache_file, $res);
}
return $res;
}
public function query_num() {
return $this->query_num;
}
public function set_cache_dir($cache_dir) {
$this->cache_dir = $cache_dir;
}
public function set_cache_time($cache_time) {
$this->cache_time = $cache_time;
}
public function query($sql, $limit = null) {
$sql = $this->getQuerySql($sql, $limit);
$this->sql = $sql;
$this->rs = $this->mysqli->query($sql);
if (!$this->rs) {
echo "<h2>".$this->mysqli->error."</h2>";
die();
} else {
$this->query_num++;
return $this->rs;
}
}
public function getOne($sql) {
$this->query($sql, 1);
$this->fetch_mode = MYSQLI_NUM;
$row = $this->fetch();
$this->free();
return $row[0];
}
public function get_one($sql) { return $this->getOne($sql); }
public function cache_one($sql) {
$sql = $this->getQuerySql($sql, 1);
return $this->get_cache($sql, 'getOne');
}
public function getRow($sql, $fetch_mode = MYSQLI_ASSOC) {
$this->query($sql, 1);
$this->fetch_mode = $fetch_mode;
$row = $this->fetch();
$this->free();
return $row;
}
public function get_row($sql, $fetch_mode = MYSQLI_ASSOC) { return $this->getRow($sql); }
public function cache_row($sql) {
$sql = $this->getQuerySql($sql, 1);
return $this->get_cache($sql, 'getRow');
}
public function getAll($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) {
$this->query($sql, $limit);
$all_rows = array();
$this->fetch_mode = $fetch_mode;
while($rows = $this->fetch()) {
$all_rows[] = $rows;
}
$this->free();
return $all_rows;
}
public function get_all($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) { return $this->getAll($sql); }
public function cache_all($sql, $limit = null) {
$sql = $this->getQuerySql($sql, $limit);
return $this->get_cache($sql, 'getAll');
}
public function insert_id() {
return $this->mysqli->insert_id();
}
public function escape($str) {
if(is_array($str)) {
foreach($str as $key=>$val) {
$str[$key] = $this->escape($val);
}
} else {
$str = addslashes(trim($str));
}
return $str;
}
}
//用法
$db = new db_mysqli('localhost', 'root', 111222, 'dict');
$db->set_cache_time(10);
$db->set_cache_dir('./cache/sql/');
$sql = "select * from words order by word_id limit 10,10";
$res1 = $db->get_all($sql);
$res2 = $db->cache_all($sql);
echo $db->query_num(),'<br>';
?>
<?php
/**
* 文件缓存类
*
* @author 废墟
* @version v1.01 2009-08-18
* @link http://anerg.cn/
*/
class cache {
private $cache_time;
private $cache_dir;
public function __construct($cache_dir = './cache/', $cache_time = 3600) {
$this->cache_time = $cache_time;
$this->cache_dir = $cache_dir;
}
public function set_cache_dir($cache_dir) {
$this->cache_dir = $cache_dir;
}
public function set_cache_time($cache_time) {
$this->cache_time = $cache_time;
}
public function get_cache($cache_file) {
$_CACHE = array();
$filename = $this->cache_dir.'/'.$cache_file.".cache.php";
if(!file_exists($filename) || time() - filemtime($filename) > $this->cache_time) {
return false;
} else {
return unserialize(file_get_contents($filename));
}
}
public function set_cache($cache_file,$data) {
$filename = $this->cache_dir.'/'.$cache_file.".cache.php";
if( $this->mkpath($this->cache_dir) ) {
$out = serialize($data);
file_put_contents($filename, $out);
}
}
public function del_cache($cache_file) {
return unlink($this->cache_dir.'/'.$cache_file.".cache.php");
}
public function mkpath($dir) {
return is_dir($dir) or ($this->mkpath(dirname($dir)) and (mkdir($dir, 0777) and chmod($dir,0777)));
}
}
//
//$cache = new cache();
//$cache->set_cache_dir('./cache/ss/ff/mm/');
//$cache->set_cache_time(10);
//$cache_file = '321ewqe3412132';
//$data = array("1ee"=>"teseweq222ewt");
//$cache->set_cache($cache_file, $data);
//print_r($cache->get_cache($cache_file));
?>







