This article mainly introduces how PHP can convert binary data into images, and analyzes PHP file, directory, and graphic operations related skills in the form of examples, for more information, see the following example. We will share this with you for your reference. The details are as follows:
Here, php is used to convert binary data to images. The Image class imageUpload. php is as follows:
Error_handler (); // Set the name of the saved Image. if not set, a unique file name $ this-> save_name = $ save_name? $ Save_name: md5 (mt_rand (), uniqid (); // you can specify the path for saving the image. if not, use the year, month, or day format for Directory Storage $ this-> save_dir = $ save_dir? Self: ROOT_PATH. $ save_dir: self: ROOT_PATH. date ('Y/m/D'); // create a folder @ $ this-> create_dir ($ this-> save_dir ); // Set the Directory + complete Image path $ this-> save_fullpath = $ this-> save_dir. '/'. $ this-> save_name;} // Compatible with PHP4 public function image ($ save_name) {$ this->__ construct ($ save_name);} public function stream2Image () {// binary data stream $ data = file_get_contents ('php: // input ')? File_get_contents ('php: // input'): gzuncompress ($ GLOBALS ['http _ RAW_POST_DATA ']); // if (! Empty ($ data) {// create and write data streams, and then save the file if (@ $ fp = fopen ($ this-> save_fullpath, 'W + ')) {fwrite ($ fp, $ data); fclose ($ fp); $ baseurl = "http ://". $ _ SERVER ["SERVER_NAME"]. ":". $ _ SERVER ["SERVER_PORT"]. dirname ($ _ SERVER ["SCRIPT_NAME"]). '/'. $ this-> save_name; if ($ this-> getimageInfo ($ baseurl) {echo $ baseurl;} else {echo (self: NOT_CORRECT_TYPE );}} else {} else {// not received Data stream echo (self: NO_STREAM_DATA) ;}/ *** create a folder * @ param String $ dirName folder pathname */public function create_dir ($ dirName, $ recursive = 1, $ mode = 0777 ){! Is_dir ($ dirName) & mkdir ($ dirName, $ mode, $ recursive);}/*** get image information, returns the image width, height, type, size, and image mine type * @ param String $ imageName image name */public 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, 'size' => $ imageSize, 'Mine '=> $ imageInfo ['Mine']);} else {// invalid image return false ;}} /* private function error_handler ($ a, $ B) {echo $ a, $ B;} */}?>
I hope this article will help you with PHP programming.
For more articles about how PHP can convert binary data into images, refer to the PHP Chinese website!