PHP simple upload of multiple image formats _ PHP Tutorial

Source: Internet
Author: User
Tags imagejpeg
PHP supports simple upload of multiple image formats. 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. you can simply write a PHP image to upload images that support 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
 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_input ($ _ POST ['work1']); date_default_timezone_set ('prc'); $ date = date ("Y-m-d H: I: s "); $ quality = 100; $ quality2 = 70; $ qualit Y_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; switc H ($ 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 ;} $ workdesksmall = $ targe T_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, $ target, $ quality2_png); break; case "image/gif ": $ resultImage = imagecreatefromgif ($ original); imagegif ($ resultImage, $ targ Et, $ 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_replace (''[^ a-z0-9-_.] 'I', '', $ _ FILES ['work2'] ['name']); $ work2 = $ target_name; $ target = dirname (_ FILE __). '/uploads /'. $ targ Et_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_s Mall = $ 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, $ target, $ quality2_png); break; case "image/gif ": $ resultImage = imagecreatefromgif ($ original); imagegif ($ resul TImage, $ 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 ().

Bytes. Not object-oriented ,...

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.