Home > Backend Development > PHP Tutorial > PHP uses ob_start() to implement the method of storing images into variables, _PHP tutorial

PHP uses ob_start() to implement the method of storing images into variables, _PHP tutorial

WBOY
Release: 2016-07-13 10:14:06
Original
888 people have browsed it

php uses ob_start() to implement the method of storing images into variables,

The example in this article describes how PHP uses ob_start() to store images into variables. Share it with everyone for your reference. The specific implementation method is as follows:

After processing the image with PHP's GD library, you can only output the image using imagejpeg() or write it to a file. Many times this is not necessary. For example, if you want to store a picture in the database, you need to write the picture into a variable to save it. Use ob_start() to enable the cache and ob_get_contents() to get the cache and then write the picture into the variable

Copy code The code is as follows:
$imgPath ="image address" ;
//Get 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 caching
ob_start();
//Generate picture
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 into a variable
$imageCode = ob_get_contents();
ob_end_clean();

Personally, I don’t recommend saving images in variables, as this would be a waste of resources. I’m just testing it here.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/911907.htmlTechArticlephp uses ob_start() to implement the method of storing images into variables. This article describes the example of php using ob_start() to implement images. Method of storing variables. Share it with everyone for your reference. Specific implementation method...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template