Returns a bitmask of compiled-in GD format capabilities (PNG, JPEG, GIF, WebP, AVIF and others, depending on build).
What It Does and When to Use It
Test a format bit in the runtime capability mask. 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. The bitmask describes GD support, not formats present in a particular image.
Example 1: Basic Operation
Test a format bit in the runtime capability mask.
<?php
$formats = imagetypes();
echo ($formats & IMG_PNG) ? 'PNG available' : 'PNG unavailable';
Example 2: Behavior and Validation
List selected formats only when the corresponding bit is present.
<?php
$capabilities = [IMG_GIF => 'GIF', IMG_JPG => 'JPEG', IMG_WEBP => 'WebP'];
foreach ($capabilities as $bit => $label) {
echo $label, ': ', (imagetypes() & $bit ? 'yes' : 'no'), "
";
}
Practical Example: Application Use
Perform the capability check before using a format encoder.
<?php
if (!(imagetypes() & IMG_WEBP)) {
throw new RuntimeException('WebP output is not supported by GD');
}
$image = imagecreatetruecolor(20, 20);
try { if (!imagewebp($image, __DIR__ . '/preview.webp', 82)) throw new RuntimeException('Write failed'); }
finally { imagedestroy($image); }
Important Behavior and Edge Cases
The second example highlights: List selected formats only when the corresponding bit is present. The practical example adds: Perform the capability check before using a format encoder.
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 | The bitmask describes GD support, not formats present in a particular image. |
Key Takeaways
- Returns a bitmask of compiled-in GD format capabilities (PNG, JPEG, GIF, WebP, AVIF and others, depending on build).
- Perform the capability check before using a format encoder.
- Check image dimensions, true decoder availability, and actual output format rather than trusting a filename suffix.