Php Image Upload and thumbnail generation effect this tutorial is a php Image Upload and then generate a small image for the uploaded image. It is a very good File Upload class, if you are looking for a class program, you can come in and check it out.
Function uploadimage ($ upname, $ smallmark = 1, $ dstsw, $ dstsh = 0, $ path_dim, $ path_xim, $ newname, $ smallname = 0, $ filetype = "null ") {
Global $ webaddr, $ _ files, $ my;
$ Phpv = str_replace ('.', '', php_version );
$ Filename = $ upname;
$ Max_file_size = 2147483648; // the size of the uploaded file. The unit is byte 2 m.
$ Path_im = $ path_dim; // generate the path of the big picture save folder.
$ Path_sim = $ path_xim; // folder path for saving thumbnails
$ Simclearly = 75;
$ Simclearlypng = $ phpv >=512? 7: 75; // The thumbnail definition ranges from 0 to 100. The larger the number, the clearer the file size.
$ Smallmark = $ smallmark; // whether to generate a thumbnail (1 is generated by adding a thumbnail, and others are not );
$ Dst_sw = $ dstsw; // define the width and height of the thumbnail. I use proportional scaling, so you only need to compare the width.
$ Uptypes = array (
'Image/jpg ',
'Image/jpeg ',
'Image/png ',
'Image/pjpeg ',
'Image/gif ',
'Image/bmp ',
'Image/x-png'
);
If (! Is_uploaded_file ($ _ files [$ filename] [tmp_name]) {
Dsetcookie ('setok', 'upload1 ');
Header ("location: bKjia. c0m/profile ");
Exit;
}
$ File = $ _ files [$ filename];
$ Pinfo = pathinfo ($ file ["name"]);
If ($ filetype = "null "){
$ Filetype = $ pinfo ['extension'];
}
If (! In_array (strtolower ($ pinfo ['extension']), array ("jpg", "jpeg", "png", "gif "))){
Dsetcookie ('setok', 'upload3 ');
Header ("location: bKjia. c0m/profile ");
Exit;
}
If ($ max_file_size <$ file ["size"]) {// check the file size
Dsetcookie ('setok', 'upload2 ');
Header ("location: bKjia. c0m/profile ");
Exit;
}
If (! In_array ($ file ["type"], $ uptypes) {// check the file type
Dsetcookie ('setok', 'upload3 ');
Header ("location: bKjia. c0m/profile ");
Exit;
}
If (! File_exists ($ path_im )){
Mkdir ($ path_im );
}
$ Filename = $ file ["tmp_name"];
$ Im_size = getimagesize ($ filename );
$ Src_w = $ im_size [0];
$ Src_h = $ im_size [1];
$ Src_type = $ im_size [2];
$ All_path = $ path_im. $ newname. ".". $ filetype; // path + file name. The name of the uploaded time is
If (file_exists ($ all_path )){
@ Unlink ($ all_path );
}
If (! Move_uploaded_file ($ filename, $ all_path )){
Dsetcookie ('setok', 'upload4 ');
Header ("location: bKjia. c0m/profile ");
Exit;
}
$ Pinfo = pathinfo ($ all_path );
$ Fname = $ pinfo [basename];
Switch ($ src_type) {// determine the source image file type
Case 1: // gif
$ Src_im = @ imagecreatefromgif ($ all_path); // obtain the image from the source Image File
Break;
Case 2: // jpg
$ Src_im = @ imagecreatefromjpeg ($ all_path );
Break;
Case 3: // png
$ Src_im = @ imagecreatefrompng ($ all_path );
Break;
// Case 6:
// $ Src_im = imagecreatefromwbmp ($ all_path );
// Break;
Default:
Dsetcookie ('setok', 'upload3 ');
Header ("location: bKjia. c0m/profile ");
Exit;
}
If ($ smallmark = 1 ){
If (! File_exists ($ path_sim) {// check whether the thumbnail directory exists and does not exist
Mkdir ($ path_sim );
}
If ($ smallname) $ newname = $ smallname;
$ Sall_path = $ path_sim. $ newname. ".". $ filetype;
If (file_exists ($ sall_path )){
@ Unlink ($ sall_path );
}
If ($ src_w <= $ dst_sw) {// source image size <= thumbnail size
If ($ dstsh = 0 ){
$ Dst_sim = @ imagecreatetruecolor ($ src_w, $ src_h); // create a thumbnail true color bitmap
$ Sx = $ sy = 0;
} Else {
$ Dst_sim = @ imagecreatetruecolor ($ dstsw, $ dstsh); // create a thumbnail true color bitmap
$ Sx = ($ dstsw-$ src_w)/2;
$ Sy = ($ dstsh-$ src_h)/2;
}
$ Img = @ imagecreatefrompng ("images/phbg.png ");
@ Imagecopymerge ($ dst_sim, $ img, 100, $ dstsw, $ dstsh,); // write the original image to the new true color bitmap.
@ Imagecopymerge ($ dst_sim, $ src_im, $ sx, $ sy, 100, $ src_w, $ src_h,); // write the original image to the new true color bitmap.
}
If ($ src_w> $ dst_sw) {// source image size> thumbnail size
$ Dst_sh = $ dst_sw/$ src_w * $ src_h;
If ($ dst_sh <$ dstsh ){
$ Dst_sh = $ dstsh;
$ Dst_sw = $ dst_sh/$ src_h * $ src_w;
}
If ($ dstsh = 0 ){
$ Dst_sim = @ imagecreatetruecolor ($ dst_sw, $ dst_sh); // create a thumbnail true color Bitmap (proportional reduction of the source image size)
} Else {
$ Dst_sim = @ imagecreatetruecolor ($ dstsw, $ dstsh); // create a thumbnail true color Bitmap (proportional reduction of the source image size)
}
@ Imagecopyresampled ($ dst_sim, $ src_im, $ dst_sw, $ dst_sh, $ src_w, $ src_h); // write the original image to the new true color bitmap.
}
Switch ($ src_type ){
Case 1: @ imagegif ($ dst_sim, $ sall_path, $ simclearly); // generates a gif file with an image definition ranging from 0 to 100.
Break;
Case 2: @ imagejpeg ($ dst_sim, $ sall_path, $ simclearly); // generate a jpg file. The image definition is 0-100.
Break;
Case 3: @ imagepng ($ dst_sim, $ sall_path, $ simclearlypng); // generate a png file with an image definition ranging from 0 to 100.
Break;
// Case 6:
// Imagewbmp ($ dst_sim, $ sall_path );
Break;
}
// Release the cache
@ Imagedestroy ($ dst_sim );
}
@ Imagedestroy ($ src_im );
Return $ newname. ".". $ filetype;
}
?>