Php Image Upload storage source code and can be previewed

Source: Internet
Author: User
Tags imagecopy imagejpeg

Copy codeThe Code is as follows: <? Php
Header ("content-Type: text/html; charset = gb2312 ");
$ Uptypes = array ('image/jpg ', // list of uploaded file types
'Image/jpeg ',
'Image/png ',
'Image/pjpeg ',
'Image/gif ',
'Image/bmp ',
'Application/x-shockwave-flash ',
'Image/x-png ',
'Application/msword ',
'Audio/x-ms-wm ',
'Audio/mp3 ',
'Application/vnd. rn-realmedia ',
'Application/x-zip-compressed ',
'Application/octet-stream ');
$ Max_file_size = 10000000; // size limit of uploaded files, in bytes
$ Path_parts = pathinfo ($ _ SERVER ['php _ SELF ']); // obtain the current path
$ Destination_folder = "up/"; // File Upload path
$ Watermark = 0; // whether to add a watermark. (1 indicates adding a watermark, and 0 indicates not adding a watermark );
$ Watertype = 1; // watermark type (1 is text, 2 is image)
$ Waterposition = 2; // watermark position (1 indicates the lower left corner, 2 indicates the lower right corner, 3 indicates the upper left corner, 4 indicates the upper right corner, and 5 indicates the center );
$ Waterstring = "www. yinao. tk"; // watermark string
$ Waterimg = "xplore.gif"; // watermark image
$ Imgpreview = 1; // whether to generate a preview image (1 is generated, 0 is not generated );
$ Imgpreviewsize = 1/1; // thumbnail Ratio
?>
<Html xmlns = "undefined">
<Head>
<Title> Image Upload and storage </title>
<LINK rel = "Bookmark" href = "http://www.yinao.tk/ico.ico">
<LINK rel = "Shortcut Icon" href = "http://www.yinao.tk/ico.ico"/>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Meta name = "description" content = "EIP _ Unlimited Online Large Image Upload and storage"/>
<Style type = "text/css">
Body, td {font-family: tahoma, verdana, arial; font-size: 11px; line-height: 15px; background-color: white; color: #666666;
Strong {font-size: 12px ;}
A: link {color: # 0066CC ;}
A: hover {color: # FF6600 ;}
A: visited {color: #003366 ;}
A: active {color: #9DCC00 ;}
A {TEXT-DECORATION: none}
Td. irows {height: 20px; background: url ("index. php? I = dots ") repeat-x bottom}
</Style>
</Head>
<Script type = "text/javascript"> function ocopy(obj1_1_obj.select(1_1_js1_obj.createtextrange(1_1_js.exe cCommand ("Copy") ;}; function sendtof (url) {window. clipboardData. setData ('text', url); alert ('copied address successful, pasted to your friends for sharing. ') ;}; Function select_format () {var on = document. getElementById ('fmt'). checked; document. getElementById ('SITE'). style. display = on? 'None': ''; document. getElementById ('sited'). style. display =! On? 'None': '';}; var flag = false; function DrawImage (ImgD) {var image = new Image (); image. src = ImgD. src; if (image. width> 0 & image. height> 0) {flag = true; if (image. width/image. height> = 120/80) {if (image. width & gt; 120) {ImgD. width = 120; ImgD. height = (image. height * 120)/image. width;} else {ImgD. width = image. width; ImgD. height = image. height;}; ImgD. alt = image. width + "×" + image. height;} else {if (image. height> 80) {ImgD. height = 80; ImgD. width = (image. width * 80)/image. height;} else {ImgD. width = image. width; ImgD. height = image. height;}; ImgD. alt = image. width + "×" + image. height ;};}; function FileChange (Value) {flag = false; document. all. uploadimage. width = 10; document. all. uploadimage. height = 10; document. all. uploadimage. alt = ""; document. all. uploadimage. src = Value ;}; </script>
<Body bgcolor = "# FFFFFF">
<Center>
<Form enctype = "multipart/form-data" method = "post" name = "upform">
<Table border = "1" width = "55%" id = "table1" cellspacing = 0>
<Tr>
<Td colspan = "2"> <p align = "center"> maximum file size: 1 MB </td>
</Tr>
<Tr>
<Td width = "10%"> <div style = "width: 120px; height: 80px; overflow: hidden; text-align: center; "> </div> </td>
<Td width = "71%"> <div style = "width: 361px; height: 80px; overflow: hidden; text-align: center; padding: 30px;">
<Input style = "width: 208; border: 1 solid #9a9999; font-size: 9pt; background-color: # ffffff; height: 18 "size =" 17 "name = upfile type = file
Onchange = "javascript: FileChange (this. value);">
<Input type = "submit" value = "Upload" style = "width: 60; border: 1 solid #9a9999; font-size: 9pt; background-color: # ffffff; height: 18 "size =" 17 "> </td>
</Tr>
</Table>
The file type that can be uploaded is: jpg | jpeg | gif | bmp | png | swf | mp3 | wma | zip | rar | doc </form>.
<? Php
If ($ _ SERVER ['request _ method'] = 'post ')
{
If (! Is_uploaded_file ($ _ FILES ["upfile"] [tmp_name])
// Whether a file exists
{
Echo "<font color = 'red'> the file does not exist! </Font> ";
Exit;
}
$ File = $ _ FILES ["upfile"];
If ($ max_file_size <$ file ["size"])
// Check the file size
{
Echo "<font color = 'red'> the file is too large! </Font> ";
Exit;
}
If (! In_array ($ file ["type"], $ uptypes ))
// Check the file type
{
Echo "<font color = 'red'> files of this type cannot be uploaded! </Font> ";
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 "<font color = 'red'> the file with the same name already exists! </A> ";
Exit;
}
If (! Move_uploaded_file ($ filename, $ destination ))
{
Echo "<font color = 'red'> An error occurred while moving the file! </A> ";
Exit;
}
$ Pinfo = pathinfo ($ destination );
$ Fname = $ pinfo [basename];
Echo "<font color = red> uploaded successfully, move the mouse to the address bar to automatically copy </font> <br> <table width = \ "348 \" cellspacing = \ "0 \" cellpadding = \ "5 \" border = \ "0 \ "class = \" table_decoration \ "align = \" center \ "> <tr> <td> <input type = \" checkbox \ "id = \" fmt \ "onclick = \ "select_format () \ "/> image UBB Code <br/> <div id = \" site \ "> <table border = \" 0 \ "> <tr> <td valign = \" top \ "> file address: </td> <input type = \ "text \" onclick = \ "sendtof (this. value) \ "onmouseover = \" oCopy (this) \ "style = font-size = 9pt; color: blue size = \" 44 \ "value = \" http: //". $ _ SERVER ['server _ name']. $ path_parts ["dirname"]. "/". $ destination_folder. $ fname. "\"/>
</Td> </tr> </table> </div> <div id = \ "sited \" style = \ "display: none \ "> <table border = \" 0 \ "> <tr> <td valign = \" top \ "> file address: </td> <input type = \ "text \" onclick = \ "sendtof (this. value) \ "onmouseover = \" oCopy (this) \ "style = font-size = 9pt; color: blue size = \ "44 \" value = \ "[img] http ://". $ _ SERVER ['server _ name']. $ path_parts ["dirname"]. "/". $ destination_folder. $ fname. "[/img] \"/> </td> </tr> </table> </div> </td> </tr> </table> ";
Echo "width:". $ image_size [0];
Echo "Length:". $ image_size [1];
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 ("<font color = 'red'> files of this type cannot be uploaded! </A> ");
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)
{
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 "<a href = \"". $ destination. "\" target = '_ blank'> Echo "alt = \" image preview: \ r file name :". $ fname. "\ r upload time :". date ('m/d/Y h: I '). "\" border = '0'> </a> ";
}
}
?>
</Center>
</Body>
</Html>

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.