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.