Phpob_start () implements the variable program for storing images

Source: Internet
Author: User
Tags imagejpeg
After processing images with the phpGD library, you can only use imagejpeg () this is often not required for output or writing to a file. for example, to store images in a database, you need to write the images into variables and save them. use ob_start () to enable cache ob_get_contents ()

After processing images with the php GD library, you can only use imagejpeg () this is often not required for output or writing to a file. for example, to store images in a database, you need to write the images into variables and save them. use ob_start () to enable cache ob_get_contents () get the cache to write the image to the variable

The instance code is as follows:

  1. $ ImgPath = "Image address ";
  2. // Obtain image information $ imgPath can be a remote address
  3. List ($ srcWidth, $ srcHeight, $ type) = getimagesize ($ imgPath );
  4. ...
  5. Switch ($ type ){
  6. Case 1: $ imgCreate = 'imagecreatefromgif'; break;
  7. Case 2: $ imgCreate = 'imagecreatefromjpeg '; break;
  8. Case 3: $ imgCreate = 'imagecreatefrompng '; break;
  9. Default: return false;
  10. }
  11. $ Orig = $ imgCreate ($ imgPath );
  12. ...
  13. // Enable cache
  14. Ob_start ();
  15. // Generate an image
  16. Switch ($ type)
  17. {
  18. Case 1: imagegif ($ orig); break;
  19. Case 2: imagejpeg ($ orig); break; // best quality
  20. Case 3: imagepng ($ orig); break; // no compression
  21. Default: echo ''; break;
  22. }
  23. // Save the image to a variable
  24. $ ImageCode = ob_get_contents ();
  25. Ob_end_clean ();

If you do not want to save the image as a variable, it will be a waste of resources. this is also a fun test.

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.