ホームページ  >  記事  >  バックエンド開発  >  PHP 画像アップロード クラス コード_PHP チュートリアル

PHP 画像アップロード クラス コード_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-21 15:45:40780ブラウズ

まずは簡単なコードを実行しましょう:

コードをコピーします コードは次のとおりです:

//http://www.jb51.net
class upLoad{
public $length / //ファイルサイズを制限する
public $file; //このクラスが画像のアップロードに使用されるか、ファイルのアップロードに使用されるかを決定します
public $fileName; //一時ファイルをアップロードします
public $fileSize;ファイルサイズ
public $error ; //ファイルのアップロードにエラーがあるかどうか、php4 には
public $fileType; //
public $maxLen;
関数 __construct($length,$file=true, $directory)
{
$this->maxLen=$length;
$this->file=$ file; // true は一般ファイル、false は画像 判定
$this->directory=$directory;
}
public function upLoadFile($fileField)
{
$this->fileName=$fileField['名前'];
$this->fileTemp=$ fileField['tmp_name'];
$this->fileType=$fileField['type' ];
$this->fileSize=$fileField[ 'size'];
$pathSign = DIRECTORY_SEPARATOR; // /
if($this->file) //一般的なファイルアップロード
{
$path = $this ->_isCreatedDir($this->directory); //パスを取得
if($path)//http://www.jb51.net
{
$createFileType = $this->_getFileType($this ->fileName);//ファイル カテゴリを設定します
$createFileName =uniqid(rand()); //ファイル名をランダムに生成します
$thisDir=$this->directory.$pathSign.$createFileName."."。 $createFileType;
if(@move_uploaded_file($this->fileTemp ,$thisDir)) //一時ファイルを指定されたパスに移動します
{
return $thisDir}
}
}else{ //画像をアップロードします
$path = $this->_isCreatedDir($this-> ;directory);//パスを取得します
if($path)//パスは存在します//http://www.jb51.net
{
$ createFileType = $this->_getFileType($this->fileName); // ファイル カテゴリを設定します
$createFileName=uniqid(rand());
return @move_uploaded_file($this->fileTemp,$this->) ;directory.$pathSign.$createFileName.".".$createFileType) ? true : false;
}
}
}
public function _isBig($length,$fsize) //ファイルが指定されたサイズを超えているかどうかを返します
{
return $fsize>$length ? true : false;
}
public function _getFileType($ fileName) //ファイルのサフィックスを取得します
{
return end(".",$fileName)
; public function _isImg($fileType) //アップロードされた画像タイプが許可されるかどうか
{
$type=array( "jpeg","gif","jpg","bmp"); ;
$fileArray=explode("/",$fileType);
$file_type=end($fileArray);//http://www.jb51.net
public function _isCreatedDir($path) //パスが存在するかどうか、存在しない場合は作成します
{
if(!file_exists ($path))
{
return @mkdir($path,0755)?true:false; //許可 755//
}
else
{
return true;
}
}
public function showError() // エラーメッセージを表示
{//http://www.jb51.net
echo "n";
exit();
}
}
class multiUpLoad extends upLoad {
public $FILES;
function __construct($arrayFiles,$length,$file=true,$directory)
{
$ this->FILES=$arrayFiles;
parent::__construct($length,$file,$ディレクトリ);
}
function upLoadMultiFile()
{
$arr=array();
if($this-> _judge()||$this->_judge()=="no") //Ifすべてのファイルが仕様を満たしているので、アップロードを開始します
{
foreach($this->FILES as $key=>$value)
{
$strDir=parent::upLoadFile($this->FILES[$key]) ;
array_push($arr, $strDir);
}
//http://www.jb51.net
return $arr ;
}else
}
関数 _judge()
{
if($this->file)
{
foreach($this->FILES as $key=>$value )
{
if($this->>_isBig($this->length,$ value['size']))
{
$this->errormsg="ファイルが $this->maxLen K を超えています"
parent::showError();
if($value['error'] =UPLOAD_ERR_NO_FILE)
{
//$this->errormsg="ファイルのアップロード中にエラーが発生しました";
return "no";
}else
{
//http://www.jb51.net
foreach($this->FILES as $key=>$ value)
{
if($this->_isBig($this-> ;length,$value['size']))
{
$this->errormsg="画像が $this->maxLen を超えています" ;
parent::showError();
if($value[' error']!=0)
{
$this->errormsg="画像のアップロード中にエラーが発生しました"
parent::showError();
if(!$this->_isImg($value) ['type']))
{
$this->errormsg="画像の形式が間違っています";
parent::showError();
}
}
return true;
}
?> ;


サムネイルを自動的に生成できる、別のより複雑な PHP アップロード クラス
最初のステップを開始します:
フォルダー、レイアウトを作成します:
付録: 添付ファイル (アップロードされた元の画像はこのディレクトリに保存されます)
|— smallimg: サムネイル画像を保存します
| — マーク: ウォーターマーク画像を保存します
インクルード: クラス ファイルとフォントを保存します (このプログラム コードでは 04B_08__.TTF を使用します)
|— upfile.php: 単純なアップロードを統合し、サムネイルとウォーターマーク クラスを生成します ファイル情報
|— 04B_08__.TTF: フォントfile
test.php: テストファイル
第二ステップアップロードクラス
upfile.php
コードをコピー コードは次のとおりです:

class UPImages {
var $annexFolder = "annex";//添付ファイルのストレージ ポイント、デフォルトは次のとおりです: annex
var $smallFolder = "smallimg";//サムネイルのストレージ パス、注意: でなければなりません$annexFolder の下のサブディレクトリ、デフォルトは次のとおりです: smallimg
var $markFolder = "mark"; //ウォーターマーク画像の保存場所
var $upFileType = "jpg gif png" //アップロードの種類、デフォルトは jpg gif png rar zip
var $upFileMax = 1024; //アップロード サイズの制限、単位は「KB」、デフォルトは 1024KB
var $fontType; //画像の最大幅
var $maxHeight = 600; ; //画像の最大高さ
function UPImages($annexFolder,$smallFolder,$includeFolder) {
$this->annexFolder = $smallFolder; = $includeFolder."/04B_08__.TTF";
}
function upLoad($inputName) {
$imageName = time();// 現在の時間を画像名として設定します
if(@empty($_FILES[$inputName ] ["name"])) die(error("画像情報がアップロードされていません。確認してください"));
$name =explode(".",$_FILES[$inputName]["name"]);//アップロード 前のファイルを「.」で区切ってファイルタイプを取得します
$imgCount = count($name);//インターセプトされた番号を取得します
$imgType = $name[$imgCount-1];//ファイルタイプを取得します
if(strpos ($this->upFileType,$imgType) === false) die(error("アップロード ファイル タイプは ".$this->upFileType のみをサポートします。" は ".$imgType" をサポートしません)) ;
$photo = $imageName.".".$imgType;//データベースに書き込まれるファイル名
$uploadFile = $this->annexFolder."/".$photo;//アップロードされたファイル名
$ upFileok = move_uploaded_file($_FILES [$inputName]["tmp_name"],$uploadFile);
if($upFileok) {
$imgSize = $_FILES[$inputName]["size"]; imgSize/1024);
if($kSize > ($this->upFileMax*1024)) {
@unlink($uploadFile);
die(error("アップロードされたファイルは超過しています".$this->upFileMax. "KB"));
}
} else {
die(error("画像のアップロードに失敗しました。アップロードしたファイルが $upFileMax KB を超えていないか、アップロード時間がタイムアウトしていないことを確認してください")); $photo;
}
function getInfo($photo ) {
$photo = $this->annexFolder."/".$photo;
$imageInfo = getimagesize($photo); $imageInfo[0];
$imgInfo["高さ"] = $imageInfo[1];
$imgInfo["名前"] = ベース名($photo) ;
$imgInfo を返す
}
関数 smallImg ($photo,$width=128,$height=128) {
$imgInfo = $this->getInfo($photo); annexFolder."/".$photo;// 画像ソースを取得
$newName = substr($imgInfo["name"],0,strrpos($imgInfo["name"], "."))."_thumb. jpg";//新しい画像名
if($ imgInfo["type"] == 1) {
$img = imagecreatefromgif($photo);
} elseif($imgInfo["type"] == 2) {
$img = imagecreatefromjpeg($photo);
} elseif ($imgInfo["type"] == 3) {
$img = imagecreatefrompng($photo);
$img = ""
if (空($img)) false;
$ width = ($width > $imgInfo["width"]) : $width = ($height > $imgInfo) ["高さ"]) $imgInfo["高さ"] : $高さ;
$srcH = $imgInfo["高さ"]; > $srcH * $height) {
$height =round($srcH * $width / $srcW); else {
$width =round($srcW * $height / $srcH); function_exists("imagecreatetruecolor")) {
$newImg = imagecreatetruecolor($width, $height)
ImageCopyResampled($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width" ], $imgInfo["height"]);
} else {
$newImg = imagecreate($width, $height)
ImageCopyResize($newImg, $img, 0, 0, 0, 0, $width, $height; , $imgInfo["幅"], $imgInfo["高さ"]);
}
if ($this->toFile) {
if (file_exists($this->annexFolder."/".$this- >smallFolder."/".$newName)) @unlink($this->annexFolder."/".$this->smallFolder."/".$newName)
ImageJPEG($newImg,$this-); >annexFolder."/".$this->smallFolder."/".$newName);
return $this->annexFolder."/".$this->smallFolder."/".$newName;
} else {
ImageJPEG($newImg);
ImageDestroy($img);
$newName を返す
}
$imgInfo = $ this->getInfo($photo);
$ photo = $this->annexFolder."/".$photo;
$newName = substr($imgInfo["name"], 0, strrpos($imgInfo[" name"], ".")) . "_mark .jpg";
switch ($imgInfo["type"]) {
ケース 1:
$img = imagecreatefromgif($photo);
break;
ケース 2:
$img = imagecreatefromjpeg($photo);
break;
$img = imagecreatefrompng($photo);
デフォルト:
Return;
if (empty($img)) は False を返します。
$width = ($this->maxWidth>$imgInfo["width"]) ? $imgInfo["幅"] : $this->maxWidth;
$height = ($this->maxHeight>$imgInfo["height"]) ? $imgInfo["高さ"] : $this->maxHeight;
$srcW = $imgInfo["幅"];
$srcH = $imgInfo["高さ"];
if ($srcW * $width > $srcH * $height) {
$height =round($srcH * $width / $srcW);
} else {
$width =round($srcW * $height / $srcH);
}
if (function_exists("imagecreatetruecolor")) {
$newImg = imagecreatetruecolor($width, $height);
ImageCopyResampled($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);
} else {
$newImg = imagecreate($width, $height);
ImageCopyResize($newImg, $img, 0, 0, 0, 0, $width, $height, $imgInfo["width"], $imgInfo["height"]);
}
$white = imageColorAllocate($newImg, 255, 255, 255);
$black = imageColorAllocate($newImg, 0, 0, 0);
$alpha = imageColorAllocateAlpha($newImg, 230, 230, 230, 40);
ImageFilledRectangle($newImg, 0, $height-26, $width, $height, $alpha);
ImageFilledRectangle($newImg, 13, $height-20, 15, $height-7, $black);
ImageTTFText($newImg, 4.9, 0, 20, $height-14, $black, $this->fontType, $text[0]);
ImageTTFText($newImg, 4.9, 0, 20, $height-6, $black, $this->fontType, $text[1]);
if($this->toFile) {
if (file_exists($this->annexFolder."/".$this->markFolder."/".$newName)) @unlink($this->gt; annexFolder."/".$this->markFolder."/".$newName);
ImageJPEG($newImg,$this->annexFolder."/".$this->markFolder."/".$newName);
return $this->annexFolder."/".$this->markFolder."/".$newName;
} else {
ImageJPEG($newImg);
}
ImageDestroy($newImg);
ImageDestroy($img);
$newName を返します;
}
}
?>

第三步:测试上传类
test.php
复制代码代码如下:

$annexFolder = "annex";
$smallFolder = "smallimg";
$markFolder = "マーク";
$includeFolder = "インクルード";
require("./".$includeFolder."/upfile.php");
$img = 新しい UPImages($annexFolder,$smallFolder,$includeFolder);
$text = array("www.jb51.net","無断複写・転載を禁じます");
if(@$_GET["go"]) {
$photo = $img->upLoad("upfile");
$img->maxWidth = $img->maxHeight = 350;//設置生成水印画像值
$img->toFile = true;
$newSmallImg = $img->smallImg($photo);
$newMark = $img->waterMark($photo,$text);
echo "

";
echo "

";
echo "继续上传";
} else {
?>






}
?>

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/320288.html技術記事先来一单: 复制代码 代码次のとおり: ? //http://www.jb51.net class upLoad{ public $length; // 限定文件大 public $file; // この種は画像上での送信用に使用されていると判断します...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。