- <?php
- /**
- * 圖片類
- * @author Haroldphp@163.com
- * @version 1.0
- *
PHP默認只識別application/x-www.form-urlencoded標准的數據類型。
因此,對型如text/xml 或者 soap 或者 application/octet-stream 之類的內容無法解析,如果用$_POST數組來接收就會失敗!
故保留原型,交給$GLOBALS['HTTP_RAW_POST_DATA'] 來接收。另外還有一項 php://input 也可以實現此這個功能
php://input 允許讀取 POST 的原始數據。和 $HTTP_RAW_POST_DATA 比起來,它給內存帶來的壓力較小,並且不需要任何特殊的 php.ini 設置。php://input和 $HTTP_RAW_POST_DATA 不能用於 enctype="multipart/form-data"。
- */
- class image {
- const ROOT_PATH = './';
- const FAIL_WRITE_DATA = 'Fail to write data';
- //沒有數據流
- const NO_STREAM_DATA = 'The post data is empty';
- //圖片類型不正確
- const NOT_CORRECT_TYPE = 'Not a correct image type';
- //不能創建文件
- const CAN_NOT_CREATE_FILE = 'Can not create file';
- //上傳圖片名稱
- public $image_name;
- //圖片保存名稱
- public $save_name;
- //圖片保存路徑
- public $save_dir;
- //目錄+圖片完整路徑
- public $save_fullpath;
- /**
- * 構造函數
- * @param String $save_name 保存圖片名稱
- * @param String $save_dir 保存路徑名稱
- */
- public function __construct($save_name, $save_dir) {
- //set_error_handler ( $this->error_handler () );
- //設置保存圖片名稱,若未設置,則隨機產生一個唯一文件名
- $this->save_name = $save_name ? $save_name : md5 ( mt_rand (), uniqid () );
- //設置保存圖片路徑,若未設置,則使用年/月/日格式進行目錄存儲
- $this->save_dir = $save_dir ? self::ROOT_PATH .$save_dir : self::ROOT_PATH .date ( 'Y/m/d' );
- //創建文件夾
- @$this->create_dir ( $this->save_dir );
- //設置目錄+圖片完整路徑
- $this->save_fullpath = $this->save_dir . '/' . $this->save_name;
- }
- //兼容PHP4
- public function image($save_name) {
- $this->__construct ( $save_name );
- }
- public function stream2Image() {
- //二進制數據流
- $data = file_get_contents ( 'php://input' ) ? file_get_contents ( 'php://input' ) : gzuncompress ( $GLOBALS ['HTTP_RAW_POST_DATA'] );
- //數據流不為空,則進行保存操作
- if (! emptyempty ( $data )) {
- //創建並寫入數據流,然后保存文件
- 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 {
- //沒有接收到數據流
- echo ( self::NO_STREAM_DATA );
- }
- }
- /**
- * 創建文件夾
- * @param String $dirName 文件夾路徑名
- */
- public function create_dir($dirName, $recursive = 1,$mode=0777) {
- ! is_dir ( $dirName ) && mkdir ( $dirName,$mode,$recursive );
- }
- /**
- * 獲取圖片信息,返回圖片的寬、高、類型、大小、圖片mine類型
- * @param String $imageName 圖片名稱
- */
- 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 {
- //不是合法的圖片
- return false;
- }
- }
- /*private function error_handler($a, $b) {
- echo $a, $b;
- }*/
- }