This article mainly introduces the File Upload class that can generate thumbnails in php, and provides the complete class file code and corresponding usage examples, which is very useful. For more information, see
This article mainly introduces the File Upload class that can generate thumbnails in php, and provides the complete class file code and corresponding usage examples, which is very useful. For more information, see
This article describes the File Upload class for generating thumbnails in php and its usage. Share it with you for your reference. The specific implementation method is as follows:
The method for calling class files is as follows:
The Code is 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 );
}
?>
Test
The upload class file is as follows:
The Code is as follows:
Class upload {
Var $ dir; // The physical directory where the attachment is stored.
Var $ time; // custom file upload time
Var $ allow_types; // The attachment type that can be uploaded
Var $ field; // The Name Of The upload control.
Var $ maxsize; // maximum allowed file size, in KB
Var $ thumb_width; // The width of the thumbnail.
Var $ thumb_height; // The height of the thumbnail.
Var $ watermark_file; // watermark image address
Var $ watermark_pos; // watermark position
Var $ watermark_trans; // watermark transparency
// Open source code phpfensi.com
// Constructor
// $ Types: Specifies the file type that can be uploaded. $ maxsize: size allowed. $ field: indicates the name of the upload control. $ time: indicates the 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 and create a directory for storing files
// $ Basedir: base Directory, which must be a physical path
// $ Filedir: Custom subdirectory. Available parameters: {y}, {m}, and {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;
}
// Set the image thumbnail. If no thumbnail is generated, you do not need to set it.
// $ Width: thumbnail width, $ height: thumbnail height
Function set_thumb ($ width = 0, $ height = 0 ){
$ This-> thumb_width = $ width;
$ This-> thumb_height = $ height;
}
// Set the image watermark. You do not need to set the watermark if no watermark is generated.
// $ File: watermark image, $ pos: watermark position, $ trans: watermark transparency
Function set_watermark ($ file, $ pos = 6, $ trans = 80 ){
$ This-> watermark_file = $ file;
$ This-> watermark_pos = $ pos;
$ This-> watermark_trans = $ trans;
}
/*----------------------------------------------------------------
After the file is uploaded, an array of information about the file that has been uploaded successfully or failed is returned ,,
Where: name is the file name. When the upload is successful, it is the file name uploaded to the server. If the upload fails, it is the local file name.
Dir is the physical path where the attachment is stored on the server. This value does not exist when the upload fails.
Size indicates the attachment size. This value does not exist when the upload fails.
Flag indicates the status, 1 indicates the success,-1 indicates that the file type is not allowed, and-2 indicates that the file size exceeds
-----------------------------------------------------------------*/
Function execute (){
$ Files = array (); // information of the successfully uploaded file
$ 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 the file extension
$ Filename = date ('ymdhis ', $ this-> time). mt_rand (10, 99).'. '. $ fileext; // generate a file name
$ Filedir = $ this-> dir; // actual directory of the Attachment
$ Filesize = $ _ FILES [$ field] ['SIZE'] [$ key]; // File size
// The file type is not allowed.
If (! In_array ($ fileext, $ this-> allow_types )){
$ Files [$ key] ['name'] =_ _ FILES [$ field] ['name'] [$ key];
$ Files [$ key] ['flag'] =-1;
Continue;
}
// The 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 the uploaded file and delete the temporary file
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;
// Add a watermark to the image and generate a thumbnail
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 a thumbnail to generate a thumbnail 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 );
// If the source image is smaller than or equal to the thumbnail, copy the source image as the thumbnail.
If ($ src_info [0] <= $ t_width & $ src_info [1] <= $ t_height ){
If (! Copy ($ src_file, $ thumb_file )){
Return false;
}
Return true;
}
// Calculate the 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 the file 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 real-color thumbnail image
$ Thumb_img = @ ImageCreateTrueColor ($ t_width, $ t_height );
// The image copied by the ImageCopyResampled function has good smoothness and is preferred.
If (function_exists ('imagecopyresampled ')){
@ ImageCopyResampled ($ thumb_img, $ src_img, 0, 0, 0, $ t_width, $ t_height, $ src_info [0], $ src_info [1]);
} Else {
@ ImageCopyResized ($ thumb_img, $ src_img, 0, 0, 0, $ t_width, $ t_height, $ src_info [0], $ src_info [1]);
}
// Generate a thumbnail
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;
}
// Destroy temporary Images
@ ImageDestroy ($ src_img );
@ ImageDestroy ($ thumb_img );
Return true;
}
// Add a watermark to the image
// $ File: the file to add a watermark
Function create_watermark ($ file ){
// If the object does not exist, return
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 ';
// Obtain the 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 position
Switch ($ this-> watermark_pos ){
Case 1: // top left
$ Dst_x = 0; $ dst_y = 0; break;
Case 2: // center the top
$ 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: // center at the bottom
$ 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); // sets the mixed color mode of the image.
If (function_exists ('imagesavealpha ') ImageSaveAlpha ($ temp_wm, True); // Save the complete alpha channel information
// Add a watermark to the 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 the image
Switch ($ fileinfo ['mime ']) {
Case 'image/jpeg ':
@ ImageJPEG ($ temp, $ file );
Break;
Case 'image/png ':
@ ImagePNG ($ temp, $ file );
Break;
Case 'image/gif ':
@ ImageGIF ($ temp, $ file );
Break;
}
// Destroy the zero-time image
@ ImageDestroy ($ temp );
@ ImageDestroy ($ temp_wm );
}
}
}
// Obtain the file extension
Function fileext ($ filename ){
Return strtolower (substr (strrchr ($ filename, '.'), 1, 10 ));
}
}
I hope this article will help you with PHP programming.