Php: Video photo Upload Avatar function instance code _ PHP Tutorial

Source: Internet
Author: User
Php implements the instance code of the video photo Upload Avatar function. If you want to implement video photography in php, you need to use the flash plug-in. after taking a photo from flash, we can use php's $ GLOBALS [HTTP_RAW_POST_DATA] to accept the data, save the graph. if you want to take a video in php, you need to use the flash plug-in to take a photo. then, we can use php's $ GLOBALS ['http _ RAW_POST_DATA '] to accept the data, save it as an image. I will introduce it below.

How can I use my mobile phone to take a photo and upload an avatar? The reason is simple: data transmission. First, the mobile phone transmits the photo information. this is not post transmission or get function transmission,
This other data format is passed using $ GLOBALS ['http _ RAW_POST_DATA ']. this system function is similar to post, however, $ GLOBALS ['http _ RAW_POST_DATA '] supports a wider range of data formats. for detailed differences, see Baidu Google.
The design process is:
$ GLOBALS ['http _ RAW_POST_DATA '] transmits the photo data stream (binary)-> open a blank image-> write the data stream into the blank image-determine whether it is a valid image-complete.
Note that the binary stream of the photo already contains the size, format, and other attributes of the photo. The time is to transfer the mobile phone image information to the web page.

The mobile phone uses flash to cut the photo size. we all know that images and videos are composed of binary streams. since images can be uploaded, can videos be uploaded? Is it the same principle as taking a photo on a mobile phone and uploading an avatar? You can study it yourself.


Directly add code

The code is as follows:

/*
* Upload an image
* @ Parameter; user ID
* @ Date: 2011-8-15
* @ Author: cy
**/
Function UpImg ($ subdir ){
// Set the storage path
If (! Empty ($ subdir )){
$ Dirnow = getcwd ();
$ Folder = $ dirnow. "/www.bkjia.com/". $ subdir; // folder where to save images
If (! Is_dir ($ folder) mkdir ($ folder );

// Image name
$ Datenow = date ('ymmd ');
$ Timenow = time ();
$ Image = comment ';

// Whether the image already exists
$ Check = $ folder. '/'. $ image;
If (file_exists ($ check )){
Unlink ($ check );
} Else {
$ Png = file_get_contents ('php: // input ')? File_get_contents ('php: // input'): gzuncompress ($ GLOBALS ['http _ RAW_POST_DATA ']); // Obtain the binary raw data from post
If (! Empty ($ png )){
$ File = @ fopen ($ folder. "/". $ image, "w ");
Fwrite ($ file, $ png); // write to hzhuti.com
Fclose ($ file); // Close
// Orignal image location
$ Write_image = $ folder. '/'. $ image;

} Else {
$ Msg = "no data flow ";
}
If (getimageInfo ($ write_image )){
$ Msg = "Upload successful ";
} Else {$ msg = "incorrect image format ";}
}
} Else {
$ Msg = "parameter error ";
}
Return $ msg;
}

/*
* Check whether the image is valid.
* @ Parameter; file name
* @ Date: 2011-8-15
* @ Author: cy
**/
Function getimageInfo ($ imageName = ''){
$ ImageInfo = getimagesize ($ imageName );
If ($ imageInfo! = False ){
$ ImageType = strtolower (substr (image_type_to_extension ($ imageInfo [2]), 1 ));
// $ ImageSize = filesize ($ imageInfo );
Return $ info = array ('width' => $ imageInfo [0], 'height' => $ imageInfo [1], 'type' => $ imageType, 'Mine '=> $ imageInfo ['Mine']);
} Else {
// The image is not valid in php100.com.
Return false;
}

}

When xml-rpc is used, the server obtains client data mainly through the php input stream input instead of the $ _ POST array.

Based on the above probes, we can summarize the following:
1. when the value of Content-Type is application/x-www-form-urlencoded, php will fill in the corresponding data of the http request body to the array $ _ POST, the data filled in the $ _ POST array is the result of urldecode () parsing. (In fact, in addition to the Content-Type, there is also multipart/form-data indicating that the data is form data. we will introduce it later)
2, php: // input data, as long as the Content-Type is not multipart/form-data (this condition will be described later ). Php: // the input data is consistent with the part of the http entity body. The consistent data Length is specified by Content-Length.
3. only when the Content-Type is application/x-www-form-urlencoded and the submission method is POST, $ _ POST data and php: // the input data is "consistent" (with quotation marks, indicating that the formats are inconsistent and the content is consistent. In other cases, they are inconsistent.
4. php: // input cannot read $ _ GET data. The reason is that the $ _ GET data is written in the PATH field of the http request header as query_path, rather than in the http request body.

This also helps us understand why the xml_rpc server reads data through file_get_contents ('php: // input', 'r '). Instead of reading from $ _ POST, it is because the xml_rpc data Type is xml, and its Content-Type is text/xml.


Accept [HTTP_RAW_POST_DATA] accept the data and save it as a graph...

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.