PHP supports uploading images in multiple formats (jpg, png, and gif)

Source: Internet
Author: User
Tags imagejpeg
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 simply record it so that we can use it to upload two images at a time. After uploading, we will generate the source image and the graph with poor quality. the source image is used to save high-quality images, poor quality charts are used for webpage display.
PHP Code
Copy codeThe code is as follows:
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']);
$ Work1 = check_input ($ _ 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 = imagecreatefromgif ($ 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, $ target, $ 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_replace (''[^ 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 ($ resultImage, $ 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 ().

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.