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 ().