<?php
/**
* The Upload class
* @author Alex Lee <[email protected]>
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License
* @version svn: $Id: upload.php 2014-11-2 22:12:32z [email protected] $
*/
/**
* @category Runtime
* @package Lotusphp\upload
*/
error_reporting (0); Block all error messages
Class Ltupload
{
/** @var ltconfig Config handle */
Public $configHandle;
/** @var Array Config */
Public $conf;
Public $confgroup;
GetImageSize + $_files Value
Public $getData = ';
Private $dir _sep = Directory_separator;
Private $err = Array (' fileempty ' + ' upload file is empty ', ' fileillegal ' = ' "Upload file is illegal ', ' filepostfixillegal ' = ' = ' upload file format illegal ', ' Filesizeillegal ' + ' upload file size is illegal ', ' filefailure ' = ' upload file failed ', ' filesizetoosmall ' = ' picture pixel too small ', ' filelayer ' = ' Split filename width out of range ', ' filecreatedir ' = ' Create directory without permissions ');
/**
* Construct
*/
Public Function __construct ()
{
if (! $this->confighandle instanceof Ltconfig)
{
if (class_exists ("Ltobjectutil", false))
{
$this->confighandle = Ltobjectutil::singleton ("Ltconfig");
}
Else
{
$this->confighandle = new Ltconfig;
}
}
}
/**
* Init
*/
Public Function init ()
{
$this->confgroup = $this->confighandle->get ("Upload");
if (Empty ($this->confgroup[' default '))
{
Detailed configuration information (default configuration)
Only GIF, JPEG, PNG, BMP are allowed to be uploaded by default
$this->confgroup[' default ' [' fileType '] = ' image '; Type is a picture, the parameter is file is upload only
$this->confgroup[' default ' [' cutting '] = FALSE; Do you need to cut the diagram
$this->confgroup[' default ' [' cuttype '] = 1; transduction mode, 1: Equal to scale, 2:
$this->confgroup[' default ' [' cutsize '] = Array (Array (' width ' =>0, ' height ' =>0)); Default equal scale micro cut width
$this->confgroup[' default ' [' allowtype '] = array (' gif ', ' jpeg ', ' jpg ', ' png '); Types of images that can be uploaded
$this->confgroup[' default ' [' checktype '] = TRUE; Whether to check the picture format
$this->confgroup[' default ' [' checksize '] = TRUE; Whether to check the picture size
$this->confgroup[' default ' [' maxSize '] = 2097152; Upload Maximum, per byte
$this->confgroup[' default ' [' pathformat '] = '/upload/image/{yyyy}{mm}/{dd}/{time}{rand:6} ';
$this->conf = $this->confgroup[' default '];
}
Else
{
$this->conf = $this->confgroup[' default '];
}
}
Public Function Setupload ($configName)
{
$this->confgroup[$configName]? $this->conf = $this->confgroup[$configName]: $this->conf = $this->confgroup[' default '];
}
Upload the original document
Public function put ($arr _files = Array ()) {
$this->getdata = & $arr _files;
Determines whether the empty
if (!isset ($this->getdata[' tmp_name ')) | |!isset ($this->getdata[' name ') | | empty ($this->getdata[' Tmp_name ']) | | Empty ($this->getdata[' name ')) {
$this->halt ($this->err[' fileempty ');
}
Determine if upload failed
if (isset ($this->getdata[' error ') && $this->getdata[' ERROR ']) {
$this->halt ($this->err[' filefailure ');
}
if (Is_uploaded_file ($this->getdata[' Tmp_name ')) {
$this->checksafe ();
$this->createnewpath ();
$this->createdir ();
Images or files can be uploaded
return $this->doupload ()? $this->getdata: $this->err[' filefailure ';
}
}
/**
* File Upload Start
*
* @param <string> Tmp_name
* @param <string> Path, New_path
* @return <boolean>
*/
Private Function Doupload () {
if (Move_uploaded_file ($this->getdata[' tmp_name '), $_server[' Document_root ']. Directory_separator. $this->getdata[' Full_path ')) {
return TRUE;
}
return FALSE;
}
/**
* Determine if the upload file size is legal
*
* @param <number> Size File value
* @return <boolean>
*/
Private Function Checksize () {
if ($this->conf[' checksize ') && $this->getdata[' size '] > $this->conf[' maxSize ']) {
return FALSE;
}
return TRUE;
}
/**
* Get filename suffix
*
* @param <string> Name
* @return <string> return suffix name
*/
Private Function Getpostfix () {
Return Strtolower (Trim (substr (STRRCHR ($this->getdata[' name '), '. '), 1, 10));
}
/**
* Check whether the file is a specified upload type
*
* @param <string> Name
* @return <boolean>
*/
Private Function Checkformat () {
if ($this->conf[' Checktype ') &&!in_array ($this->getpostfix (), $this->conf[' Allowtype '])) {
return FALSE;
}
return TRUE;
}
/**
* Create a separate directory
*
* @param <string> Path configuration directory, New_path full directory
*/
Private Function Createdir () {
$realpath =$_server[' Document_root '). Directory_separator. $this->getdata[' Full_path '];
if (!file_exists ($path = dirname ($realpath))) {
if ([email protected] ($path, 0777, TRUE)) {
return $this->err[' Filecreatedir '];
}
}
}
/**
* $autoDir If False, the directory segmentation function is turned off
*
* @param <string> new_name, New_dir, New_path
* @return <string> New_dir, New_path
*/
Private Function Createnewpath () {
$t = time ();
$d = Explode ('-', date ("Y-y-m-d-h-i-s"));
$format = $this->conf["Pathformat"];
$format = Str_replace ("{yyyy}", $d [0], $format);
$format = Str_replace ("{yy}", $d [1], $format);
$format = Str_replace ("{mm}", $d [2], $format);
$format = Str_replace ("{dd}", $d [3], $format);
$format = Str_replace ("{hh}", $d [4], $format);
$format = Str_replace ("{II}", $d [5], $format);
$format = Str_replace ("{SS}", $d [6], $format);
$format = Str_replace ("{Time}", $t, $format);
Replace random string
$randNum = rand (1, 10000000000). Rand (1, 10000000000);
if (Preg_match ("/\{rand\:([\d]*) \}/i", $format, $matches)) {
$format = Preg_replace ("/\{rand\:[\d]*\}/i", substr ($randNum, 0, $matches [1]), $format);
}
$this->getdata[' Full_path ']= $format. $this->getpostfix ();
}
/**
* Image Validity detection
*
* @param <string> FullPath complete full path address
* @return <string> width picture width, height picture altitude, img_type_number picture number type
*/
Private Function Checksafe () {
if (! $this->checkformat ()) {
$this->halt ($this->err[' Filepostfixillegal ');
}
if (! $this->checksize ()) {
$this->halt ($this->err[' Filesizeillegal ');
}
$this->getimage ();
}
/**
* Get the image length, width, type
*
* @param <string> FullPath complete full path address
*/
Private Function getImage ($fullPath = ") {
if ($this->conf[' fileType '] = = ' image ') {
List ($this->getdata[' width '), $this->getdata[' height '], $this->getdata[' img_type_number ']) = getimagesize ($fullPath $fullPath: $this->getdata[' tmp_name ');
if (!in_array ($this->getdata[' Img_type_number '), Array ()) {
$this->halt ($this->err[' Filepostfixillegal ');
}
}
}
/**
* Generate equal scale thumbnail thumb
*
* @return <boolean> TRUE succeeded, FALSE failed
*/
Public Function Putthumb () {
$filePath = $_server[' Document_root '). Directory_separator. $this->getdata[' Full_path '];
if ($filePath && $this->conf[' fileType ') = = ' Image ' && $this->conf[' cutting '] && @file_ Exists ($filePath)) {
if (!isset ($this->getdata[' width ') | |!isset ($this->getdata[' height ')) {
$this->getimage ($filePath);
}
if (!in_array ($this->getdata[' Img_type_number '), Array ()) {
return $this->err[' Filepostfixillegal '];
}
Switch ($this->getdata[' Img_type_number ') {
Case 1:
$im = Imagecreatefromgif ($filePath);
Break
Case 2:
$im = Imagecreatefromjpeg ($filePath);
Break
Case 3:
$im = Imagecreatefrompng ($filePath);
Break
}
if (! $im) {
return FALSE;
}
foreach ($this->conf[' cutsize ') as $img) {
$imgWidth = $img [' width '];
$imgHeight = $img [' height '];
$filename = $img [' width ']. ' -'. $img [' height '];
if ($img [' width '] > $this->getdata[' width ' && $img [' width ']>0) {$img [' width '] = $this->getdata[' Width '];}
if ($img [' height '] > $this->getdata[' height ' && $img [' height ']>0) {$img [' height ']= $this getdata[' height ';}
$width = $img [' width '];
$height = $img [' height '];
if ($img [' Width ']>0 && ($this->getdata[' width '] < $this->getdata[' height ')) {
$width = ($img [' height ']/$this->getdata[' height ']) * $this->getdata[' width ');
} else {
$height = ($img [' width ']/$this->getdata[' width ') * $this->getdata[' height '];
}
if (function_exists ("Imagecreatetruecolor")) {
if ($this->conf[' Cuttype ' ==1) {
$new = Imagecreatetruecolor ($width, $height);
$this->getdata[' img_type_number '] = = 3 && $this->transparent ($new);
Imagecopyresampled ($new, $im, 0, 0, 0, 0, $width, $height, $this->getdata[' width '), $this->getdata[' height ']);
}else{
$DST _x = 0;
$DST _y = 0;
if (($imgWidth/$imgHeight-$width/$height) > 0) {
$DST _x = ($imgWidth-$width)/2;
} elseif (($imgWidth/$imgHeight-$width/$height) < 0) {
$DST _y = ($imgHeight-$height)/2;
}
$new = Imagecreatetruecolor ($imgWidth, $imgHeight);
$color = Imagecolorallocate ($new, Hexdec (substr ($this->conf[' bgcolor '], 1, 2)), Hexdec (substr ($this->conf[' BgColor '], 3, 2), Hexdec (substr ($this->conf[' bgcolor '], 5, 2));
Imagefill ($new, 0, 0, $color);
Imagecopyresampled ($new, $im, $dst _x, $dst _y, 0, 0, $width, $height, $this->getdata[' width '), $this->getdata[' Height ']);
}
} else {
$new = Imagecreate ($width, $height);
if ($this->conf[' Cuttype ' ==1) {
$new = Imagecreatetruecolor ($width, $height);
$this->getdata[' img_type_number '] = = 3 && $this->transparent ($new);
Imagecopyresampled ($new, $im, 0, 0, 0, 0, $width, $height, $this->getdata[' width '), $this->getdata[' height ']);
}else{
$DST _x = 0;
$DST _y = 0;
if (($imgWidth/$imgHeight-$width/$height) > 0) {
$DST _x = ($imgWidth-$width)/2;
} elseif (($imgWidth/$imgHeight-$width/$height) < 0) {
$DST _y = ($imgHeight-$height)/2;
}
$new = Imagecreatetruecolor ($imgWidth, $imgHeight);
$color = Imagecolorallocate ($new, Hexdec (substr ($this->conf[' bgcolor '], 1, 2)), Hexdec (substr ($this->conf[' BgColor '], 3, 2), Hexdec (substr ($this->conf[' bgcolor '], 5, 2));
Imagefill ($new, 0, 0, $color);
Imagecopyresampled ($new, $im, $dst _x, $dst _y, 0, 0, $width, $height, $this->getdata[' width '), $this->getdata[' Height ']);
}
}
$newFilePath = $_server[' Document_root '). Directory_separator. $this->getdata[' Full_path ']. ‘.‘ . $filename;
Switch ($this->getdata[' Img_type_number ') {
Case 1:
Imagegif ($new, $newFilePath. '. gif ');
Break
Case 2:
Imagejpeg ($new, $newFilePath. '. jpg ', 100);
Break
Case 3:
Imagepng ($new, $newFilePath. '. png ');
Break
}
Unset ($filePath, $filename, $newFilePath, $width, $height);
}
Imagedestroy ($new); Imagedestroy ($im);
return TRUE;
}
return FALSE;
}
/**
* PNG Transparent background image processing
*
* @param <resource> New Resources
* @param <int> Transparent returns FALSE if the allocation fails
*/
Private function Transparent ($new) {
$transparent = Imagecolorallocatealpha ($new, 0, 0, 0, 127);
Imagealphablending ($new, false);
Imagefill ($new, 0, 0, $transparent);
Imagesavealpha ($new, true);
}
Format Error Output
Private function Halt ($message) {
Echo $message;
Exit ();
}
}
Ltupload Uploading components