Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php5.4以下模拟getimagesizefromstring函数

php5.4以下模拟getimagesizefromstring函数

发布时间:2017-08-11   编辑:www.jquerycn.cn
本文介绍下,在php5.4以下的开发环境中,模拟getimagesizefromstring函数的方法,有需要的朋友参考学习下。

小贴士:html5上传图片时可用php://input的数据流来操作.

例如:
 

复制代码 代码示例:
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().
 

复制代码 代码示例:
<?php
$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);
 

成功转换格式。

这种方法不错,但一次base64看起来不是那么爽。

可以考虑更简单的方法,stream_register_wrapper — 注册一个用 PHP 类实现的 URL 封装协议。

代码:
 

复制代码 代码示例:

<?php
 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_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);

没有意外,因为这个函数支持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;
 }

您可能感兴趣的文章:
php5.4以下模拟getimagesizefromstring函数
php GD库函数getimagesize()的用法
PHP getimagesizefromstring – 获取图片信息函数
解决PHP5.4环境下ECSHOP网站报错的方法
php提示Redefining already defined constructor for class 错误
PHP fastcgi模式运行的PHP上传文件MaxRequestLen错误解决
PHP CURL获取cookies模拟登录的方法介绍
PHP 图像处理
php各版本区别
php5.4 织梦dedecms后台登录空白问题

[关闭]