Current Location: Home> Latest Articles> Detailed explanation and examples of imagewbmp function parameters

Detailed explanation and examples of imagewbmp function parameters

gitbox 2025-05-31

In PHP, the imagewbmp() function is used to output or save an image in WBMP format. WBMP (Wireless Bitmap) is a black and white bitmap format that is often used for image display on mobile devices. Understanding the parameters of the imagewbmp() function and its correct usage is very important for image processing and generation related projects.


Detailed explanation of the parameters of imagewbmp function

The basic syntax of the imagewbmp() function is as follows:

 bool imagewbmp ( resource $image [, string $filename = NULL [, int $threshold = 128 ]] )

Parameter description:

  1. $image
    Required parameters. Resource type, which represents an image resource, is usually an image resource generated by imagecreate() or other image creation function.

  2. $filename
    Optional parameters. Specifies the saved file name. If omitted or set to NULL , the function will output the image directly to the browser. Note: When outputting to the browser, the correct Content-Type header needs to be set before output.

  3. $threshold
    Optional parameters. Specifies a color threshold to convert color images to black and white. The default value is 128, indicating that colors greater than or equal to 128 are converted to white, and those less than 128 are converted to black.


How to use these parameters correctly?

  • Pass in a legal image resource , otherwise the function will report an error.

  • If you need to save the file , pass in the legal and write-rights file path as $filename .

  • If it is output directly to the browser , the header information needs to be set before calling: header('Content-Type: image/vnd.wap.wbmp');

  • Adjust the threshold to obtain a more suitable black and white effect, especially when color images are converted to WBMP.


Detailed examples

Example 1: Direct output of WBMP images to the browser

 <?php
// Create a150x50Black and white image
$image = imagecreate(150, 50);

// Set background to white
$white = imagecolorallocate($image, 255, 255, 255);

// Set text color to black
$black = imagecolorallocate($image, 0, 0, 0);

// Write text on image
imagestring($image, 5, 10, 15, 'Hello WBMP!', $black);

// set upHTTPhead,Tell the browser to outputWBMPimage
header('Content-Type: image/vnd.wap.wbmp');

// 直接输出image
imagewbmp($image);

// 销毁image资源,Free memory
imagedestroy($image);
?>

Example 2: Save WBMP images to server file

 <?php
// Create a100x100的image
$image = imagecreate(100, 100);

// Assign colors
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);

// Draw a black rectangle
imagerectangle($image, 10, 10, 90, 90, $black);

// 保存image到文件
imagewbmp($image, '/var/www/html/images/sample.wbmp', 100);

// Free up resources
imagedestroy($image);
?>

Related reference materials

For more official documentation about the imagewbmp() function, please refer to:
<code> https://gitbox.net/manual/en/function.imagewbmp.php </code>