《PHP教程:PHP實(shí)現(xiàn)接收二進(jìn)制流轉(zhuǎn)換成圖片的方法》要點(diǎn):
本文介紹了PHP教程:PHP實(shí)現(xiàn)接收二進(jìn)制流轉(zhuǎn)換成圖片的方法,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
本文實(shí)例講述了PHP實(shí)現(xiàn)接收二進(jìn)制流轉(zhuǎn)換成圖片的方法.分享給大家供大家參考,具體如下:PHP教程
這里實(shí)現(xiàn)php 接收二進(jìn)制流轉(zhuǎn)換成圖片,所使用的圖片類imageUpload.php如下:PHP教程
<?php /** * 圖片類 * @version 1.0 * * PHP默認(rèn)只識(shí)別application/x-www.form-urlencoded標(biāo)準(zhǔn)的數(shù)據(jù)類型. * 因此,對(duì)型如text/xml 或者 soap 或者 application/octet-stream 之類的內(nèi)容無法解析,如果用$_POST數(shù)組來接收就會(huì)失敗! * 故保留原型,交給$GLOBALS['HTTP_RAW_POST_DATA'] 來接收. * 另外還有一項(xiàng) php://input 也可以實(shí)現(xiàn)此這個(gè)功能 * php://input 允許讀取 POST 的原始數(shù)據(jù).和 $HTTP_RAW_POST_DATA 比起來,它給內(nèi)存帶來的壓力較小,并且不需要任何特殊的 php.ini 設(shè)置.php://input和 $HTTP_RAW_POST_DATA 不能用于 enctype="multipart/form-data". */ class imageUpload { const ROOT_PATH = './'; const FAIL_WRITE_DATA = 'Fail to write data'; //沒有數(shù)據(jù)流 const NO_STREAM_DATA = 'The post data is empty'; //圖片類型不正確 const NOT_CORRECT_TYPE = 'Not a correct image type'; //不能創(chuàng)建文件 const CAN_NOT_CREATE_FILE = 'Can not create file'; //上傳圖片名稱 public $image_name; //圖片保存名稱 public $save_name; //圖片保存路徑 public $save_dir; //目錄+圖片完整路徑 public $save_fullpath; /** * 構(gòu)造函數(shù) * @param String $save_name 保存圖片名稱 * @param String $save_dir 保存路徑名稱 */ public function __construct($save_name, $save_dir) { //set_error_handler ( $this->error_handler () ); //設(shè)置保存圖片名稱,若未設(shè)置,則隨機(jī)產(chǎn)生一個(gè)唯一文件名 $this->save_name = $save_name ? $save_name : md5 ( mt_rand (), uniqid () ); //設(shè)置保存圖片路徑,若未設(shè)置,則使用年/月/日格式進(jìn)行目錄存儲(chǔ) $this->save_dir = $save_dir ? self::ROOT_PATH .$save_dir : self::ROOT_PATH .date ( 'Y/m/d' ); //創(chuàng)建文件夾 @$this->create_dir ( $this->save_dir ); //設(shè)置目錄+圖片完整路徑 $this->save_fullpath = $this->save_dir . '/' . $this->save_name; } //兼容PHP4 public function image($save_name) { $this->__construct ( $save_name ); } public function stream2Image() { //二進(jìn)制數(shù)據(jù)流 $data = file_get_contents ( 'php://input' ) ? file_get_contents ( 'php://input' ) : gzuncompress ( $GLOBALS ['HTTP_RAW_POST_DATA'] ); //數(shù)據(jù)流不為空,則進(jìn)行保存操作 if (! empty ( $data )) { //創(chuàng)建并寫入數(shù)據(jù)流,然后保存文件 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 { //沒有接收到數(shù)據(jù)流 echo ( self::NO_STREAM_DATA ); } } /** * 創(chuàng)建文件夾 * @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; }*/ } ?>
PS:這里再為大家推薦幾款比較實(shí)用的圖片處理工具供大家參考使用:PHP教程
在線圖片轉(zhuǎn)換BASE64工具:
http://tools.jb51.net/transcoding/img2base64PHP教程
ICO圖標(biāo)在線生成工具:
http://tools.jb51.net/aideddesign/ico_imgPHP教程
在線Email郵箱圖標(biāo)制作工具:
http://tools.jb51.net/email/emaillogoPHP教程
在線圖片格式轉(zhuǎn)換(jpg/bmp/gif/png)工具:
http://tools.jb51.net/aideddesign/picextPHP教程
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》PHP教程
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助.PHP教程
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/2009.html