Encodes a GdImage as WebP, optionally choosing a quality value between 0 and 100.
What It Does and When to Use It
Create a small example image and save it using the exact encoder. GD operates on raster images in PHP; Imagick delegates to ImageMagick; EXIF reads metadata and is not an image decoder.
Requirements, Inputs and Expected Behavior
Image decoding and encoding need the relevant GD, EXIF, or Imagick support; features vary by PHP build and server. WebP support is build-dependent; check GD capabilities and output file rather than relying only on a boolean.
Example 1: Basic Operation
Create a small example image and save it using the exact encoder.
<?php
$image = imagecreate(16, 16);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imageline($image, 0, 0, 15, 15, $black);
$destination = __DIR__ . '/line.webp';
try { if (!imagewebp($image, $destination, 78)) throw new RuntimeException('Image export failed'); }
finally { imagedestroy($image); }
Example 2: Behavior and Validation
Show the same encoder handling a different source image; ensure the destination is writable.
<?php
$image = imagecreatetruecolor(100, 40);
$background = imagecolorallocate($image, 240, 240, 240);
imagefill($image, 0, 0, $background);
$destination = __DIR__ . '/label.webp';
if (!imagewebp($image, $destination, 78)) { throw new RuntimeException('Encoder failed'); }
imagedestroy($image);
Practical Example: Application Use
Verify encoder availability and the file rather than relying on the function name alone.
<?php
if (!function_exists('imagewebp')) {
throw new RuntimeException('Required GD encoder unavailable');
}
$image = imagecreatetruecolor(4, 4);
$destination = sys_get_temp_dir() . '/sample.webp';
try {
if (!imagewebp($image, $destination, 78) || !is_file($destination)) throw new RuntimeException('No output generated');
echo filesize($destination), " bytes
";
} finally { imagedestroy($image); if (is_file($destination)) unlink($destination); }
Important Behavior and Edge Cases
The second example highlights: Show the same encoder handling a different source image; ensure the destination is writable. The practical example adds: Verify encoder availability and the file rather than relying on the function name alone.
Comparison with Related Approaches
| Approach | What changes |
|---|---|
| GD / Imagick / EXIF | GD operates on raster images in PHP; Imagick delegates to ImageMagick; EXIF reads metadata and is not an image decoder. |
| Choosing safely | Check image dimensions, true decoder availability, and actual output format rather than trusting a filename suffix. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Trusting the image extension or MIME header alone | Decode the source and verify the actual format, dimensions, and resource limits. |
| Forgetting transparency or aspect ratio | Preserve alpha for transparent formats and calculate dimensions before resampling. |
| Skipping operational constraints | WebP support is build-dependent; check GD capabilities and output file rather than relying only on a boolean. |
Key Takeaways
- Encodes a GdImage as WebP, optionally choosing a quality value between 0 and 100.
- Verify encoder availability and the file rather than relying on the function name alone.
- Check image dimensions, true decoder availability, and actual output format rather than trusting a filename suffix.