=php5.4。有直接操纵数据流的方法吗?答案是有, php官方手册上有 "支持的协议和封装协议",其中的data://大家应该都很熟悉"> 跳转到主内容
极星编程网:以代码为星,赴技术山海!

php5.4以下模拟getimagesizefromstring函数

if($in = fopen('php://input', "rb"))while($buff = fread($in, 4096))fwrite('e:\1.jpg', $buff);复制代码要得到图片信息,那只有getimagesize($filename),再打一次刚关闭的文件。

php中有一个getimagesizefromstring,但是要求>=php5.4。有直接操纵数据流的方法吗?答案是有, php官方手册上有 "支持的协议和封装协议",其中的data://大家应该都很熟悉.可以利用它完成在数据流中直接操纵图片(水印,缩略图之类)。

以下代码为了方便,就直接file_get_contents().$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:WEBuploads est.jpg', 100);复制代码成功转换格式。这种方法不错,但一次base64看起来不是那么爽。可以考虑更简单的方法,stream_register_wrapper — 注册一个用 PHP 类实现的 URL 封装协议。代码: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;}立即学习“PHP免费学习笔记(深入)”;function stream_eof(){return $this->position >= strlen($this->imgStream);}function stream_tell(){return $this->position;}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:WEBuploads est.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 if($ret)$p = $newPos;return $ret;}复制代码

相关文章