PHP image upload, can be implemented preview _php tutorial

Source: Internet
Author: User
Tags imagecopy imagejpeg
PHP Tutorial picture Upload, can be implemented preview

Header ("content-type:text/html; charset=gb2312 ");
$uptypes =array (' image/jpg ',//upload file type list
' Image/jpeg ',
' Image/png ',
' Image/pjpeg ',
' Image/gif ',
' Image/bmp ',
' Application/x-shockwave-flash ',
' Image/x-png ',
' Application/msword ',
' Audio/x-ms-wma ',
' Audio/mp3 ',
' Application/vnd.rn-realmedia ',
' Application/x-zip-compressed ',
' Application/octet-stream ');

$max _file_size=10000000; Upload file size limit, unit byte
$path _parts=pathinfo ($_server[' php_self '); Get current path
$destination _folder= "up/"; Upload file path
$watermark = 0; Whether to add watermark (1 is watermark, 0 is not watermark);
$watertype = 1; Watermark Type (1 for text, 2 for picture)
$waterposition = 2; The watermark position (1 is the lower left corner, 2 is the lower right corner, 3 is the upper left corner, 4 is the upper right corner, and 5 is centered);
$waterstring = "www.yinao.tk"; Watermark String
$waterimg = "Xplore.gif"; Watermark Picture
$imgpreview = 1; Whether to generate a preview map (1 is generated, 0 is not generated);
$imgpreviewsize =1/1; Thumbnail scale
?>


Image upload Storage






if ($_server[' request_method '] = = ' POST ')
{
if (!is_uploaded_file ($_files["Upfile"][tmp_name]))
Whether the file exists
{
echo "file does not exist! ";
Exit
}

$file = $_files["Upfile"];
if ($max _file_size < $file ["size"])
Check File size
{
echo "file is too big! ";
Exit
}

if (!in_array ($file ["type"], $uptypes))
Check file types
{
echo "Cannot upload this type of file! ";
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 "successfully uploaded, Mouse moved to the address bar to automatically copy

Picture UBB Code
File Address:
File Address:
";
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,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 ("Cannot upload this type of file! ");
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 "
Picture preview:
";
echo "echo" alt= "Picture preview: R filename:". $fname. " R upload time: ". Date (' m/d/y h:i ')." "border= ' 0 ' >";
}
}
?>




http://www.bkjia.com/PHPjc/444756.html www.bkjia.com true http://www.bkjia.com/PHPjc/444756.html techarticle php Tutorial picture Upload, can be implemented preview? PHP header (content-type:text/html; charset=gb2312); $uptypes =array (' image/jpg ',//Upload file type list ' Image/jpeg ', ' image/png ', ' Image ...

  • 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.