Maison  >  Article  >  développement back-end  >  php5.4以下模拟getimagesizefromstring函数

php5.4以下模拟getimagesizefromstring函数

WBOY
WBOYoriginal
2016-07-25 08:55:09790parcourir
  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官方手册上有 "支持的协议和封装协议",其中的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. private $imgStream;
  3. private $position;
  4. function stream_open($path, $mode, $options, &$opened_path){
  5. $url = parse_url($path);
  6. $this->imgStream = $GLOBALS[$url["host"]];
  7. $this->position = 0;
  8. return true;
  9. }
  10. function stream_read($count){

  11. $ret = substr($this->imgStream, $this->position, $count);
  12. $this->position += strlen($ret);
  13. return $ret;
  14. }
  15. function stream_stat(){
  16. //maxmemory: 5 * 1024 * 1024;
  17. $fp = fopen("php://temp/maxmemory:5242880", 'r+');
  18. fwrite($fp, $this->imgStream);
  19. $fstat = fstat($fp);
  20. fclose($fp);
  21. return $fstat;
  22. }
  23. function stream_eof(){

  24. return $this->position >= strlen($this->imgStream);
  25. }
  26. function stream_tell(){

  27. return $this->position;
  28. }
  29. function stream_close(){
  30. unset($this->imgStream, $this->position);
  31. }
  32. }
  33. $file_path = 'http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif';

  34. $stream = file_get_contents($file_path);
  35. stream_wrapper_register("image", "getImgStream");

  36. print_r(getimagesize('image://stream'));

  37. $new_img = imagecreatefromgif('image://stream');//或者$new_img = imagecreatefromstring($stream);
  38. print_r($new_img);
  39. 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的操作,添加代码:

  1. function stream_seek($offset, $whence){
  2. $l = strlen($this->imgStream);
  3. $p = &$this->position;
  4. switch($whence){
  5. case SEEK_SET: $newPos = $offset;
  6. break;
  7. case SEEK_CUR: $newPos = $p + $offset;
  8. break;
  9. case SEEK_END: $newPos = $l + $offset;
  10. break;
  11. default: return false;
  12. }
  13. $ret = ($newPos >= 0 && $newPos if($ret)
  14. $p = $newPos;
  15. return $ret;
  16. }
复制代码


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn