You can simply write a PHP Image Upload file that supports jpg, png, gif, and other formats. If you want to support more formats, such as bmp, you can expand it by yourself. It is not object-oriented. Here we will record it for future use.
You can upload two images at a time. After the image is uploaded, the source image and the graph with poor quality are generated. The source image is used to save the picture with high quality, and the graph with poor quality is used for webpage display.
PHP Code
<? Phpinclude_once ("db. php "); include_once (" dbinfo. php "); $ connector = new nmdb ($ host, $ username, $ password); $ connector-> select_db ($ database ); $ work_group = check_input ($ _ POST ['work _ group']); $ name = check_input ($ _ POST ['name']); $ tel = check_input ($ _ POST ['tel']); $ id_card = check_input ($ _ POST ['id _ card']); $ work_name = check_input ($ _ POST ['work _ name']); $ place = check_input ($ _ POST ['place']); $ work1 = check_indium Ut ($ _ POST ['work1']); date_default_timezone_set ('prc'); $ date = date ("Y-m-d H: I: s "); $ quality = 100; $ quality2 = 70; $ quality_png = 9; $ quality2_png = 7; if (isset ($ _ FILES ['work1']) & $ _ FILES ['work1'] ['error'] = 0) {$ size = getimagesize ($ _ FILES ['work1'] ['tmp _ name']); $ original = $ _ FILES ['work1'] ['tmp _ name']; $ type = $ _ FILES ['work1'] ['type']; // echo $ type; $ target_name = date ('ymdhis '). '_'. preg_replace (''[^ a-z0- 9-_.] 'I', '', $ _ FILES ['work1'] ['name']); $ work1 = $ target_name; $ target = dirname (_ FILE __). '/uploads /'. $ target_name; switch ($ type) {case "image/jpeg": $ resultImage = imagecreatefromjpeg ($ original); imagejpeg ($ resultImage, $ target, $ quality); break; case "image/png": $ resultImage = imagecreatefrompng ($ original); imagepng ($ resultImage, $ target, $ quality_png); break; case "image/gif ": $ resultImage = imagecreatefr Omgif ($ original); imagegif ($ resultImage, $ target, $ quality); break; default: die ("this file type is not supported"); exit ;} $ workdesksmall = $ target_name; $ target = dirname (_ FILE __). '/uploads/small /'. $ target_name; switch ($ type) {case "image/jpeg": $ resultImage = imagecreatefromjpeg ($ original); imagejpeg ($ resultImage, $ target, $ quality2); break; case "image/png": $ resultImage = imagecreatefrompng ($ original); imagepng ($ resultImage, $ t Arget, $ quality2_png); break; case "image/gif": $ resultImage = imagecreatefromgif ($ original); imagegif ($ resultImage, $ target, $ quality2); break; default: die ("this file type is not supported"); exit ;}} if (isset ($ _ FILES ['work2']) & $ _ FILES ['work2'] ['error'] = 0) {$ size = getimagesize ($ _ FILES ['work2'] ['tmp _ name']); $ original = $ _ FILES ['work2'] ['tmp _ name']; $ type = $ _ FILES ['work2'] ['type']; $ target_name = date ('ymdhis '). '_'. preg_repl Ace (''[^ a-z0-9-_.] 'I', '', $ _ FILES ['work2'] ['name']); $ work2 = $ target_name; $ target = dirname (_ FILE __). '/uploads /'. $ target_name; switch ($ type) {case "image/jpeg": $ resultImage = imagecreatefromjpeg ($ original); imagejpeg ($ resultImage, $ target, $ quality); break; case "image/png": $ resultImage = imagecreatefrompng ($ original); imagepng ($ resultImage, $ target, $ quality_png); break; case "image/gif": $ resultImage = Imagecreatefromgif ($ original); imagegif ($ resultImage, $ target, $ quality); break; default: die ("this file type is not supported"); exit ;} $ work2_small = $ target_name; $ target = dirname (_ FILE __). '/uploads/small /'. $ target_name; switch ($ type) {case "image/jpeg": $ resultImage = imagecreatefromjpeg ($ original); imagejpeg ($ resultImage, $ target, $ quality2); break; case "image/png": $ resultImage = imagecreatefrompng ($ original); imagepng ($ re SultImage, $ target, $ quality2_png); break; case "image/gif": $ resultImage = imagecreatefromgif ($ original); imagegif ($ resultImage, $ target, $ quality2 ); break; default: die ("this file type is not supported"); exit ;}$ SQL = "insert into photo (work_group, name, tel, id_card, work_name, place, work, work_small, date) values ('$ work_group', '$ name',' $ tel ',' $ id_card ',' $ work_name ',' $ place ', '$ work1',' $ workdesksmall', '$ date') "; // echo $ SQL; $ result = $ Connector-> query ($ SQL) or die (mysql_error (); if ($ work2! = "" & $ Work2_small! = Null) {$ sql2 = "insert into photo (work_group, name, tel, id_card, work_name, place, work, work_small, date) values ('$ work_group ', '$ name',' $ tel', '$ id_card', '$ work_name', '$ place', '$ work2', '$ work2_small', '$ date ') "; $ result2 = $ connector-> query ($ sql2) or die (mysql_error ();} $ connector-> close ($ connector); $ page = ".. /upload. php "; echo" <script> alert ('upload successful! '); Window. location = "". $ page. ""; </script> ";?>
Some problems need to be explained as follows:
The quality of the image generated by JPEG images ranges from 0 (minimum quality, Minimum File Size) to 100 (maximum quality, maximum file size ). However, if the quality range of the image generated by ImagePNG is from 0 to 9, the function does not work if the parameter is not passed in.
Determine the type of the uploaded file, $ type = $ _ FILES ['work1'] ['type'];.
You can use these functions to create images of various formats: imagecreatefromjpeg (), imagecreatefrompng (), and imagecreatefromgif ().