Image upload site PHP image upload Class Code

Source: Internet
Author: User
Tags explode imagejpeg
Let's start with a simple one:

Copy the Code code as follows:


Http://www.jb51.net
Class upload{
Public $length; Limit file Size
public $file; Determine if this class is for image upload or file upload
Public $fileName; Filename
Public $fileTemp; Uploading Temporary files
Public $fileSize; Upload file size
Public $error; Upload file is wrong, PhP4 no
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 for normal file, false for 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 Path
if ($path)//http://www.jb51.net
{
$createFileType = $this->_getfiletype ($this->filename);//Set file category
$createFileName =uniqid (rand ()); Randomly generated file names
$thisDir = $this->directory. $pathSign. $createFileName. ".". $createFileType;
if (@move_uploaded_file ($this->filetemp, $thisDir))//move temporary files to the specified path
{
return $thisDir;
}
}
}else{//Image upload
$path = $this->_iscreateddir ($this->directory);//Get Path
if ($path)//path exists//http://www.jb51.net
{
$createFileType = $this->_getfiletype ($this->filename);//Set 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)//upload picture 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)//path exists, does not exist to create
{
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")//documents all conform to 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= "error uploading 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= "Pictures over $this->maxlen";
Parent::showerror ();
}
if ($value [' Error ']!=0)
{
$this->errormsg= "Upload image Error";
Parent::showerror ();
}
if (! $this->_isimg ($value [' type ']))
{
$this->errormsg= "Picture is not in the right format";
Parent::showerror ();
}
}
return true;
}
}
}
?>


A more complicated PHP upload class that can automatically generate thumbnail functionality
Start the first step:
To create a folder, layout:
Annex: Attachment (the original image of the upload is stored in this directory)
|-smallimg: Storing thumbnail images
|-mark: Storing watermark Picture
Include: Store the class file, font (this program code uses: 04b_08__. TTF)
|-upfile.php: Integrated simple upload, generate thumbnail and watermark class file information
|-04b_08__. TTF: Font file
test.php: Test File
Second Step upload class
upfile.php

Copy the Code code as follows:


Class Upimages {
var $annexFolder = "Annex";//Attachment storage point, default: Annex
var $smallFolder = "smallimg";//thumbnail storage path, note: Must be placed under the $annexFolder subdirectory, default: Smallimg
var $markFolder = "Mark";//Watermark Picture Storage Place
var $upFileType = "jpg gif png";//upload type, default: JPG gif png rar zip
var $upFileMax = 1024;//upload size limit in KB, default: 1024KB
var $fontType;//font
var $maxWidth = 500; Max width of picture
var $maxHeight = 600; Maximum height of picture
function Upimages ($annexFolder, $smallFolder, $includeFolder) {
$this->annexfolder = $annexFolder;
$this->smallfolder = $smallFolder;
$this->fonttype = $includeFolder. " /04b_08__. TTF ";
}
function UpLoad ($inputName) {
$imageName = time ();//set current times as picture names
if (@empty ($_files[$inputName ["name"])) Die (Error ("Do not upload picture information, please confirm"));
$name = Explode (".", $_files[$inputName ["name"]);//upload the file before uploading to "." Get file types separately
$imgCount = count ($name);//Get the number of intercepts
$imgType = $name [$imgCount -1];//Gets the type of the file
if (Strpos ($this->upfiletype, $imgType) = = = False) die (Error ("Upload file type only". $this->upfiletype. "Not supported". $imgType));
$photo = $imageName. ".". $imgType;//write the file name of the database
$uploadFile = $this->annexfolder. " /". $photo;//file name after uploading
$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 ("Upload file exceeds". $this->upfilemax. " KB "));
}
} else {
Die (Error ("Upload image failed, please verify that your upload file does not exceed $upFileMax KB or upload time timeout"));
}
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 a 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, $height -20, $height-7, $black);
Imagettftext ($NEWIMG, 4.9, 0, $height -14, $black, $this->fonttype, $text [0]);
Imagettftext ($NEWIMG, 4.9, 0, $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;
}
}
?>


Step three: Test the upload class
test.php

Copy the Code code as follows:


$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;//settings Generate watermark image values
$img->tofile = true;
$NEWSMALLIMG = $img->smallimg ($photo);
$newMark = $img->watermark ($photo, $text);
echo "

";
echo "

";
echo "Continue uploading";
} else {
?>

}
?>

The above introduces the image upload site PHP image upload class code, including the content of the image upload site, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.