PHP multiple image format simple upload _php tutorial

Source: Internet
Author: User
Tags imagejpeg
Simply write a PHP image upload, can support JPG, PNG, GIF and other formats of the picture, if you want to support more formats, such as BMP, you can expand their own. It is not object-oriented, and is simply recorded here for later access.

This time support upload 2 images, upload the original image and poor quality diagram, the original image for the preservation of high-quality pictures, poor quality of the picture for the page 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 '); $work 1 = check_input ($_post[' Work1 ']);d ate_default_timezone_set (' PRC '); $ Date = Date ("y-m-d h:i:s"), $quality = N, $quality 2 = $quality, $quality _png = 9; 2_png = 7;if (isset ($_files[' Work1 ']) &am p;& $_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 ']); $work 1 = $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:d IE ("This file type is not supported"); Exit;} $work 1_small = $target _name; $target = DirName (__file__). ' /uploads/small/'. $target _name;switch ($type) {case "image/jpeg": $resultImage = Imagecreatefromjpeg ($original); Imagejpeg ($resultImage, $target, $quality 2); Break;case "Image/png": $resultImage = Imagecreatefrompng ($original); Imagepng ($resultImage, $target, $quality 2_png); Break;case "Image/gif": $resultImage = Imagecreatefromgif ($original); Imagegif ($resultImage, $target, $quality 2);        Break;default:d IE ("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 ']); $work 2 = $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        ;d efault:d IE ("This file type is not supported"); Exit;} $work 2_small = $target _name; $target = DirName (__file__). ' /uploads/small/'. $target _name;switch ($type) {case "image/jpeg": $resultImage = Imagecreatefromjpeg ($original); Imagejpeg ($resultImage, $target, $quality 2); Break;case "Image/png": $resultImage = Imagecreatefrompng ($original); Imagepng ($resultImage, $target, $quality 2_png); Break;case "Image/gif": $resultImage = Imagecreatefromgif ($original); Imagegif ($resultImage, $target, $quality 2);        Break;default:d IE ("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 ', ' $work 1 ', ' $work 1_small ', ' $date ');// echo $sql; $result = $connector, query ($sql) or Die (Mysql_error ()); if ($work 2! = "" && $work 2_small! = null) {$s ql2= "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 ', ' $work 2 ', ' $work 2_small ', ' $date '); $result 2 = $connector, query ( $sql 2) or Die (Mysql_error ());} $connector, Close ($connector); $page = ". /upload.php "; echo" ";? >

Some questions need to be explained below:

The quality of the image generated by the JPEG image is a range from 0 (lowest quality, smallest file size) to 100 (highest quality, maximum file size). The imagepng generates an image with a mass range from 0 to 9, and the function does not work if the parameter is passed outside this range.

Determine the type of upload file, $type = $_files[' work1 ' [' type '];.

You can use these functions to create pictures of various formats: Imagecreatefromjpeg (), Imagecreatefrompng (), Imagecreatefromgif ().

http://www.bkjia.com/PHPjc/752564.html www.bkjia.com true http://www.bkjia.com/PHPjc/752564.html techarticle simply write a php image upload, can support JPG, PNG, GIF and other formats of the picture, if you want to support more formats, such as BMP, you can expand their own. 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.