PHP image upload and generate thumbnail functions

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags address domain domain name file file size file type function functions

Write your own PHP image upload and generate thumbnail function, please refer to

Function is divided into three parts

First, PHP image upload function

Second, PHP generate thumbnail function

Third, PHP message prompt function

// PHP image upload function is as follows:

function img_upload ($ form_name, $ size, $ dir, $ file_name, $ width, $ height) {
// file domain name, file size limit, file storage path, thumbnail width, thumbnail height
$ file_type_arr = array ("image / png" => "png", "image / jpeg" => "jpg",
"Image / x-png" => "png", "image / pjpeg" => "jpg", "image / gif" => "gif"); // array of file types
if ($ _FILES ["$ form_name"] ['size']> $ size) exit_close ("artist portrait must not exceed 50KB");
$ singer_pic_type = $ _ FILES ["$ form_name"] ['type']; // Get the file type
if (! array_key_exists ($ singer_pic_type, $ file_type_arr)) exit_close ("file type is incorrect!");
$ pic_dir = $ dir; // photo upload path
$ file_name = $ pic_dir. $ file_name. ".". $ file_type_arr ["$ singer_pic_type"];
if (move_uploaded_file ($ _ FILES ["$ form_name"] ['tmp_name'], $ file_name)) {
img_create_small ($ file_name, $ width, $ height, $ file_name); / / to upload the thumbnail generated image
return $ file_name; // return the file address
} else {
exit_close ("File upload failed, please try again");
}
}

// PHP generated thumbnail function is as follows

function img_create_small ($ big_img, $ width, $ height, $ small_img) {// large file address, thumbnail width, thumbnail height, thumbnail address
$ imgage = getimagesize ($ big_img); // Get big picture info
switch ($ imgage [2]) {// Determine the type of image
case 1:
$ im = imagecreatefromgif ($ big_img);
break;
case 2:
$ im = imagecreatefromjpeg ($ big_img);
break;
case 3:
$ im = imagecreatefrompng ($ big_img);
break;
}
$ src_W = imagesx ($ im); // Get wide image
$ src_H = imagesy ($ im); // Get big picture height
$ tn = imagecreatetruecolor ($ width, $ height); // Create a thumbnail
imagecopyresized ($ tn, $ im, 0,0,0,0, $ width, $ height, $ src_W, $ src_H); // Copy image and resize
imagejpeg ($ tn, $ small_img); // Output the image
}

// pop-up message function is as follows

function exit_close ($ msg) {// Eject message and return
echo "<script> alert ('$ msg'); history.go (-1); </ script>";
exit ();
}

Related Article

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.