第一步:生成文件上傳類
/* 該用於文件上傳
* 有4個公有方法可以在對象外部調用:
* __construct()構造方法用於初使化成員屬性
* uploadFile()方法用於上傳文件
* getNewFileName()方法用於獲取上傳成功後的文件名稱
* getErrorMsg()方法用於上傳失敗後獲取錯誤提示信息
* 其它屬性和方法都被本類封裝,不可以在對象外部調用
*/
class FileUpload {
private $filepath; // 上傳文件的目的路徑
private $allowtype = array(‘jpg’,’gif’,’png’); //充許上傳文件的類型,使用小字母
private $maxsize = 1000000; //允許文件上傳的最大長度1m
private $israndname = true; //是否隨機重命名 false為不隨機
private $originName; //源文件名
private $tmpFileName; //臨時文件名
private $fileType; //文件類型(文件後綴)
private $fileSize; //文件大小
private $newFileName; //新文件名
private $errorNum = 0; //錯誤號
private $errorMess=””; //錯誤報告消息
/* 構造方法:為成員屬性初使化
* 參數$options:為一個數組,數組下標為成員員屬性名稱字符串
* 本類需要初使化的屬性有 filepath, allowtype, maxsize,israndname四個屬性,其中filepath為必須設置的屬性
* 使用的格式為 new FileUpload(array(‘filepath’=>’./uploads’, ‘maxsize’=>10000000)) 的格式
*/
function __construct($options=array()) {
foreach ($options as $key=>$val) {
$key=strtolower($key); //在為成員屬性設置值時,不區分大小寫
if (!in_array($key,get_class_vars(get_class($this))))
continue;
$this->setOption($key, $val);
}
}
/* 調用該方法上傳文件
* 參數: 上傳文件的表單名稱 例如:<input type=”file” name=”myfile”> 參數則為myfile
* 返回值: 如果上傳成功返回數字0,如果上傳失敗則返回小於0的數,如:-1、-2、-3、-4、-5中的一個
*/
function uploadFile($fileField) {
$return=true;
if(!$this->checkFilePath()) {//檢查文件路徑
$this->errorMess=$this->getError();
return false;
}
$name=$_FILES[$fileField][‘name’];
$tmp_name=$_FILES[$fileField][‘tmp_name’];
$size=$_FILES[$fileField][‘size’];
$error=$_FILES[$fileField][‘error’];
if(is_Array($name)){ //如果是多個文件上傳則$file[“name”]會是一個數組
$errors=array();
for($i = 0; $i < count($name); $i++){
if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i] )) {//設置文件信息
if(!$this->checkFileSize() || !$this->checkFileType()){
$errors[]=$this->getError();
$return=false;
}
}else{
$errors[]=$this->getError();
$return=false;
}
if(!$return) // 如果有問題,則重新初使化屬性
$this->setFiles();
}
if($return){
$fileNames=array(); //存放所有上傳後文件名的變量數組
for($i = 0; $i < count($name); $i++){
if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i] )) {//設置文件信息
$this->setNewFileName(); //設置新文件名
if(!$this->copyFile()){
$errors[]=$this->getError();
$return=false;
}
$fileNames[]=$this->newFileName;
}
}
$this->newFileName=$fileNames;
}
$this->errorMess=$errors;
return $return;
} else {
if($this->setFiles($name,$tmp_name,$size,$error)) {//設置文件信息
if($this->checkFileSize() && $this->checkFileType()){
$this->setNewFileName(); //設置新文件名
if($this->copyFile()){ //上傳文件 返回0為成功, 小於0都為錯誤
return true;
}else{
echo ‘3333333333333’;
$return=false;
}
}else{
$return=false;
}
} else {
$return=false;
}
if(!$return)
$this->errorMess=$this->getError();
return $return;
}
}
/* 獲取上傳後的文件名稱
* 沒有參數
* 返回值:上傳後,新文件的名稱
*/
public function getNewFileName(){
return $this->newFileName;
}
public function getErrorMsg(){
return $this->errorMess;
}
/* 上傳失敗後,調用該方法則返回,上傳出錯信息
* 沒有參數
* 返回值:返回上傳文件出錯的信息提示
*/
private function getError() {
$str = “上傳文件<font color=’red’>{$this->originName}</font>時出錯 : “;
switch ($this->errorNum) {
case 4: $str .= “沒有文件被上傳”; break;
case 3: $str .= “文件只有部分被上傳”; break;
case 2: $str .= “上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值”; break;
case 1: $str .= “上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值”; break;
case -1: $str .= “未允許類型”; break;
case -2: $str .= “文件過大,上傳的文件不能超過{$this->maxsize}個字節”; break;
case -3: $str .= “上傳失敗”; break;
case -4: $str .= “建立存放上傳文件目錄失敗,請重新指定上傳目錄”; break;
case -5: $str .= “必須指定上傳文件的路徑”; break;
default: $str .= “未知錯誤”;
}
return $str.'<br>’;
}
//設置和$_FILES有關的內容
private function setFiles($name=””, $tmp_name=””, $size=0, $error=0) {
$this->setOption(‘errorNum’, $error);
if($error)
return false;
$this->setOption(‘originName’, $name);
$this->setOption(‘tmpFileName’,$tmp_name);
$aryStr = explode(“.”, $name);
$this->setOption(‘fileType’, strtolower($aryStr[count($aryStr)-1]));
$this->setOption(‘fileSize’, $size);
return true;
}
//為單個成員屬性設置值
private function setOption($key, $val) {
$this->$key = $val;
}
//設置上傳後的文件名稱
private function setNewFileName() {
if ($this->israndname) {
$this->setOption(‘newFileName’, $this->proRandName());
} else{
$this->setOption(‘newFileName’, $this->originName);
}
}
//檢查上傳的文件是否是合法的類型
private function checkFileType() {
if (in_array(strtolower($this->fileType), $this->allowtype)) {
return true;
}else {
$this->setOption(‘errorNum’, -1);
return false;
}
}
//檢查上傳的文件是否是允許的大小
private function checkFileSize() {
if ($this->fileSize > $this->maxsize) {
$this->setOption(‘errorNum’, -2);
return false;
}else{
return true;
}
}
//檢查是否有存放上傳文件的目錄
private function checkFilePath() {
if(empty($this->filepath)){
$this->setOption(‘errorNum’, -5);
return false;
}
if (!file_exists($this->filepath) || !is_writable($this->filepath)) {
if (!@mkdir($this->filepath, 0755)) {
$this->setOption(‘errorNum’, -4);
return false;
}
}
return true;
}
//設置隨機文件名
private function proRandName() {
$fileName=date(‘YmdHis’).”_”.rand(100,999); //獲取隨機文件名
return $fileName.’.’.$this->fileType; //返迴文件名加原擴展名
}
//複製上傳文件到指定的位置
private function copyFile() {
if(!$this->errorNum) {
$filepath = rtrim($this->filepath, ‘/’).’/’;
$filepath .= $this->newFileName;
if (@move_uploaded_file($this->tmpFileName, $filepath)) {
return true;
}else{
$this->setOption(‘errorNum’, -3);
return false;
}
} else {
return false;
}
}
}
第二步:生成圖像處理類
class Image {
private $path;
//構造方法用來對圖片所在位置進行初使化
function __construct($path=”./”){
$this->path=rtrim($path, “/”).”/”;
}
/* 對圖片進行縮放
*
* 參數$name: 是需要處理的圖片名稱
* 參數$width:是縮放後的寬度
* 參數$height:是縮放後的高度
* 參數$qz: 是新圖片的名稱前綴
* 返回值:就是縮放後的圖片名稱,失敗則返回false
*
*/
function thumb($name, $width, $height, $qz=”th_”){
//獲取圖片信息
$imgInfo=$this->getInfo($name); //圖片的寬度,高度,類型
//獲取圖片資源, 各種類型的圖片都可以創建資源 jpg, gif, png
$srcImg=$this->getImg($name, $imgInfo);
//獲取計算圖片等比例之後的大小, $size[“width”], $size[“height”]
$size=$this->getNewSize($name, $width, $height, $imgInfo);
//獲取新的圖片資源, 處理一下gif透明背景
$newImg=$this->kidOfImage($srcImg, $size, $imgInfo);
//另存為一個新的圖片,返回新的縮放後的圖片名稱
return $this->createNewImage($newImg, $qz.$name, $imgInfo);
}
private function createNewImage($newImg, $newName, $imgInfo){
switch($imgInfo[“type”]){
case 1://gif
$result=imageGif($newImg, $this->path.$newName);
break;
case 2://jpg
$result=imageJPEG($newImg, $this->path.$newName);
break;
case 3://png
$return=imagepng($newImg, $this->path.$newName);
break;
}
imagedestroy($newImg);
return $newName;
}
private function kidOfImage($srcImg, $size, $imgInfo){
$newImg=imagecreatetruecolor($size[“width”], $size[“height”]);
$otsc=imagecolortransparent($srcImg);
if($otsc >=0 && $otsc <= imagecolorstotal($srcImg)){
$tran=imagecolorsforindex($srcImg, $otsc);
$newt=imagecolorallocate($newImg, $tran[“red”], $tran[“green”], $tran[“blue”]);
imagefill($newImg, 0, 0, $newt);
imagecolortransparent($newImg, $newt);
}
imagecopyresized($newImg, $srcImg, 0, 0, 0, 0, $size[“width”], $size[“height”], $imgInfo[“width”], $imgInfo[“height”]);
imagedestroy($srcImg);
return $newImg;
}
private function getNewSize($name, $width, $height, $imgInfo){
$size[“width”]=$imgInfo[“width”];
$size[“height”]=$imgInfo[“height”];
//縮放的寬度如果比原圖小才重新設置寬度
if($width < $imgInfo[“width”]){
$size[“width”]=$width;
}
//縮放的高度如果比原圖小才重新設置高度
if($height < $imgInfo[“height”]){
$size[“height”]=$height;
}
//圖片等比例縮放的算法
if($imgInfo[“width”]*$size[“width”] > $imgInfo[“height”] * $size[“height”]){
$size[“height”]=round($imgInfo[“height”]*$size[“width”]/$imgInfo[“width”]);
}else{
$size[“width”]=round($imgInfo[“width”]*$size[“height”]/$imgInfo[“height”]);
}
return $size;
}
private function getInfo($name){
$data=getImageSize($this->path.$name);
$imageInfo[“width”]=$data[0];
$imageInfo[“height”]=$data[1];
$imageInfo[“type”]=$data[2];
return $imageInfo;
}
private function getImg($name, $imgInfo){
$srcPic=$this->path.$name;
switch($imgInfo[“type”]){
case 1: //gif
$img=imagecreatefromgif($srcPic);
break;
case 2: //jpg
$img=imageCreatefromjpeg($srcPic);
break;
case 3: //png
$img=imageCreatefrompng($srcPic);
break;
default:
return false;
}
return $img;
}
/* 功能:為圖片加水印圖片
* 參數$groundName: 背景圖片,即需要加水印的圖片
* 參數$waterName: 水錢圖片
* 參數#aterPost:水印位置, 10種狀態,
* 0為隨機位置
*
* 1. 為頂端居左 2. 為頂端居中 3 為頂端居右
* 4 為中部居左 5. 為中部居中 6 為中部居右
* 7 . 為底端居左 8. 為底端居中, 9. 為底端居右
*
* 參數$qz : 是加水印後的圖片名稱前綴
* 返回值:就是處理後圖片的名稱
*
*/
function waterMark($groundName, $waterName, $waterPos=0, $qz=”wa_”){
if(file_exists($this->path.$groundName) && file_exists($this->path.$waterName)){
$groundInfo=$this->getInfo($groundName);
$waterInfo=$this->getInfo($waterName);
//水印的位置
if(!$pos=$this->position($groundInfo, $waterInfo, $waterPos)){
echo “水印不應該比背景圖片小!”;
return;
}
$groundImg=$this->getImg($groundName, $groundInfo);
$waterImg=$this->getImg($waterName, $waterInfo);
$groundImg=$this->copyImage($groundImg, $waterImg, $pos, $waterInfo);
return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo);
}else{
echo “圖片或水印圖片不存在”;
return false;
}
}
private function copyImage($groundImg, $waterImg, $pos, $waterInfo){
imagecopy($groundImg, $waterImg, $pos[“posX”], $pos[“posY”], 0, 0, $waterInfo[“width”], $waterInfo[“height”]);
imagedestroy($waterImg);
return $groundImg;
}
private function position($groundInfo, $waterInfo, $waterPos){
//需要背景比水印圖片大
if(($groundInfo[“width”]< $waterInfo[“width”]) ||($groundInfo[“height”] < $waterInfo[“height”])){
return false;
}
switch($waterPos){
case 1:
$posX=0;
$posY=0;
break;
case 2:
$posX=($groundInfo[“width”]-$waterInfo[“width”])/2;
$posY=0;
break;
case 3:
$posX=$groundInfo[“width”]-$waterInfo[“width”];
$posY=0;
break;
case 4:
$posX=0;
$posY=($groundInfo[“height”]-$waterInfo[“height”]) /2;
break;
case 5:
$posX=($groundInfo[“width”]-$waterInfo[“width”])/2;
$posY=($groundInfo[“height”]-$waterInfo[“height”]) /2;
break;
case 6:
$posX=$groundInfo[“width”]-$waterInfo[“width”];
$posY=($groundInfo[“height”]-$waterInfo[“height”]) /2;
break;
case 7:
$posX=0;
$posY=$groundInfo[“height”]-$waterInfo[“height”];
break;
case 8:
$posX=($groundInfo[“width”]-$waterInfo[“width”])/2;
$posY=$groundInfo[“height”]-$waterInfo[“height”];
break;
case 9:
$posX=$groundInfo[“width”]-$waterInfo[“width”];
$posY=$groundInfo[“height”]-$waterInfo[“height”];
break;
case 0:
default:
$posX=rand(0, ($groundInfo[“width”]-$waterInfo[“width”]));
$posY=rand(0, ($groundInfo[“height”]-$waterInfo[“height”]));
break;
}
return array(“posX”=>$posX, “posY”=>$posY);
}
}
第三步:上傳表單
<form action=”upload.php” method=”post” enctype=”multipart/form-data”>
pic: <input type=”file” name=”pic”><br>
<input type=”submit” name=”sub” value=”uploadpicture”>
</form>
第四步:上傳處理
<?php
/*
功能:上傳圖片到指定路徑”./images/”下
並對指定路徑下的圖片做圖像處理操作!
*/
include “FileUpload.class.php”;
include “image.class.php”;
$up=new FileUpload(array(“filepath”=>”./images/”, “allowtype”=>array(“gif”, “jpg”, “png”)));
if($up->uploadFile(“pic”)){
$filename=$up->getNewFileName();
$img=new Image(“./images/”);
$th_filename=$img->thumb($filename, 300, 300, “th_”);
$img->waterMark($th_filename, “gaolf.gif”, 5, “wa_”);
$img->waterMark($filename, “gaolf.gif”, 0, “”);
}else{
echo $up->getErrorMsg();
}
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/274831.html
微信掃一掃
支付寶掃一掃