Php uses ob_start () to save images to variables,

Source: Internet
Author: User
Tags imagejpeg

Php uses ob_start () to save images to variables,

This example describes how php uses ob_start () to store images into variables. Share it with you for your reference. The specific implementation method is as follows:

After processing the image with the GD library of php, you can only output the image with imagejpeg () or write it into a file. This is often not required. For example, if you want to store images to a database, you need to write the images to the variables and save them. You can use ob_start () to enable the cache ob_get_contents () to obtain the cache and write the images to the variables.
Copy codeThe Code is as follows: <? Php
$ ImgPath = "image address ";
// Obtain image information $ imgPath can be a remote address
List ($ srcWidth, $ srcHeight, $ type) = getimagesize ($ imgPath );
...
Switch ($ type ){
Case 1: $ imgCreate = 'imagecreatefromgif'; break;
Case 2: $ imgCreate = 'imagecreatefromjpeg '; break;
Case 3: $ imgCreate = 'imagecreatefrompng '; break;
Default: return false;
}
$ Orig = $ imgCreate ($ imgPath );
...
// Enable Cache
Ob_start ();
// Generate an image
Switch ($ type)
{
Case 1: imagegif ($ orig); break;
Case 2: imagejpeg ($ orig); break; // best quality
Case 3: imagepng ($ orig); break; // no compression
Default: echo ''; break;
}
// Save the image to a variable
$ ImageCode = ob_get_contents ();
Ob_end_clean ();
Saving images in variables is a waste of resources. This is just a test.

I hope this article will help you with PHP programming.

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.