From the PHP100, the function is very powerful. Almost every detail is taken into account to share with you! ~~~
- <meta http-equiv="Content-type" content= "text/html; Charset=utf-8 "/>
- <?php
- /******************************************************************************
- Parameter description:
- $max _file_size: Upload file size limit, unit byte
- $destination _folder: Upload file path
- $watermark: Whether additional watermark (1 is watermark, the other is not watermark);
- Instructions for use:
- 1. Remove the "Extension=php_gd2.dll" line in the php.ini file, because we need to use the GD library;
- 2. Change Extension_dir = to the directory where your Php_gd2.dll is located;
- ******************************************************************************/
- Upload file Type list
- $uptypes =Array (
- ' Image/jpg ',
- ' Image/jpeg ',
- ' Image/png ',
- ' Image/pjpeg ',
- ' Image/gif ',
- ' Image/bmp ',
- ' Image/x-png '
- );
- $max _file_size=2000000; //Upload file size limit, unit byte
- $destination _folder="uploadimg/"; //Upload file path
- $watermark = 1; //Whether additional watermark (1 is watermark, the other is not watermark);
- $watertype = 1; //Watermark type (1 for text, 2 for picture)
- $waterposition = 1; //Watermark position (1 is lower left corner, 2 is lower right corner, 3 is upper left corner, 4 is upper right corner, 5 is centered);
- $waterstring ="http://www.xplore.cn/"; Watermark String
- $waterimg ="Xplore.gif"; //Watermark picture
- $imgpreview = 1; //Whether to generate a preview map (1 is generated, others are not generated);
- $imgpreviewsize =1/2; //thumbnail scale
- ?>
- <title>zwell Picture Uploading program </title>
- <style type="Text/css" >
- <!--
- Body
- {
- font-size:9pt;
- }
- Input
- {
- Background-color: #66CCFF;
- border:1px inset #CCCCCC;
- }
- -
- </style>
- <body>
- <form enctype="Multipart/form-data" method="post" name="Upform" >
- Upload file:
- <input name="upfile" type="file" >
- <input type="Submit" value="upload" ><br>
- The file types that are allowed to be uploaded are: <?=implode (', ',$uptypes)?>
- </form>
- <?php
- if ($_server[' request_method '] = = ' POST ')
- {
- if (! Is_uploaded_file ($_files["Upfile"][tmp_name]))
- Whether the file exists
- {
- echo "Picture does not exist!";
- Exit
- }
- $file = $_files["Upfile"];
- if ($max _file_size < $file ["size"])
- Check File size
- {
- echo "file too big!";
- Exit
- }
- if (!in_array ($file ["type"], $uptypes))
- Check file types
- {
- echo "file type does not match!". $file ["type"];
- Exit
- }
- if (! File_exists ($destination _folder))
- {
- mkdir ($destination _folder);
- }
- $filename =$file ["Tmp_name"];
- $image _size = getimagesize ($filename);
- $pinfo =pathinfo ($file ["name"]);
- $ftype =$pinfo [' extension '];
- $destination = $destination _folder.time (). ".". $ftype;
- if (file_exists ($destination) && $overwrite! = True)
- {
- echo "file with the same name already exists";
- Exit
- }
- if (!move_uploaded_file ($filename, $destination))
- {
- echo "error moving File";
- Exit
- }
- $pinfo =pathinfo ($destination);
- $fname =$pinfo [basename];
- echo "<font Color=red> has successfully uploaded </font><br> FileName: <font color=blue>". $destination _folder. $fname. "</font><br>";
- echo "width:". $image _size[0];
- echo "Length:". $image _size[1];
- echo "<br> size:". $file ["size"]."bytes";
- if ($watermark ==1)
- {
- $iinfo =getimagesize ($destination,$iinfo);
- $nimage =imagecreatetruecolor ($image _size[0],$image _size[1]);
- $white =imagecolorallocate ($nimage, 255,255,255);
- $black =imagecolorallocate ($nimage, 0,0,0);
- $red =imagecolorallocate ($nimage, 255,0,0);
- Imagefill ($nimage, 0,0,$white);
- Switch ($iinfo [2])
- {
- Case 1:
- $simage =imagecreatefromgif ($destination);
- Break
- Case 2:
- $simage =imagecreatefromjpeg ($destination);
- Break
- Case 3:
- $simage =imagecreatefrompng ($destination);
- Break
- Case 6:
- $simage =imagecreatefromwbmp ($destination);
- Break
- Default
- Die ("Unsupported file type");
- Exit
- }
- Imagecopy ($nimage,$simage, 0,0,0,0,$image _size[0],$image _size[1]);
- Imagefilledrectangle ($nimage, 1,$image _size[1]-15,80,$image _size[1],$white);
- Switch ($watertype)
- {
- Case 1: //Add watermark string
- Imagestring ($nimage, 2,3,$image _size[1]-15,$waterstring,$black);
- Break
- Case 2: //Add watermark Picture
- $simage 1 =imagecreatefromgif ("xplore.gif");
- Imagecopy ($nimage,$simage 1,0,0,0,0,85,15);
- Imagedestroy ($simage 1);
- Break
- }
- Switch ($iinfo [2])
- {
- Case 1:
- Imagegif ($nimage, $destination);
- Imagejpeg ($nimage, $destination);
- Break
- Case 2:
- Imagejpeg ($nimage, $destination);
- Break
- Case 3:
- Imagepng ($nimage, $destination);
- Break
- Case 6:
- Imagewbmp ($nimage, $destination);
- Imagejpeg ($nimage, $destination);
- Break
- }
- Overwrite the original upload file
- Imagedestroy ($nimage);
- Imagedestroy ($simage);
- }
- if ($imgpreview ==1)
- {
- echo "<br> picture preview:<br>";
- echo " $destination. " \" Width= ". ($image _size[0]*$imgpreviewsize). "height=". ($image _size[1]*$imgpreviewsize);
- echo "alt=\" Picture preview: \ r file name: ". $destination. "\ r upload time: \" > ";
- }
- }
- ?>
- </body>
PHP image upload Program (full version)