php5.4 the following simulated getimagesizefromstring function

Source: Internet
Author: User
Tags imagejpeg
    1. if ($in = fopen (' Php://input ', "RB"))
    2. while ($buff = Fread ($in, 4096))
    3. Fwrite (' e:\\1.jpg ', $buff);
Copy Code

To get the picture information, only getimagesize ($filename), and then hit the file just closed. PHP has a getimagesizefromstring, but requires >=php5.4.

Is there a way to manipulate the data flow directly? The answer is yes, there is a "supported Protocol and encapsulation Protocol" in the official PHP manual, and the data://should be familiar to everyone.

It can be used to manipulate images (watermarks, thumbnails, and the like) directly in the data stream. The following code is directly file_get_contents () for convenience.

    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));//or $new_img = Imagecreatefromstring ($stream);
    5. Print_r ($new _img);
    6. Imagejpeg ($new _img, ' E:\WEB\uploads\test.jpg ', 100);
Copy Code

The format was converted successfully.

That's a good idea, but a base64 doesn't seem so cool at once.

Consider a simpler approach, stream_register_wrapper-registering a URL wrapper protocol implemented with PHP classes.

Code:

  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 ');//or $new_img = Imagecreatefromstring ($stream);
  38. Print_r ($new _img);
  39. Imagejpeg ($new _img, ' E:\WEB\uploads\test.jpg ', 100);

Copy Code

No surprises, because this function supports PHP 4 >= 4.3.0, PHP 5.

Tested, the same local picture (300x800), method one average 43ms, method two average 39ms

Correction: If the getimagesize stream does not the support seeking, you may want to add the seek operation to the wrapper by adding the code:

    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 <= $l);
    14. if ($ret)
    15. $p = $newPos;
    16. return $ret;
    17. }
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.