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 ();
}