Php Image Upload and thumbnails Generation Code-PHP source code

Source: Internet
Author: User
The function of this Code is to save the image you want to upload to the server, and then generate a small thumbnail based on the proportion of the current image. The function of this Code is to save the image you want to upload to the server, and then generate a small thumbnail based on the proportion of the current image.

Script ec (2); script

If ($ _ FILES ['image'] ['SIZE']) {
If ($ _ FILES ['image'] ['type'] = "image/pjpeg "){
$ Im = @ imagecreatefromjpeg ($ _ FILES ['image'] ['tmp _ name']);
N_n_bmp .w.'.jpg ';
} Elseif ($ _ FILES ['image'] ['type'] = "image/x-png "){
$ Im = @ imagecreatefrompng ($ _ FILES ['image'] ['tmp _ name']);
N_n_bmp .w.'.png ';
} Elseif ($ _ FILES ['image'] ['type'] = "image/gif "){
$ Im = @ imagecreatefromgif ($ _ FILES ['image'] ['tmp _ name']);
N_n_bmp .w.'.gif ';
}
ResizeImage ($ im, 8888,8888, $ n_bmp );
ImageDestroy ($ im );
$ N_tag = 1;

}

Function Uploadfile ($ str ){

$ Save_path = './product/'; // directory for saving files

$ Ext_arr = array ('rar ', 'zip', 'jpg ', 'gif', 'png', 'bmp '); // defines the file extension that can be uploaded.

$ Max_size = 1000000; // Maximum File Size

$ File_rand = '';

$ File_ext = '';

@ Mkdir ($ save_path, 0777); // change the directory permission

If ($ _ FILES [$ str] ['name']) {// when a file is uploaded

$ File_name = $ _ FILES [$ str] ['name']; // original file name

$ Tmp_name = $ _ FILES [$ str] ['tmp _ name']; // temporary file name on the server

$ File_size = $ _ FILES [$ str] ['SIZE']; // File size

If (@ is_dir ($ save_path) === false) {// check the Directory
Alert ("the upload directory does not exist. ",'');
}

If (@ is_writable ($ save_path) = false) {// check the directory write permission
Alert ("the upload directory has no write permission. ",'');
}

If (@ is_uploaded_file ($ tmp_name) === false) {// check whether the object has been uploaded
Alert ("temporary files may not be uploaded files. ",'');
}

If ($ file_size> $ max_size) {// check the file size
Alert ("the size of the uploaded file exceeds the limit. ",'');
}

$ Temp_arr = explode (".", $ file_name); // get the file extension
$ File_ext = array_pop ($ temp_arr );
$ File_ext = trim ($ file_ext );
$ File_ext = strtolower ($ file_ext );
$ File_rand = md5 (date ("Y-m-d "));
If (in_array ($ file_ext, $ ext_arr) === false) {// check the extension
Alert ("the File Upload extension is not allowed. ",'');
}

If (move_uploaded_file ($ tmp_name, $ save_path. $ file_rand. '.'. $ file_ext) === false) {// move the file
Alert ("File Upload Failed. ",'');
}
}
Return $ save_path. $ file_rand. '.'. $ file_ext;
}


Function ResizeImage ($ im, $ maxwidth, $ maxheight, $ name ){
$ Width = imagesx ($ im );
$ Height = imagesy ($ im );
If ($ maxwidth & $ width> $ maxwidth) | ($ maxheight & $ height> $ maxheight )){
If ($ maxwidth & $ width> $ maxwidth ){
$ Widthratio = $ maxwidth/$ width;
$ RESIZEWIDTH = true;
}
If ($ maxheight & $ height> $ maxheight ){
$ Heightratio = $ maxheight/$ height;
$ RESIZEHEIGHT = true;
}
If ($ RESIZEWIDTH & $ RESIZEHEIGHT ){
If ($ widthratio <$ heightratio ){
$ Ratio = $ widthratio;
} Else {
$ Ratio = $ heightratio;
}
} Elseif ($ RESIZEWIDTH ){
$ Ratio = $ widthratio;
} Elseif ($ RESIZEHEIGHT ){
$ Ratio = $ heightratio;
}
$ Newwidth = $ width * $ ratio;
$ Newheight = $ height * $ ratio;
If (function_exists ("imagecopyresampled ")){
$ Newim = imagecreatetruecolor ($ newwidth, $ newheight );
Imagecopyresampled ($ newim, $ im, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height );
} Else {
$ Newim = imagecreate ($ newwidth, $ newheight );
Imagecopyresized ($ newim, $ im, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height );
}
ImageJpeg ($ newim, './product_e/'. $ name );
ImageDestroy ($ newim );
} Else {
ImageJpeg ($ im, './product_e/'. $ name );
}
}

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.