Php image uploading and generating thumbnails _ PHP Tutorial

Source: Internet
Author: User
Tags upload php
Upload php images and generate thumbnails. 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 to upload php images and generate thumbnails, this tutorial is a php image upload, and then generate a small image for the uploaded images. it is a very good File upload class, if you are looking for a class program, you can come in and check it out.

Php Tutorial: upload images and generate thumbnails
This tutorial is a php image upload and then generates 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 see it.
*/

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;
}
?>

...

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.