html5上传图片时可用php://input的数据流来运作.
例如:
1 if($in = fopen('php://input', "rb")) 2 while($buff = fread($in, 4096)) 3 fwrite('e:\\1.jpg', $buff);
如果要得到图片信息,那只有getimagesize($filename),再打一次刚关闭的文件.
php里有一个getimagesizefromstring,但是要>=php5.4,但目前还没服务器还没用上这么高版本的php.
作为一个效率癖,这是不被允许的,有直接操纵数据流的方法吗?答案是有, php官方手册上有 "支持的协议和封装协议",其中的data://大家应该都很熟悉.
我们可以利用它完成在数据流中直接操纵图片(水印,缩略图之类),以下代码为了方便我就直接file_get_contents().
1 $file_path = 'http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif'; 2 $stream = file_get_contents($file_path); 3 print_r(getimagesize("data://text/plain;base64," . base64_encode($stream))); 4 $new_img = imagecreatefromgif("data://text/plain;base64," . base64_encode($stream));//或者$new_img = imagecreatefromstring($stream);
5 print_r($new_img);
6 imagejpeg($new_img, 'E:\WEB\uploads\test.jpg', 100);
成功转换格式了.
这个方法固然好,但一次base64看起来不是那么爽,还有更简单的吗?当然, stream_register_wrapper — 注册一个用 PHP 类实现的 URL 封装协议
我们看看代码:
1 class getImgStream{ 2 3 private $imgStream; 4 private $position; 5 6 function stream_open($path, $mode, $options, &$opened_path){ 7 $url = parse_url($path); 8 $this->imgStream = $GLOBALS[$url["host"]]; 9 $this->position = 0; 10 return true; 11 } 12 13 function stream_read($count){ 14 $ret = substr($this->imgStream, $this->position, $count); 15 $this->position += strlen($ret); 16 return $ret; 17 } 18 19 function stream_stat(){ 20 //maxmemory: 5 * 1024 * 1024; 21 $fp = fopen("php://temp/maxmemory:5242880", 'r+'); 22 fwrite($fp, $this->imgStream); 23 $fstat = fstat($fp); 24 fclose($fp); 25 return $fstat; 26 } 27 28 function stream_eof(){ 29 return $this->position >= strlen($this->imgStream); 30 } 31 32 function stream_tell(){ 33 return $this->position; 34 } 35 36 function stream_close(){ 37 unset($this->imgStream, $this->position); 38 } 39 40 } 41 42 $file_path = 'http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif'; 43 $stream = file_get_contents($file_path); 44 45 stream_wrapper_register("image", "getImgStream"); 46 47 print_r(getimagesize('image://stream')); 48 $new_img = imagecreatefromgif('image://stream');//或者$new_img = imagecreatefromstring($stream); 49 print_r($new_img); 50 imagejpeg($new_img, 'E:\WEB\uploads\test.jpg', 100);
没有意外,因为这个函数支持PHP 4 >= 4.3.0, PHP 5,这样才像效率癖干的事,但代码看起来多了好多,而且小小的图片.也看不出多大效率.
经测试,同一张本地图片(300x800),方法一平均43ms,方法二平均39ms
更正,如果报getimagesize的stream does not support seeking,可能要在wrapper中加入seek的操作,加入以下代码
function stream_seek($offset, $whence){ $l = strlen($this->imgStream); $p = &$this->position; switch($whence){ case SEEK_SET: $newPos = $offset; break; case SEEK_CUR: $newPos = $p + $offset; break; case SEEK_END: $newPos = $l + $offset; break; default: return false; } $ret = ($newPos >= 0 && $newPos <= $l); if($ret) $p = $newPos; return $ret; }