Share: PHP Upload the image code

Source: Internet
Author: User
Tags imagecopy imagejpeg php file upload
Transferred from: http://www.jbxue.com/article/6379.html

PHP upload images of the code, very simple, to achieve the basic file type, file size detection, and achieve the basic watermark and thumbnail function, more suitable for beginners friends reference.

The upload page is merged with the processing page, the file name is test_upload_pic.php and the complete code is as follows:

Copy Code code example:

!--? php
/*
* Parameter Description
* $max _file_size: Upload file size limit, Unit byte
* $destination _folder: Upload file path
* $wate Rmark: Whether additional watermark (1 is watermark, the other is not watermark);
* http://www.jbxue.com
* Instructions for use:
* 1. The "Extension=php_gd2.dll" line in the php.ini file is removed, 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/png ',
' image/jpeg ',
' image/pjpeg ',
' Image/gif ',
' image/bmp ',
' image/x-png '
);
$max _file_size = 20000000;//upload file size limit, unit byte
$ Destination_folder = ' uploadimg/'; Upload file path
$watermark = 1;//Whether additional watermark (1 is watermark, the other is no watermark);
$watertype = 1;//watermark type (1 text, 2 as picture)
$waterposition = 1;//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 = "http://www.jbxue.com/";//watermark string
$waterimg = "xplore.gif";//watermark picture
$imgpreview = 1;//whether to generate a preview map (1 For generation, others are not generated);
$imgpreviewsize = 1/2;//thumbnail scale
?


zwell picture uploader



if ($_server[' request_method '] = = ' POST ') {
Determine if there is an upload file

if (Is_uploaded_file ($_files[' upfile ' [' tmp_name '])) {
$upfile = $_files[' upfile ');
Print_r ($_files[' upfile ');
$name = $upfilep [' name ']; Filename
$type = $upfile [' type ']; File type
$size = $upfile [' Size ']; File size
$tmp _name = $upfile [' Tmp_name ']; Temporary files
$error = $upfile [' ERROR ']; Cause of error

if ($max _file_size < $size) {//Determine the size of the file
Echo ' upload file is too large ';
Exit ();
}

if (!in_arrar ($type, $uptypes)) {//Determine the type of file
echo ' upload file type does not match '. $type;
Exit ();
}

if (!file_exists ($destination _folder)) {
mkdir ($destination _folder);
}

if (file_exists ("upload/". $_files["File" ["Name"])) {
echo $_files["File" ["Name"]. "already exists.";
} else {
Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/". $_files["File" ["name"]);
echo "Stored in:". " Upload/". $_files["File" ["Name"];
}

$pinfo = PathInfo ($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 ($tmp _name, $destination)) {
echo "Error moving File";
Exit ();
}

$pinfo = PathInfo ($destination);
$fname = $pinfo [basename];
echo "has been successfully uploaded
File name: ". $destination _folder. $fname. "
";
echo "width:". $image _size[0];
echo "Length:". $image _size[1];
echo "
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]-$image _size[1], $white);

Switch ($watertype) {
Case 1://Add watermark String

Imagestring ($nimage, 2, 3, $image _size[1]-$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 file name:". $destination. "\ r upload time: \" > ";
}
}
}
?>

Articles you may be interested in:
PHP Multi-image Upload simple example (graphic)
PHP File Upload encountered an issue (Move_uploaded_file)
PHP common form Multi-file upload code
Learn how PHP files are uploaded
Examples of PHP Simple file uploads
PHP to determine the file type of file upload several methods
A php upload download file source code
A handy php file upload processing class
PHP code to upload multiple files
PHP three ways to upload multiple files
The implementation of PHP upload image function

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