標籤:ade 字串 led 採樣 extension 數組 字母 info 構造
<?php
//封裝一個表單驗證類
//中文驗證、郵箱驗證、電話號碼、手機、QQ、身份證、(由字母、數字、底線組成,不能以數字開頭)
header(‘content-type:text/html;charset=utf-8‘);
class Form{
/*
//中文驗證的方法
//參數:$str,$num1,$num2
//傳回值:匹配成功返回匹配的次數
*/
public function checkChina($str,$num1,$num2=‘‘){
//正則
$reg=‘/^[\x{4e00}-\x{9fa5}]{‘.$num1.‘,‘.$num2.‘}‘.‘$/u‘;
return preg_match($reg,$str);
}
/*
//郵箱驗證
//參數:$email
//傳回值: 匹配成功返回匹配的次數
*/
public function checkEmail($email){
//正則
$reg=‘/^\[email protected]\w+[.]com|cn|net$/‘;
return preg_match($reg,$email);
}
/*
//驗證身份證
//參數 $card
//傳回值: 匹配成功返回匹配的次數
*/
public function checkCard($card){
//正則
$reg=‘/^(\d{18}|\d{17}x)$/‘;
return preg_match($reg,$card);
}
/*
//要求輸入內容由數字、字母、底線組成,不能以數字開頭,有位元限制
//參數:$str,$num1,$num2
//傳回值: 匹配成功返回匹配的次數
*/
public function checkStr($str,$num1,$num2){
//正則
$reg=‘/^[a-zA-Z_]\w{‘.($num1-1).‘,‘.($num2-1).‘}$/‘;
return preg_match($reg,$str);
}
}
<?php
//圖片處理工具類
class Image{
//屬性
private $thumb_width; //縮圖的寬
private $thumb_height;
//錯誤屬性
public $thumb_error;
//構造方法
public function __construct($width = 0,$height = 0){
$this->thumb_width = ($width == 0) ? $GLOBALS[‘config‘][‘admin_goods_thumb‘][‘width‘] : $width;
$this->thumb_height = ($height == 0) ? $GLOBALS[‘config‘][‘admin_goods_thumb‘][‘height‘] : $height;
}
/*
* 製作縮圖
* @param1 string $src,原圖路徑,/uploads/20150122101010abcdef.gif
* @param2 string $path,縮圖儲存路徑/uploads/thumb_20150122101010abcdef.gif
* @return 縮圖的名字
*/
public function makeThumb($src,$path){
//判斷原圖是否存在
if(!file_exists($src)){
$this->thumb_error = ‘原圖不存在!‘;
return false;
}
//開啟原圖資源
//擷取能夠使用的尾碼
$ext = $this->getFunctionName($src); //gif
//拼湊函數名
$open = ‘imagecreatefrom‘ . $ext; //imagecreatefromgif
$save = ‘image‘ . $ext; //imagegif
//如果不清楚;echo $open,$save;exit;
//可變函數開啟原圖資源
$src_img = $open($src); //利用可變函數開啟圖片資源
//imagecreatefromgif($src)
//縮圖資源
$dst_img = imagecreatetruecolor($this->thumb_width,$this->thumb_height);
//背景色填充白色
$dst_bg_color = imagecolorallocate($dst_img,255,255,255);
imagefill($dst_img,0,0,$dst_bg_color);
//寬高比確定寬高
$dst_size = $this->thumb_width / $this->thumb_height;
//擷取原圖資料
$file_info = getimagesize($src);
$src_size = $file_info[0]/$file_info[1];
//求出縮圖寬和高
if($src_size > $dst_size){
//原圖寬高比大於縮圖
$width = $this->thumb_width;
$height = round($width / $src_size);
}else{
$height = $this->thumb_height;
$width = round($height * $src_size);
}
//求出縮圖起始位置
$dst_x = round($this->thumb_width - $width)/2;
$dst_y = round($this->thumb_height - $height)/2;
//製作縮圖
if(imagecopyresampled($dst_img,$src_img,$dst_x,$dst_y,0,0,$width,$height,$file_info[0],$file_info[1])){
//採樣成功:儲存,將檔案儲存到對應的路徑下
$thumb_name = ‘thumb_‘ . basename($src);
$save($dst_img,$path . ‘/‘ . $thumb_name);
//儲存成功
return $thumb_name;
}else{
//採樣失敗
$this->thumb_error = ‘縮圖採樣失敗!‘;
return false;
}
}
/*
* 擷取檔案要調用的函數名
* @param1 string $file,檔案名稱字
* @return 通過檔案尾碼名得到的函數字串
*/
private function getFunctionName($file){
//得到檔案的尾碼
$file_info = pathinfo($file);
$ext = $file_info[‘extension‘]; //尾碼:gif,png,jpg,jpeg,pjpeg
//imagecreatefromgif,imagecreatefromjpeg,imagecreatefrompng
//定義一個數組儲存函數名
$func = array(
‘gif‘ => ‘gif‘,
‘png‘ => ‘png‘,
‘jpg‘ => ‘jpeg‘,
‘jpeg‘ => ‘jpeg‘,
‘pjpeg‘ => ‘jpeg‘
);
//傳回值
return $func[$ext];
}
}
php表單和縮圖處理類是什麼樣呢