Image upload website php image upload class code

WBOY
Release: 2016-07-29 08:40:21
Original
791 people have browsed it

Let’s do a simple one first:

Copy the code The code is as follows:


//http://www.jb51.net
class upLoad{
public $length; //Limit file size
public $file; //Determine whether this class is used for image upload or file upload
public $fileName; //File name
public $fileTemp; //Upload temporary file
public $fileSize; //Upload file size
public $error ; //Whether there is an error in uploading the file, php4 does not have
public $fileType; //Upload file type
public $directory; //
public $maxLen;
public $errormsg;
function __construct($length,$file=true, $directory)
{
$this->maxLen=$length;
$this->length=$length*1024;
$this->file=$file; //true is a general file, false is a picture Judgment
$this->directory=$directory;
}
public function upLoadFile($fileField)
{
$this->fileName=$fileField['name'];
$this->fileTemp=$ fileField['tmp_name'];
$this->error=$fileField['error'];
$this->fileType=$fileField['type'];
$this->fileSize=$fileField[ 'size'];
$pathSign = DIRECTORY_SEPARATOR; // /
if($this->file) //General file upload
{
$path = $this->_isCreatedDir($this->directory); //Get the path
if($path)//http://www.jb51.net
{
$createFileType = $this->_getFileType($this->fileName);//Set the file category
$createFileName =uniqid(rand()); //Randomly generate file name
$thisDir=$this->directory.$pathSign.$createFileName.".".$createFileType;
if(@move_uploaded_file($this->fileTemp ,$thisDir)) //Move the temporary file to the specified path
{
return $thisDir;
}
}
}else{ //Upload the image
$path = $this->_isCreatedDir($this-> ;directory);//Get the path
if($path)//The path exists//http://www.jb51.net
{
$createFileType = $this->_getFileType($this->fileName); //Set the file category
$createFileName=uniqid(rand());
return @move_uploaded_file($this->fileTemp,$this->directory.$pathSign.$createFileName.".".$createFileType) ? true : false;
}
}
}
public function _isBig($length,$fsize) //Returns whether the file exceeds the specified size
{
return $fsize>$length ? true : false;
}
public function _getFileType($ fileName) //Get the suffix of the file
{
return end(explode(".",$fileName));
}
public function _isImg($fileType) //Whether the uploaded image type is allowed
{
$type=array( "jpeg","gif","jpg","bmp");
$fileType=strtolower($fileType);
$fileArray=explode("/",$fileType);
$file_type=end($fileArray) ;
return in_array($file_type,$type);//http://www.jb51.net
}
public function _isCreatedDir($path) //Whether the path exists, create it if it does not exist
{
if(!file_exists ($path))
{
return @mkdir($path,0755)?true:false; //Permission 755//
}
else
{
return true;
}
}
public function showError() // Display error message
{//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,$directory);
}
function upLoadMultiFile()
{
$arr=array();
if($this-> _judge()||$this->_judge()=="no") //If all files meet the specifications, start uploading
{
foreach($this->FILES as $key=>$value)
{
$strDir=parent::upLoadFile($this->FILES[$key]);
array_push($arr, $strDir);
}
//http://www.jb51.net
return $arr ;
}else
{
return false;
}
}
function _judge()
{
if($this->file)
{
foreach($this->FILES as $key=>$value )
{
if($this->_isBig($this->length,$value['size']))
{
$this->errormsg="File exceeds $this->maxLen K" ;
parent::showError();
}
if($value['error']=UPLOAD_ERR_NO_FILE)
{
//$this->errormsg="An error occurred while uploading the file";
//parent::showError ();
return "no";
}
}
return true;
}else
{
//http://www.jb51.net
foreach($this->FILES as $key=>$ value)
{
if($this->_isBig($this->length,$value['size']))
{
$this->errormsg="Picture exceeds $this->maxLen" ;
parent::showError();
}
if($value['error']!=0)
{
$this->errormsg="An error occurred while uploading the image";
parent::showError();
}
if(!$this->_isImg($value['type']))
{
$this->errormsg="The picture format is wrong";
parent::showError();
}
}
return true;
}
}
}
?>


Another more complicated PHP upload class that can automatically generate thumbnails
Start the first step:
Create a folder, layout:
annex: Attachment (the original uploaded image is stored in this directory)
|— smallimg: Store thumbnail images
|— mark: store watermark images
include: store class files and fonts (this program code uses: 04B_08__.TTF)
|— upfile.php: integrate simple upload, generate thumbnails and watermark classes File information
|— 04B_08__.TTF: font file
test.php: test file
Second step upload class
upfile.php

Copy code The code is as follows:


class UPImages {
var $annexFolder = "annex";//Attachment storage point, the default is: annex
var $smallFolder = "smallimg";//Thumbnail storage path, note: must be The subdirectory under $annexFolder, the default is: smallimg
var $markFolder = "mark"; //Watermark image storage location
var $upFileType = "jpg gif png"; //The upload type, the default is: jpg gif png rar zip
var $upFileMax = 1024; //Upload size limit, unit is "KB", default is: 1024KB
var $fontType; //Font
var $maxWidth = 500; //Maximum image width
var $maxHeight = 600; //Maximum height of image
function UPImages($annexFolder,$smallFolder,$includeFolder) {
$this->annexFolder = $annexFolder;
$this->smallFolder = $smallFolder;
$this->fontType = $includeFolder."/04B_08__.TTF";
}
function upLoad($inputName) {
$imageName = time();//Set the current time as the image name
if(@empty($_FILES[$inputName] ["name"])) die(error("No picture information uploaded, please confirm"));
$name = explode(".",$_FILES[$inputName]["name"]);//Will upload The previous files are separated by "." to get the file type
$imgCount = count($name);//Get the intercepted number
$imgType = $name[$imgCount-1];//Get the file type
if(strpos ($this->upFileType,$imgType) === false) die(error("The upload file type only supports ".$this->upFileType." does not support ".$imgType));
$photo = $ imageName.".".$imgType;//The file name written to the database
$uploadFile = $this->annexFolder."/".$photo;//The uploaded file name
$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("Uploaded file exceeds".$this->upFileMax."KB"));
}
} else {
die(error("Failed to upload image, please make sure your uploaded file does not exceed $upFileMax KB or the upload time has timed out"));
}
return $photo;
}
function getInfo($photo ) {
$photo = $this->annexFolder."/".$photo;
$imageInfo = getimagesize($photo);
$imgInfo["width"] = $imageInfo[0];
$imgInfo[" height"] = $imageInfo[1];
$imgInfo["type"] = $imageInfo[2];
$imgInfo["name"] = basename($photo);
return $imgInfo;
}
function smallImg ($photo,$width=128,$height=128) {
$imgInfo = $this->getInfo($photo);
$photo = $this->annexFolder."/".$photo;// Get the picture source
$newName = substr($imgInfo["name"],0,strrpos($imgInfo["name"], "."))."_thumb.jpg";//New picture name
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["height "] : $height;
$srcW = $imgInfo["width"];
$srcH = $imgInfo["height"];
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);
ImageCopyResized($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);
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["height"]) ? $imgInfo["height"] : $this->maxHeight;
$srcW = $imgInfo["width"];
$srcH = $imgInfo["height"];
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);
ImageCopyResized($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;
}
}
?>


第三步:测试上传类
test.php

复制代码 代码如下:


$annexFolder = "annex";
$smallFolder = "smallimg";
$markFolder = "mark";
$includeFolder = "include";
require("./".$includeFolder."/upfile.php");
$img = new UPImages($annexFolder,$smallFolder,$includeFolder);
$text = array("www.jb51.net","all rights reserved");
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 {
?>







}
?>

以上就介绍了图片上传网站 php 图片上传类代码,包括了图片上传网站方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template