php uses ob_start of to realize the method of saving pictures into variables

  • 2021-07-26 07:21:01
  • OfStack

In this paper, the method of php using ob_start () to store pictures into variables is described as an example. Share it for your reference. The specific implementation method is as follows:

With php GD library after processing the picture, can only use imagejpeg () picture output, or write a file. Most of the time, you don't need to do this. For example, if you want to store a picture in the database, you need to save the picture write variable. Enable the cache with ob_start () and get the cache with ob_get_contents (), you can write the picture into the variable

<?php
$imgPath =" Picture address " ;
// Get picture 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 );
...
// Turn on the cache
ob_start();
// Generate a 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 pictures into variables
$imageCode = ob_get_contents();
ob_end_clean();

Individuals do not build meaning to save the picture in variables, which will waste resources. Here is also Test 1.

I hope this article is helpful to everyone's PHP programming.


Related articles: