龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php5.4以下模拟getimagesizefromstring

时间:2014-07-22 14:49来源: 作者: 点击:
分享到:
span style=font-family:Verdana, Arial, Helvetica, sans-serif;font-size:14px;line-height:20px;background-color:#FFFFFF;html5上传图片时可用php://input的数据流来运作.br / span style=font-family:Verdana, Arial, Helvetica, sans-seri
html5上传图片时可用php://input的数据流来运作.
如果要得到图片信息,那只有getimagesize($filename),再打一次刚关闭的文件.
php里有一个getimagesizefromstring,但是>=php5.4,但目前还没服务器还没用上这么高版本的php.
作为一个效率癖,这是不被允许的,有直接操纵数据流的方法吗?
当然, stream_register_wrapper — 注册一个用 PHP 类实现的 URL 封装协议

http://www.cnblogs.com/wc1217/archive/2013/04/02/2995961.html

class getImgStream{
 
     private $imgStream;
     private $position;
 
     function stream_open($path, $mode, $options, &$opened_path){
         $url = parse_url($path);
         $this->imgStream = $GLOBALS[$url["host"]];
         $this->position = 0;
         return true;
     }
 
     function stream_read($count){
         $ret = substr($this->imgStream, $this->position, $count);
         $this->position += strlen($ret);
         return $ret;
     }
 
     function stream_stat(){
         //maxmemory: 5 * 1024 * 1024;
         $fp = fopen("php://temp/maxmemory:5242880", 'r+');
         fwrite($fp, $this->imgStream);
         $fstat = fstat($fp);
         fclose($fp);
         return $fstat;
     }
 
     function stream_eof(){
         return $this->position >= strlen($this->imgStream);
     }
 
     function stream_tell(){
         return $this->position;
     }
        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;
        }

     function stream_close(){
         unset($this->imgStream, $this->position);
     }
 
 }
 
 $file_path = 'http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif';
 $stream = file_get_contents($file_path);
 
 stream_wrapper_register("image", "getImgStream");
 
 print_r(getimagesize('image://stream'));
 $new_img = imagecreatefromgif('image://stream');
 //或者
 $new_img = imagecreatefromstring($stream);
 print_r($new_img);
 imagejpeg($new_img, 'E:\WEB\uploads\test.jpg', 100);

2. [代码]方法二     跳至 [1] [2] [全屏预览]

$file_path = 'http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif';
$stream = file_get_contents($file_path);
print_r(getimagesize("data://text/plain;base64," . base64_encode($stream)));
$new_img = imagecreatefromgif("data://text/plain;base64," . base64_encode($stream));
//或者
$new_img = imagecreatefromstring($stream);
print_r($new_img);
imagejpeg($new_img, 'E:\WEB\uploads\test.jpg', 100);
精彩图集

赞助商链接