PHP file Pictures bulk upload program code

Source: Internet
Author: User
Tags explode file size html form imagecopy imagejpeg php file rand require

Description
UPLOADX PHP Bulk Upload components Follow the Open source Protocol (GPL), any individual, organization can be free to use this program, two development and other powers.
It will also be stated that I am not responsible for the business interests and losses caused by your personal or organizational use of this procedure, but please keep the copyright information.
We also welcome the proposal and advice of UPLOADX.
This procedure uses the PHP program to write, can more efficient batch processing PHP development The file uploads, the picture processing, the batch adds the picture watermark and so on question, before using this procedure please read the use instruction in detail!

HTML Form page

The code is as follows Copy Code

<form enctype= "Multipart/form-data" action= "uploadx.php" method= "POST" >
<input name= "uploadx[]" type= "file" >
<input name= "uploadx[]" type= "file" >
<input type= "Submit" value= "Upload" >
</form>

uploadx.php Processing page

Require ('./classes/uploadx.class.php ');

$UPLOADX = new Uploadx;

$UPLOADX->save = './temp ';
$UPLOADX->name = ' auto ';
$UPLOADX->mini = ' 200,200,mini ';
$UPLOADX->mark = './images/logo.png,0,60 ';
Print_r ($UPLOADX->is ());

Parameter description:

$UPLOADX->save
Save directory for uploaded files
$UPLOADX->name;
How to name an uploaded file
Parameter description
auto= automatic random file name,
Null= the original filename (overwrite),
Other customizations such as time: Date (' ymdhis ');
$UPLOADX->mini;
Generate thumbnail parameters
width = produces the thumbnail width.
Height = the height of the thumbnail is generated.
Mini = Generate thumbnail file suffix add-on name
Default blank does not generate thumbnails;
$UPLOADX->mark;
Upload picture file add watermark parameter:
Watermark file, watermark location, watermark transparency

Location parameter Description:
0 = random;
1 = upper left corner;
2 = top center;
3 = upper right corner;
4 = Left center;
5 = Middle Center;
6 = right Center;
7 = lower left corner;
8 = Bottom Center;
9 = lower right corner;

$UPLOADX->is (TRUE);
Returns an array of uploaded files
is parameter description:
true = return all uploaded files
False = Filter failed files
$UPLOADX->is ();
Returns the data array format description:
Name = file name that has been saved after uploading
Mini = Name of the thumbnail generated
Mark = Add Watermark State, 1 indicates success or failure
Code = Error Codes: 0 means upload succeeded
Error = Error message: Errors prompted when uploading errors
The generation of thumbnails and add watermark function can also be used alone!

The above is a simple introduction to uploadx2.0,
Also welcome you to be able to use the process of improvement supplement

Example

The code is as follows Copy Code

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<TITLE>UPLOADX demo</title>

<body>
<form enctype= "Multipart/form-data" action= "uploadx.php" method= "POST" >
<input name= "uploadx[]" type= "file" > <br/>
<input name= "uploadx[]" type= "file" > <br/>
<input name= "uploadx[]" type= "file" > <br/>
<input name= "uploadx[]" type= "file" > <br/>
<br/>
<input type= "Submit" value= "Upload selected Files" >
</form>
</body>

<?php

Require ('./classes/uploadx.class.php ');

$UPLOADX = new Uploadx;

$UPLOADX->save = './temp ';
$UPLOADX->name = ' auto ';
$UPLOADX->mini = ' 200,200,mini ';
$UPLOADX->mark = './images/logo.png,0,60 ';
Print_r ($uploadx->mini ('./temp/2.jpg '));


uploadx.class.php

<?php

Class UPLOADX {
var $form = ' UPLOADX ';
var $save = './';
var $size = ' 1024 ';
var $type = ' gif,bmp,png,jpg,jpeg,swf,flv,mp3,wma,rar,zip,7z,doc,docx,ppt,pptx,xls,xlsx,txt,pdf ';
var $name = null;
var $mini = null;
var $mark = null;
var $version = ' 2.0 ';

The public function is ($type = True) {
foreach ($this->files () as $key => $val) {
$file = $mini = null;
$file = $this->saves ($val [' name '], $val [' type '], $val [' Tmp_name '], $val [' size '], $val [' Error ']];
$file [' Code '] | | $file [' path '] = RTrim ($this->save, '/'). ' /'. $file [' name '];
$file [' Code '] | | $file [' mini '] = $this->mini ($file [' path ']);
$file [' Code '] | | $file [' mark '] = $this->mark ($file [' path ']);
$file [' Code '] && $file [' error '] = $this->error ($file [' Code ']);
$type? $files [] = $file: ($file [' Code '] | | | $files [] = $file);
}
return Isset ($files)? $files: Array ();
}
Private function files () {
if (count ($_files[$this->form]) <1) return array ();
if (Is_array ($_files[$this->form][' name ')) {
For ($i =0 $i <count ($_files[$this->form][' name '); $i + +) {
if ($_files[$this->form][' error '] [$i]==4) continue;
$files [] = Array (
' Name ' =>$_files[$this->form][' name ' [$i],
' Type ' =>$_files[$this->form][' type ' [$i],
' Tmp_name ' =>$_files[$this->form][' tmp_name ' [$i],
' Error ' =>$_files[$this->form][' ERROR ' [$i],
' Size ' =>$_files[$this->form][' size '] [$i]);
}
}else{
$files [] = $_files[$this->form];
}
return $files;
}
Private function saves ($name, $type, $temp, $size, $error) {
if ($error) return Array (' name ' => $name, ' code ' => $error);
$prefix = Strtolower (PathInfo ($name, pathinfo_extension));
if (!in_array ($prefix, Explode (', ', Strtolower ($this->type))) {
Return Array (' name ' => $name, ' Code ' => '-1 ');
}
if ($size/1024> $this->size) {
Return Array (' name ' => $name, ' Code ' => '-2 ');
}
if (!is_dir ($this->save)) {
if (!mkdir ($this->save, 0777, TRUE)) {
Return Array (' name ' => $name, ' Code ' => '-3 ');
}
}
$filename = $this->name? ($this->name== ' auto ' uniqid (): $this->name): Trim (basename ($name, $prefix), '. ');
$savefile = Trim ($this->save, '/'). ' /'. $filename. '. ' $prefix;
if (! @move_uploaded_file ($temp, $savefile)) {
Return Array (' name ' => $name, ' Code ' => '-4 ');
}
@chmod ($savefile, 0777);
Return Array (' name ' => $filename. '. ') $prefix, ' code ' =>0);
}
Public Function Mini ($file = null) {
if (! $file | |! $this->mini) return false;
if (!is_file ($file)) return $this->error ( -5, $file);
List ($width, $height, $extends) = Explode (', ', $this->mini);
$types = Array (' gif ', ' png ', ' jpg ', ' jpeg ');
$type = PathInfo ($file, pathinfo_extension);
if (!in_array ($type, $types)) return $this->error (-6);
if (!is_file ($file)) return $this->error ( -5, $file);
$mini = $extends? BaseName ($file, $type). $extends. $type: Trim (basename ($file), '. ');
$image = imagecreatefromstring (file_get_contents ($file));
$imagex = Imagesx ($image);
$imagey = Imagesy ($image);
$scale = $width/$imagex;
if ($width > $imagex) {
$mini _width = $imagex;
$mini _height = $imagey;
}else{
$mini _width = $width;
$mini _height = Round ($scale * $imagey);
}
if (function_exists (' Imagecreatetruecolor ') && function_exists (' imagecopyresampled ')) {
$temp = Imagecreatetruecolor ($mini _width, $mini _height);
Imagecopyresampled ($temp, $image, 0,0,0,0, $mini _width, $mini _height, $imagex, $imagey);
}else{
$temp = Imagecreate ($mini _width, $mini _height);
Imagecopyresized ($temp, $image, 0,0,0,0, $mini _width, $mini _height, $imagex, $imagey);
}
Imagejpeg ($temp, RTrim ($this->save, '/'). ' /'. $mini, 100);
Imagedestroy ($temp);
Imagedestroy ($image);

Return Is_file (RTrim ($this->save, '/'). ' /'. $mini)? $mini: false;
}
Public Function mark ($file = null) {
if (! $file | |! $this->mark) return false;
List ($watermark, $position, $opacity) = Explode (', ', $this->mark);
if (!is_file ($file) | | |!is_file ($watermark)) return $this->error ( -5, ' file= '. $file. ' | | Watermark= '. $watermark);
$type = PathInfo ($file, pathinfo_extension);
$types = Array (' gif ', ' png ', ' jpg ', ' jpeg ');
if (!in_array ($type, $types)) return $this->error ( -6, $file);
$opacity = min ($opacity, 100);
$file _data = imagecreatefromstring (file_get_contents ($file));
$file _width = imagesx ($file _data);
$file _height = Imagesy ($file _data);
if (In_array (PathInfo ($watermark, pathinfo_extension), array (' GIF ', ' png ')) {
$mark _data = imagecreatefromstring (file_get_contents ($watermark));
$mark _width = imagesx ($mark _data);
$mark _height = Imagesy ($mark _data);
Switch ($position) {
Case 1: $x = 5; $y = 5; Break
Case 2: $x = ($file _width-$mark _width)/2; $y = $mark _height; Break
Case 3: $x = ($file _width-$mark _width)-5; $y = $mark _height; Break
Case 4: $x = 5; $y = ($file _height-$mark _height)/2; Break
Case 5: $x = ($file _width-$mark _width)/2; $y = ($file _height-$mark _height)/2; Break
Case 6: $x = ($file _width-$mark _width)-5; $y = ($file _height-$mark _height)/2; Break
Case 7: $x = 5; $y = ($file _height-$mark _height)-5; Break
Case 8: $x = ($file _width-$mark _width)/2; $y = ($file _height-$mark _height)-5; Break
Case 9: $x = ($file _width-$mark _width)-5; $y = ($file _height-$mark _height)-5; Break
Default: $x = rand (0, ($file _width-$mark _width)); $y = rand (0, $file _height-$mark _height));
}
$temp = Imagecreatetruecolor ($mark _width, $mark _height);
Imagecopy ($temp, $file _data, 0, 0, $x, $y, $mark _width, $mark _height);
Imagecopy ($temp, $mark _data, 0, 0, 0, 0, $mark _width, $mark _height);
Imagecopymerge ($file _data, $temp, $x, $y, 0, 0, $mark _width, $mark _height, $opacity);
Imagejpeg ($file _data, $file, 100);
Imagedestroy ($temp);
Imagedestroy ($file _data);
Imagedestroy ($mark _data);
return true;
}else{
return $this->error ( -6, $watermark);
} www.111cn.net
}
Private Function error ($code = 0, $extends = ') {
if ($code) {
Switch ($code) {
Case 6: $error = ' Write temporary folder failed '; Break
Case 5: $error = ' Write system Temp folder error '; Break
Case 4: $error = ' No files are uploaded please check the form '; Break
Case 3: $error = ' Error uploading on file is incomplete '; Break
Case 2: $error = ' file size exceeds the form limit '; Break
Case 1: $error = ' file size exceeds system limit '; Break
Case-1: $error = ' upload file type is illegal '; Break
Case-2: $error = ' upload file size exceeds background limit '; Break
Case-3: $error = ' failed to create file save path '; Break
Case-4: $error = ' Save file failed please check path '; Break
Case-5: $error = ' Error reading file '; Break
Case-6: $error = ' does not support this operation '; Break
Default: $error = ' unknown error ';
}
Return ' ['. $code. ']: '. $error $extends;
}else{
return false;
}
}

}

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.