php uses function_exists to determine the methods available for the function

  • 2021-08-05 09:04:34
  • OfStack

This article gives an example of how php can use function_exists to judge the functions available. Share it for your reference. The details are as follows:

The function described in this paper is used to establish a graphic in gif format. The parameter im is the picture code established by using imagecreate (), and the parameter filename can be omitted. If there is no filename, the picture will be sent directly to the browser. Remember to send the header string (header) using content-type: image/gif to the browser before sending the picture, so as to transmit the picture smoothly. To use an gif diagram with a transparent background, which is in the format of gif89a, you first need to configure the transparent background using imagecolortransparent ().

$values=array( 
  40,50,         // No. 1 1 Coordinates of vertices
  20,240,         // No. 1 1 Coordinates of vertices
  60,60,         // No. 1 1 Coordinates of vertices
  240,20,         // No. 1 1 Coordinates of vertices
  50,40,         // No. 1 1 Coordinates of vertices
  10,10         // No. 1 1 Coordinates of vertices
);
$im=imagecreatetruecolor(250,250);   // Create an image
$bg=imagecolorallocate($im,200,200,200);  // Define gray background
$yellow=imagecolorallocate($im,255,255,0);  // Define yellow foreground
imagefilledpolygon($im,$values,6,$yellow);  // Draw a polygon
header('content-type: image/png');
// Judge gif Does the function exist
if(function_exists("imagegif"))
{
  // If it exists, use the gif Format output
  header("content-type: image/gif");
  imagegif($im);
}
// Judge jpeg Does the function exist
elseif(function_exists("imagejpeg"))
{
  // If it exists, use the jpg Format output
  header("content-type: image/jpeg");
  imagejpeg($im, "", 0.5);
}
// Judge png Does the function exist
elseif (function_exists("imagepng"))
{
  // If it exists, use the png Format output
  header("content-type: image/png");
  imagepng($im);
}
// Judge wbmp Does the function exist
elseif (function_exists("imagewbmp"))
{
  // If it exists, use the bmp Format output
  header("content-type: image/vnd.wap.wbmp");

The header () function sends the original http header to the client, and it is important to realize that the header () function must be called before any actual output is sent (in php 4 and later, you can use output caching to solve this problem):
  imagewbmp($im); 
}
else
{
  // If none of them are supported, output the content
  die("no image support in this php server");
}

The code judges a variety of image support, and then outputs the image in the corresponding format.

Syntax: int imagegif (int im, string [filename]);

Return value: integer, function type: graphics processing

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


Related articles: