検索
ホームページphp教程php手册PHP画像アップロードクラスコード

PHP 画像アップロード コードは比較的充実した機能を備えており、ニーズに応じて選択できます。 簡単なことから始めましょう:


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

これは、サムネイルを自動的に生成できる別のより複雑な PHP アップロード クラスです
最初の一歩を踏み出しましょう:
フォルダー、レイアウトの作成:
付録: 添付ファイル (アップロードされた元の画像はこのディレクトリに保存されます)
— smallimg: サムネイル画像を保存
— マーク: 透かし画像を保存
include: クラス ファイルとフォントを格納します (このプログラム コードでは 04B_08__.TTF を使用します)
— upfile.php: シンプルなアップロードを統合し、サムネイルと透かし入りのファイル情報を生成します
— 04B_08__.TTF: フォント ファイル
test.php: テストファイル

2 番目のステップはクラスをアップロードすることです

upfile.php


クラス 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 $maxWidth = 500 //画像の最大幅
var $maxHeight = 600 //画像の最大高さ
function UPImages($annexFolder,$smallFolder,$includeFolder) {
$this->annexFolder = $annexFolder
$this->smallFolder = $smallFolder
$this->fontType = $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]["サイズ"]
$kSize = ラウンド($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); $imgInfo["幅"] = $imageInfo[0]; $imgInfo["高さ"] = $imageInfo[1]
; $imgInfo["タイプ"] = $imageInfo[2]; $imgInfo["名前"] = ベース名($photo); $imgInfo を返します
}
function smallImg($photo,$width=128,$height=128) {
$imgInfo = $this->getInfo($photo); $photo = $this->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); } else {
$img = ""; }
if(empty($img)) は False を返します。 $width = ($width > $imgInfo["width"]) $imgInfo["width"] : $width; $height = ($height > $imgInfo["height"]) : $height; $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"]); }
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($newImg); ImageDestroy($img); $newName を返します。 }
function WaterMark($photo,$text) {
$imgInfo = $this->getInfo($photo);$photo = $this->annexFolder."/".$photo;
$newName = substr($imgInfo["name"], 0, strrpos($imgInfo["name"], ".")) 。 "_マーク.jpg";
switch ($imgInfo["type"]) {
ケース 1:
$img = imagecreatefromgif($photo);
壊す;
ケース 2:
$img = imagecreatefromjpeg($photo);
壊す;
ケース 3:
$img = imagecreatefrompng($photo);
壊す;
デフォルト:
False を返します。
}
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->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 = "別館";
$smallFolder = "smallimg";
$markFolder = "マーク";
$includeFolder = "インクルード";
require("./".$includeFolder."/upfile.php");
$img = 新しい UPImages($annexFolder,$smallFolder,$includeFolder);
$text = array("www.jzxue.com","全著作権所有");
if(@$_GET["go"]) {
$photo = $img->upLoad("アップファイル");
$img->maxWidth = $img->maxHeight = 350;//設置生成水印图像值
$img->toFile = true;
$newSmallImg = $img->smallImg($photo);
$newMark = $img->waterMark($photo,$text);
echo "PHP画像アップロードクラスコード

";
echo "PHP画像アップロードクラスコード

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






フォーム>
}
?>
 



声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

SecLists

SecLists

SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。