This article mainly introduces how php uses ob_start () to store images in variables. it is a flexible use of cache and has a set reference value, for more information about how php uses ob_start () to store images in variables, see the example below. 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.
The 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.