PHP supports multiple format images upload (support jpg, PNG, GIF) _php tips

Source: Internet
Author: User
Tags imagejpeg
Here once to support the upload of 2 pictures, upload the original and poor quality of the picture, the original image for the preservation of high quality pictures, poor quality of the graphic for Web page display.
PHP Code
Copy Code code as follows:

<?php
Include_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 ']);
$work 1 = check_input ($_post[' work1 '));
Date_default_timezone_set (' PRC ');
$date = Date ("y-m-d h:i:s");
$quality = 100;
$quality 2 = 70;
$quality _png = 9;
$quality 2_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 ']];
$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:
Die ("does not support this file type");
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:
Die ("does not support this file type");
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
Default:
Die ("does not support this file type");
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:
Die ("does not support this file type");
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)
{
$sql 2= "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 "<script>alert" (' Upload success! '); Window.location = \ "". $page. " \ ";</script>";
?>

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 of a mass range from 0 to 9, and the function does not work if it is passed in a parameter other than the 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 ().

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.