PHP implementation to receive binary transfer of images

Source: Internet
Author: User
This article mainly introduces the implementation of PHP to receive binary transfer to the image of the method, combined with examples of PHP files, directories and graphics operations related skills, the need for friends can refer to the following

Specific as follows:

Here to implement PHP to receive binary flow into the picture, the picture class used imageupload.php as follows:

<?php/** * Picture class * @version 1.0** PHP recognizes only application/x-www.form-urlencoded standard data types by default. * Therefore, the type such as text/xml or soap or application/octet-stream content can not be resolved, if the $_post array to receive will fail! * So keep the prototype and give it to $globals[' Http_raw_post_data ') to receive it. * There is also a php://input that can be implemented this feature * Php://input allows to read the original data of the POST. Compared to $HTTP _raw_post_data, it brings less pressure to memory and does not require any special php.ini settings. Php://input and $HTTP _raw_post_data cannot be used for enctype= "Multipart/form-data".  */class imageupload {Const Root_path = './';  Const FAIL_WRITE_DATA = ' FAIL to WRITE DATA ';  No data flow Const NO_STREAM_DATA = ' The post data is empty ';  The picture type is not correct const NOT_CORRECT_TYPE = ' Not a CORRECT image type ';  Cannot create file const Can_not_create_file = ' Can not ' create file ';  Upload image name public $image _name;  Picture save name public $save _name;  Picture save path public $save _dir;  Directory + picture full path public $save _fullpath; /** * constructor * @param string $save _name Save Picture name * @param string $save _dir Save path name */Public function __construct ($sav E_name, $save _dir) {//set_error_handler ($this->error_handler () ); Set Save picture name, if not set, randomly generate a unique file name $this->save_name = $save _name?    $save _name:md5 (Mt_rand (), uniqid ()); Set save picture Path, if not set, use year/month/day format for directory storage $this->save_dir = $save _dir?    Self::root_path. $save _dir:self::root_path. Date (' y/m/d ');    Create folder @ $this->create_dir ($this->save_dir); Set directory + picture full 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 ']); The data stream is not empty, then the save operation if (! empty ($data)) {//creates and writes a data stream, and then saves 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 {//did not receive the data flow echo (Self::no_stream_data); }}/** * Create folder * @param String $dirName Folder path name */Public Function Create_dir ($dirName, $recursive = 1, $mode =0777  ) {! Is_dir ($dirName) && mkdir ($dirName, $mode, $recursive); /** * Get picture information, return the picture's width, height, type, size, picture mine type * @param String $imageName Picture 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 {//not a valid picture return false;  }}/*private function Error_Handler ($a, $b) {echo $a, $b; }*/}?>

The above is the whole content of this article, I hope that everyone's study has helped.


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.