How to Convert binary data into images in PHP

Source: Internet
Author: User
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!

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.