PHP file Upload class can generate thumbnails code _ PHP Tutorial

Source: Internet
Author: User
Tags imagejpeg php file upload
The PHP file Upload class can generate the thumbnail code. Php Tutorial file Upload class can generate thumbnail code? Phpif ($ _ GET [action] save) {$ upnewupload (); $ up-set_dir (dirname (_ FILE __). upload, {y} {m}); $ up-set_thumb (, 80 php Tutorial file upload class can generate thumbnail code
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



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


// 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 file information including successful or failed uploads 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 ));
}
}

Why? Php if ($ _ GET ['action'] = 'save') {$ up = new upload (); $ up-set_dir (dirname (_ FILE __). '/upload/', '{y}/{m}'); $ up-set_thumb (100, 80...

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.