Current Location: Home> Latest Articles> Example of using imagewbmp function and imagecreate function

Example of using imagewbmp function and imagecreate function

gitbox 2025-05-29

In PHP image processing function, imagecreate and imagewbmp are two common functions, which are used to create image resources and output images into WBMP format respectively. The WBMP (Wireless Bitmap) format is a black and white image format for wireless devices. Although the usage scenarios are gradually decreasing, it is still useful in some lightweight systems or scenarios that are compatible with old devices. This article will introduce in detail how to use these two functions efficiently to generate images that suit your needs.

1. Introduction to basic functions

imagecreate

The imagecreate function is used to create an image canvas with a specified width and height. The basic syntax is:

 $img = imagecreate(int $width, int $height);

It returns an image resource handle for subsequent operations. Note: This function creates a palette image (not a true color image), which is suitable for generating simple image structures such as black and white images.

imagewbmp

The imagewbmp function outputs image resources to WBMP format. Its syntax is as follows:

 imagewbmp(GdImage $image, ?string $file = null, int $foreground = 0);
  • $image : The image resource created by imagecreate .

  • $file : Optional, specify the output path, if null , it will be output directly to the browser.

  • $foreground : Specifies the foreground index (optional).

2. Efficient combination usage method

Here is a complete example of efficiently generating images using imagecreate and imagewbmp :

 <?php
// Set the header information to WBMP type
header("Content-Type: image/vnd.wap.wbmp");

// Create a wide100high50Images
$width = 100;
$height = 50;
$img = imagecreate($width, $height);

// Assign colors:The first color is usually the background color
$white = imagecolorallocate($img, 255, 255, 255); // Background color
$black = imagecolorallocate($img, 0, 0, 0);       // Foreground color

// Draw a diagonal line
imageline($img, 0, 0, $width - 1, $height - 1, $black);

// Output image
imagewbmp($img);

// Destroy resources
imagedestroy($img);
?>

This code directly outputs a white background image with black lines on the diagonal line, and the format is WBMP, which is very suitable for lightweight image processing tasks, such as generating verification code diagrams, QR code basic images, etc.

3. Output to a file instead of a browser

If you want to save WBMP images to a file instead of outputting them directly to the browser, just pass in the target path parameters:

 imagewbmp($img, '/var/www/gitbox.net/images/sample.wbmp');

Note that the server must have write permissions, otherwise the file write failure will be caused.

4. Some suggestions for improving efficiency

  • Reduce color allocation : Because WBMP is in black and white format, try to use only two colors to avoid unnecessary memory usage.

  • Release resources as soon as possible : Release image resources in time through imagedestroy() to avoid memory leakage.

  • Reasonable control of image size : WBMP is mainly used in lightweight image scenes without the need for large-sized canvases.

  • Cache processing : The generated WBMP images can be cached to avoid duplicate generation and improve response speed.

5. Combining network resources or dynamic data

Suppose you need to generate images from dynamic data and provide user downloads, which can be implemented in combination with URL parameters, such as:

 https://gitbox.net/gen_image.php?text=Hello

Draw text after reading parameters in gen_image.php :

 $text = $_GET['text'] ?? 'Default';

// Create an image
$img = imagecreate(120, 30);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);

// Draw text
imagestring($img, 5, 5, 8, $text, $black);

// The output is WBMP
header("Content-Type: image/vnd.wap.wbmp");
imagewbmp($img);
imagedestroy($img);

Conclusion

Although the WBMP format is not used frequently in modern development, it is still of practical significance in specific fields. By combining imagecreate with imagewbmp function, PHP can quickly generate lightweight black and white images to meet the image display needs in low bandwidth and low memory environments. The key to using these two functions efficiently is to understand their basic behavior, allocate resources reasonably, and control the output method and image complexity.