ホームページ  >  記事  >  バックエンド開発  >  超簡単なPHP画像アップロードクラス(ランダム名、サムネイル、ウォーターマーク)_PHPチュートリアル

超簡単なPHP画像アップロードクラス(ランダム名、サムネイル、ウォーターマーク)_PHPチュートリアル

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

Upimages.class.php 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->smallFolder = $smallFolder; 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 を超えていないか、アップロード時間がタイムアウトしていないことを確認してください"));
}
function getInfo($photo) {
$photo = $this->annexFolder."/".$photo;
$imageInfo = getimagesize($photo);
$imgInfo["width"] = $imageInfo[ 0];
$imgInfo["高さ"] = $imageInfo[1];
$imgInfo["名前"] =
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);
$img = "";
if( $img)) false を返します;
$width = ( $width > $imgInfo["width"]) : $width = ($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."/".$newName); {
ImageJPEG($newImg);
ImageDestroy($newImg);
$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);
ケース 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 を返します;
}
}
?>

使用方法
复制代码代码如下:

include 'Upimages.class.php';
$max="アップロード"; //文件上传路径
$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);
エコー $newSmallImg;
$newMark をエコー;
echo "

";
echo "

";

注意ここには文字ファイルがあり、大家はネットワークから上下にダウンロードできます。

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