This example describes the file upload class that PHP can generate thumbnails and its usage. Share to everyone for your reference. The implementation methods are as follows:
The class file invocation method is as follows:
Copy Code code as follows:
<?php
if ($_get[' action '] = = ' Save ') {
$up = new Upload ();
$up->set_dir (DirName (__file__). /upload/', ' {y}/{m} ');
$up->set_thumb (100,80);
$up->set_watermark (DirName (__file__). /jblog/images/watermark.png ', 6,90);
$fs = $up->execute ();
Var_dump ($FS);
}
?>
<html>
<head><title>test</title></head>
<body style= "margin:0;padding:0" >
<form name= "Upload" method= "post" action= "Action=save" enctype= "Multipart/form-data" style= "margin:0" >
<input type= "File" Name= "attach[]"/>
<input type= "File" Name= "attach[]"/>
<input type= "Submit" name= "submit" value= "Upload"/>
</form>
</body>
</html>
Upload upload class files are as follows:
Copy Code code as follows:
Class Upload {
var $dir; Attachment Storage Physical Directory
var $time; Custom File Upload Time
var $allow _types; Allow upload attachment type
var $field; Upload Control Name
var $maxsize; Maximum allowable file size in KB
var $thumb _width; Thumbnail width
var $thumb _height; Thumbnail height
var $watermark _file; Watermark Picture Address
var $watermark _pos; Watermark Location
var $watermark _trans;//Watermark Transparency
Constructors
$types: File types allowed to upload, $maxsize: Allowed size, $field: Upload control name, $time: Custom upload Time
function upload ($types = ' jpg|png ', $maxsize = 1024, $field = ' attach ', $time = ') {
$this->allow_types = explode (' | ', $types);
$this->maxsize = $maxsize * 1024;
$this->field = $field;
$this->time = $time? $time: Time ();
}
Set up and create a directory where files are stored specifically
$basedir: Base directory, must be a physical path
$filedir: Custom subdirectory, available parameters {Y}, {m}, {d}
function Set_dir ($basedir, $filedir = ' ") {
$dir = $basedir;
!is_dir ($dir) && @mkdir ($dir, 0777);
if (!emptyempty ($filedir)) {
$filedir = str_replace (Array (' {y} ', ' {m} ', ' {d} '), Array (date (' Y ', $this->time), date (' m ', $this->time), date (' d ' , $this->time)), Strtolower ($filedir));
$dirs = explode ('/', $filedir);
foreach ($dirs as $d) {
!emptyempty ($d) && $dir. = $d. '/';
!is_dir ($dir) && @mkdir ($dir, 0777);
}
}
$this->dir = $dir;
}
Picture thumbnail settings, not set if you don't generate thumbnails
$width: Thumbnail width, $height: thumbnail height
function Set_thumb ($width = 0, $height = 0) {
$this->thumb_width = $width;
$this->thumb_height = $height;
}
Picture watermark settings, if not generated add watermark is not set
$file: Watermark picture, $pos: Watermark location, $trans: Watermark Transparency
function Set_watermark ($file, $pos = 6, $trans = 80) {
$this->watermark_file = $file;
$this->watermark_pos = $pos;
$this->watermark_trans = $trans;
}
/*----------------------------------------------------------------
Execute file Upload, finish processing returns an array of file information that contains uploaded successes or failures,
Where: Name is the filename, upload success is uploaded to the server file name, upload failure is the local file name
Dir holds the physical path for the attachment on the server, and the upload fails without the value
Size is an attachment, upload failure does not exist this value
Flag is Status ID, 1 indicates success,-1 indicates file type is not allowed,-2 indicates file size exceeds
-----------------------------------------------------------------*/
function Execute () {
$files = Array (); Successfully uploaded file information
$field = $this->field;
$keys = Array_keys ($_files[$field] [' name ']);
foreach ($keys as $key) {
if (!$_files[$field] [' name '] [$key]) continue;
$fileext = $this->fileext ($_files[$field [' name '] [$key]); Get file name extension
$filename = Date (' Ymdhis ', $this->time). Mt_rand (10,99). $fileext; Generate file name
$filedir = $this->dir; Attachment Actual Storage Directory
$filesize = $_files[$field] [' size '] [$key]; File size
File type not allowed
if (!in_array ($fileext, $this->allow_types)) {
$files [$key] [' name '] = $_files[$field] [' name '] [$key];
$files [$key] [' flag '] =-1;
Continue
}
File size exceeds
if ($filesize > $this->maxsize) {
$files [$key] [' name '] = $_files[$field] [' name '] [$key];
$files [$key] [' name '] = $filesize;
$files [$key] [' flag '] =-2;
Continue
}
$files [$key] [' name '] = $filename;
$files [$key] [' dir '] = $filedir;
$files [$key] [' size '] = $filesize;
Save uploaded files and delete temporary files
if (Is_uploaded_file ($_files[$field] [' tmp_name '] [$key])) {
Move_uploaded_file ($_files[$field] [' tmp_name '] [$key], $filedir. $filename);
@unlink ($_files[$field] [' tmp_name '] [$key]);
$files [$key] [' flag '] = 1;
Watermark and generate thumbnails for a picture
if (In_array ($fileext, array (' jpg ', ' png ')) {
if ($this->thumb_width) {
if ($this->create_thumb ($filedir. $filename, $filedir. ' Thumb_ '. $filename)) {
$files [$key] [' thumb '] = ' thumb_ '. $filename; Thumbnail file name
}
}
$this->create_watermark ($filedir. $filename);
}
}
}
return $files;
}
Create thumbnails to generate thumbnails with the same extension
$SRC _file: Source image path, $thumb _file: Thumbnail path
function Create_thumb ($src _file, $thumb _file) {
$t _width = $this->thumb_width;
$t _height = $this->thumb_height;
if (!file_exists ($src _file)) return false;
$src _info = getimagesize ($src _file);
Copy the source image as a thumbnail if the source image is less than or equal to the thumbnail
if ($src _info[0] <= $t _width && $src _info[1] <= $t _height) {
if (!copy ($src _file, $thumb _file)) {
return false;
}
return true;
}
Calculate thumbnail size proportionally
if ($src _info[0]-$t _width > $src _info[1]-$t _height) {
$t _height = ($t _width/$src _info[0]) * $SRC _info[1];
} else {
$t _width = ($t _height/$src _info[1]) * $SRC _info[0];
}
Get file name extension
$fileext = $this->fileext ($src _file);
Switch ($fileext) {
Case ' jpg ':
$src _img = imagecreatefromjpeg ($src _file); Break
Case ' PNG ':
$src _img = imagecreatefrompng ($src _file); Break
Case ' gif ':
$src _img = imagecreatefromgif ($src _file); Break
}
Create a true-color thumbnail image
$thumb _img = @ImageCreateTrueColor ($t _width, $t _height);
imagecopyresampled function Copy of the image smoothness is better, the first consideration
if (function_exists (' imagecopyresampled ')) {
@ImageCopyResampled ($thumb _img, $src _img,0,0,0,0, $t _width, $t _height, $src _info[0], $src _info[1]);
} else {
@ImageCopyResized ($thumb _img, $src _img,0,0,0,0, $t _width, $t _height, $src _info[0], $src _info[1]);
}
Generate thumbnails
Switch ($fileext) {
Case ' jpg ':
Imagejpeg ($thumb _img, $thumb _file); Break
Case ' gif ':
Imagegif ($thumb _img, $thumb _file); Break
Case ' PNG ':
Imagepng ($thumb _img, $thumb _file); Break
}
Destroying temporary images
@ImageDestroy ($src _img);
@ImageDestroy ($thumb _img);
return true;
}
Add a watermark to a picture
$file: Files to add a watermark
function Create_watermark ($file) {
Returns if the file does not exist
if (!file_exists ($this->watermark_file) | | |!file_exists ($file)) return;
if (!function_exists (' getimagesize ')) return;
Check the file types supported by GD
$GD _allow_types = Array ();
if (function_exists (' imagecreatefromgif ')) $gd _allow_types[' image/gif '] = ' imagecreatefromgif ';
if (function_exists (' imagecreatefrompng ')) $gd _allow_types[' image/png '] = ' imagecreatefrompng ';
if (function_exists (' imagecreatefromjpeg ')) $gd _allow_types[' image/jpeg '] = ' imagecreatefromjpeg ';
Get file information
$fileinfo = getimagesize ($file);
$wminfo = getimagesize ($this->watermark_file);
if ($fileinfo [0] < $wminfo [0] | | | $fileinfo [1] < $wminfo [1]) return;
if (array_key_exists ($fileinfo [' MIME '], $GD _allow_types)) {
if (array_key_exists ($wminfo [' MIME '], $GD _allow_types)) {
Create an image from a file
$temp = $gd _allow_types[$fileinfo [' MIME ']] ($file);
$temp _wm = $gd _allow_types[$wminfo [' MIME ']] ($this->watermark_file);
Watermark Location
Switch ($this->watermark_pos) {
Case 1://Top Left
$DST _x = 0; $DST _y = 0; Break
Case 2://Top Center
$DST _x = ($fileinfo [0]-$wminfo [0])/2; $DST _y = 0; Break
Case 3://Top Right
$DST _x = $fileinfo [0]; $DST _y = 0; Break
Case 4://Bottom Left
$DST _x = 0; $DST _y = $fileinfo [1]; Break
Case 5://Bottom Center
$DST _x = ($fileinfo [0]-$wminfo [0])/2; $DST _y = $fileinfo [1]; Break
Case 6://Bottom Right
$DST _x = $fileinfo [0]-$wminfo [0]; $DST _y = $fileinfo [1]-$wminfo [1]; Break
Default://Random
$DST _x = Mt_rand (0, $fileinfo [0]-$wminfo [0]); $DST _y = Mt_rand (0, $fileinfo [1]-$wminfo [1]);
}
if (function_exists (' imagealphablending ')) imagealphablending ($temp _wm,true); Set the blending mode of the image
if (function_exists (' Imagesavealpha ')) Imagesavealpha ($temp _wm,true); To save full alpha channel information
Add a watermark to an image
if (function_exists (' Imagecopymerge ')) {
Imagecopymerge ($temp, $temp _wm, $dst _x, $dst _y,0,0, $wminfo [0], $wminfo [1], $this->watermark_trans);
} else {
Imagecopymerge ($temp, $temp _wm, $dst _x, $dst _y,0,0, $wminfo [0], $wminfo [1]);
}
Save picture
Switch ($fileinfo [' MIME ']) {
Case ' Image/jpeg ':
@imageJPEG ($temp, $file);
Break
Case ' image/png ':
@imagePNG ($temp, $file);
Break
Case ' image/gif ':
@imageGIF ($temp, $file);
Break
}
Destroying zero images
@imageDestroy ($temp);
@imageDestroy ($temp _wm);
}
}
}
Get file name extension
function Fileext ($filename) {
Return Strtolower (substr strrchr ($filename, '. '), 1,10);
}
}
I hope this article will help you with your PHP program design.