portraits are often used on websites. Many websites use FLSH to generate images. Today, we can see that flash is doing well. So we started to study its implementation principles. The following is an analysis process, if you are not interested, skip this step and download the source code package at the end (the test is passed on the Shenzhou database of Sha Jia). During the test, upload avatar_test to the root directory of the website, if not, most directories do not have the write permission. Enable the write permission for the three subdirectories starting with Avatar. in particular, avatareditor.swf was developed by yun.sohu.com and is copyrighted by Sohu. reprinted in this article, please indicate from darkangle.cnblogs.com Thank you
to analyze how to use it, first call the Code and use firebug to find the call code for uploading images and cameras respectively, probably like this:
input several parameters to the flash file, the specific meaning is unclear for the moment. Let's analyze it slowly.
Find avatareditor.swf from the IE saved file, and use sothink SWF decompiler (which can be found on the special edition online, I used version 5.0) to decomcode it. Although I have never done flash before, I have read the script in it, it is not too difficult. Find two key scripts: avatareditor. as and binarytransfer. as, where binarytransfer is responsible for transmitting data to the specified address in binary format.
The preceding parameters: photourl is the URL of the original image. Whether it is captured by a camera or uploaded, the server must return the address of the original image for editing by avatareditor, type = photo is the upload method or the image in the photo album with the Avatar. when type = camera, the camera is used. do is the script address for saving the photo taken by the camera. Note that parameters need to be added later (because the splicing address is required). This is not required in this example, so it is replaced by a meaningless Radom parameter, otherwise, the last request address is incorrect.
Icon. Do is the address for saving the thumbnail. photoid is the ID of the original image.
To better understand the post data process, you can install a fiddler to monitor the post address and return value.
After the image is saved, some JSON data is returned. Based on the data, avatareditor determines whether the upload is successful or fails, and calls an external JS interface to notify the browser.
By checking the fiddler and analyzing the binarytransfer code, avatareditor will post the generated JPG to the server in binary mode. In this way, there are some differences in the receiving method, instead of the traditional method of receiving form upload files, we found that we can use file_get_contents ("PHP: // input") to obtain the uploaded binary data, and then use file_put_contents (sd_root. '. /'. $ new_avatar_path, file_get_contents ("PHP: // input"); you can write the contents of the directory file. The problem of saving the file here is solved, and some necessary data is returned to avatareditor in JSON format.
I have added detailed comments to the source code. Please read them carefully.
Package and download source code
:
The files I extracted are for study only.