<? PHP /*************************************** *************************************** Parameter description: $ Max_file_size: size limit of the uploaded file, in bytes $ Destination_folder: File Upload path $ Watermark: whether to add a watermark. (1 indicates adding a watermark; others indicates not adding a watermark ); Instructions for use: 1. Remove the number before the line "extension = php_gd2.dll" in the PHP. ini file because the GD library is used; 2. Change extension_dir = to the directory where your php_gd2.dll is located; **************************************** **************************************/ // list of uploaded file types $ uptypes = array ( 'image/jpg ', 'image/JPEG ', 'image/PNG ', 'image/pjpeg', 'image/gif', 'image/BMP ', 'image/X-PNG ' ); $ max_file_size = 2000000; // the size of the uploaded file. Unit: byte $ destination_folder = "uploadimg /"; // Upload File Path $ watermark = 1; // whether to append the watermark. (1 indicates adding a watermark, and others indicates not adding a watermark.) $ watertype = 1; // watermark type (1 is text, 2 is image) $ waterposition = 1; // watermark position (1 is lower left, 2 is lower right, 3 is upper left, 4 is the upper right corner, 5 is the center); $ waterstring = "http://www.xplore.cn/"; // watermark string $ waterimg = "xplore.gif "; // watermark image $ imgpreview = 1; // whether to generate a preview image (1 is generated, other is not generated); $ imgpreviewsize = 1/2; // thumbnail ratio ?> Zwell Image Upload Program <Body> <Form enctype = "multipart/form-Data" method = "Post" name = "upform"> Upload files: <Input name = "upfile" type = "file"> <Input type = "Submit" value = "Upload"> <br> The file type that can be uploaded is: <? = Implode (',', $ uptypes)?> </Form> <? PHP If ($ _ server ['request _ method'] = 'post ') { If (! Is_uploaded_file ($ _ FILES ["upfile"] [tmp_name]) // Whether a file exists { Echo "the image does not exist! "; Exit; } $ File = $ _ FILES ["upfile"]; If ($ max_file_size <$ file ["size"]) // Check the file size { Echo "the file is too large! "; Exit; } If (! In_array ($ file ["type"], $ uptypes )) // Check the file type { 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 "a file with the same name already exists "; Exit; } If (! Move_uploaded_file ($ filename, $ destination )) { Echo "An error occurred while moving the file "; Exit; } $ Pinfo = pathinfo ($ destination ); $ Fname = $ pinfo [basename]; Echo "<font color = Red> uploaded successfully </font> <br> file name: <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 ); $ 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 types "); Exit; } Imagecopy ($ nimage, $ simage, 0, 0, 0, $ image_size [0], $ image_size [1]); Imagefilledrectangle ($ nimage, 1, $ image_size [1]-15, 80, $ image_size [1], $ white ); switch ($ watertype) {< br> case 1: // Add a watermark string imagestring ($ nimage, 2, 3, $ image_size [1]-15, $ waterstring, $ black); break; case 2: // Add a watermark image $ simage1 = imagecreatefromgif ("xplore.gif "); imagecopy ($ nimage, $ simage1,); imagedestroy ($ simage1); 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 uploaded file Imagedestroy ($ nimage ); Imagedestroy ($ simage ); } If ($ imgpreview = 1) { Echo "<br> image preview: <br> "; Echo "Echo "alt = \" image preview: \ r file name: ". $ destination." \ r upload time: \ "> "; } } ?> </Body> </Html> |