Generate a thumbnail of the file stream and distribute it to fastDFS
20, 'H' => 20), array ('W' => 30, 'H' => 30 ),); private $ allow_type = array ('image/png '=> 'PNG', 'image/jpeg '=> 'jpg ', 'image/jpg '=> 'jpg ',); /*** receive file upload in binary stream mode * @ param $ buff binary stream * @ return bool | string returns the access address of the source file, false */public function uploadImageByBuffOne ($ buff) {if (! $ Buff | ($ buff> $ this-> max_size) {// when the file is empty or exceeds the upper limit of Upload, return false ;} $ filename = $ this-> getFileNameFromBuff ($ buff); // Generate the written file name if (! $ Filename) {return false;} $ file_path = $ this-> saveBuffToDisk ($ filename, $ buff); // Save the object if (! $ File_path) {return false;} $ files = $ this-> createThumb ($ file_path); // return $ this-> postImageToFastDFSByFileName ($ files) of the generated file );} /*** fastDFS * @ param $ files Image array * @ return bool | string returns the source file access address, false */private function postImageToFastDFSByFileName ($ files) {$ source = $ files ['source']; unset ($ files ['source']); $ tracker = fastdfs_tracker_get_connection (); if (! Fastdfs_active_test ($ tracker) {error_log ("errno :". fastdfs_get_last_error_no (). ", error info :". fastdfs_get_last_error_info (); return false ;}$ storage = fastdfs_tracker_query_storage_store (); if (! $ Storage) {error_log ("errno :". fastdfs_get_last_error_no (). ", error info :". fastdfs_get_last_error_info (); return false;} // Save the source file $ original_uploaded_info = fastdfs_storage_upload_by_filename ($ source, null, array (), null, $ tracker, $ storage ); if ($ original_uploaded_info) {unlink ($ source); // delete the source file $ group_name = $ original_uploaded_info ['group _ name']; $ remote_filename = $ original_uploaded_ I Nfo ['filename']; foreach ($ files as $ size => $ path) {// upload a file thumbnail as a file $ thumbnail_info = fastdfs_storage_upload_slave_by_filename ($ path, $ group_name, $ remote_filename ,'_'. $ size); unlink ($ path); // delete thumbnail} return '/'. $ group_name. '/'. $ remote_filename;} else {error_log ("errno :". fastdfs_get_last_error_no (). ", error info :". fastdfs_get_last_error_info (); return false ;}}/*** according to the file Stream the mimes information of the file and generate the file name to be written * @ param $ buff * @ return bool | string */private function getFileNameFromBuff ($ buff) {// Obtain the file type based on the mimes information of the binary stream // filter out information that does not conform to the mimes header $ imagick = new \ Imagick (); $ imagick-> readImageBlob ($ buff); $ mimes = $ imagick-> getImageMimeType (); if (isset ($ this-> allow_type [$ mimes]) = false) {return false;} $ file_name = md5 ($ buff ). '. '. $ this-> allow_type [$ mimes]; return $ file_name ;} /*** Write the binary stream to the disk file * @ param $ filename the file name to be written * @ param $ binary stream of the buff content * @ return bool | string */private function saveBuffToDisk ($ filename, $ buff) {$ path = $ this-> image_dir. '/'. $ filename; $ fp = fopen ($ path, 'w'); fwrite ($ fp, $ buff); fclose ($ fp); return is_file ($ path )? $ Path: false ;} /*** create thumbnails of different sizes based on the configuration file * @ param $ file_path * @ return array ('width X high' => 'path/filename ') */private function createThumb ($ file_path) {$ ret ['source'] = $ file_path; // source file path if (! Is_file ($ file_path) {// if the thumbnail size is not configured or the file does not exist, exit return $ ret;} foreach ($ this-> thumb_size as $ size) {$ path = $ this-> createThumbBySize ($ file_path, $ size ['w'], $ size ['H']); $ key = $ size ['w']. 'X '. $ size ['H']; $ ret [$ key] = $ path;} return $ ret ;} /*** generate a thumbnail based on the parameter * @ param $ file_path file path * @ param $ weight width of the thumbnail * @ param $ high Thumbnail height * @ param int $ quality default 80 * @ return string path for saving the thumbnail */private function createThumbBySize ($ file_path, $ weight, $ high, $ quality = 80) {$ image = new \ Think \ Image (2); #1 = GD, 2 = Imagick $ ext = pathinfo ($ file_path, PATHINFO_EXTENSION); // get the file suffix $ image-> open ($ file_path); $ thumb = uniqid (). '. '. $ ext; $ path = $ this-> image_dir. '/'. $ thumb; $ image-> thumb ($ weight, $ high)-> save ($ path, $ image-> type (), $ quality); return $ path ;}}