ホームページ  >  記事  >  バックエンド開発  >  PHP は非常に強力で、非常にシンプルな PHP アップロードを使用します。 class_PHP チュートリアル

PHP は非常に強力で、非常にシンプルな PHP アップロードを使用します。 class_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-13 17:40:47665ブラウズ

uploading.class.php クラスをアップロードしています

class UpImages {
var $annexFolder = "upload";//添付ファイルの保存ポイント、デフォルトは: annex
var $smallFolder = "small";//サムネイルの保存パス、注意: アップロードする必要があります$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 = $annexFolder;
$this->smallFolder = $smallFolder;
$this-> fontType = $includeFolder."/04B_08__.TTF";
}
function upLoad($inputName) {
$imageName = time();//現在時刻を画像名として設定します
if(@empty($_FILES[$ inputName] ["name"])) die("画像情報がアップロードされていません。確認してください");
$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"];
$kSize =round($imgSize/1024 );
if($ kSize > ($this->upFileMax*1024)) {
@unlink($uploadFile);
die(error("アップロード ファイルが超過しています".$this->upFileMax."KB" ));
}
} else {
die(error("画像のアップロードに失敗しました。アップロードしたファイルが $upFileMax KB を超えていないか、アップロード時間がタイムアウトしていないことを確認してください"));
}
return $photo;
}
function getInfo($photo) {
$photo = $this->annexFolder."/".$photo;
$imageInfo = getimagesize($photo);
$imgInfo["width"] = $imageInfo[ 0];
$imgInfo["高さ"] = $imageInfo[1];
$imgInfo["タイプ"] = $imageInfo[2];
$imgInfo["名前"] = ベース名($photo);
return $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)) return False;
$width = ( $width > $imgInfo["width"]) $imgInfo["width"] : $width;
$height = ($height > $imgInfo["height "]) ? $imgInfo["高さ"] : $高さ;
$srcW = $imgInfo["幅"];
$srcH = $imgInfo["高さ"];
if ($srcW * $幅 > $ 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 ["幅"], $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 ($newImg);
ImageDestroy($img);
return $newName;
}
function WaterMark($photo,$text) {
$imgInfo = $this-> ;getInfo($photo);
$photo = $ this->annexFolder."/".$photo;
$newName = substr($imgInfo["name"], 0, strrpos($imgInfo["name"] , ".")) . "_mark.jpg" ;
switch ($imgInfo["type"]) {
case 1:
$img = imagecreatefromgif($photo);
break;
case 2:
$img = imagecreatefromjpeg($photo);
break;
case 3 :
$img = imagecreatefrompng($photo);
break;
default:
return False;
}
if (empty($img)) return False;
$width = ($this->maxWidth > $imgInfo ["width"]) $imgInfo["width"] : $this->maxWidth;
$height = ($this->maxHeight > $imgInfo[ "高さ"]) $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);
return $newName;
}
}
?>


开始调用(使用方法

include uploading.class.php;
$max="upload"; //文件上传路径
$mix="small"; //缩略图路径(必须在アップロード下建立)
$mark="mark"; //加水引の写真保存放路
$text = array("oktang","2012"); //水印内容
$img= new UpImages($max,$mix,$max); //实例化类文件
$photo = $img->upLoad("file"); // 上の文書フィールド
$img->maxWidth = $img->maxHeight = 600; //設定高、和宽
$img->toFile = true;
$newSmallImg = $img->smallImg($photo);
$newMark = $img->waterMark($photo,$text);
echo $newSmallImg;
echo $newMark;
echo "

";
echo "

";

php は非常に単一の PHP 上で転送されるクラスを使用しており、大家を助けることができれば幸いです。

http://www.bkjia.com/PHPjc/486186.html

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/486186.html技術記事アップロード .class.php 上传类 ?php class UpImages { var $annexFolder = "upload";//付属品存放点,默认:annex var $smallFolder = "small";//缩略图存放路径,注:必须.. .
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。