Content · Web Development · PHP

PHP imagetypes() Function: Usage and Examples

Returns a bitmask of compiled-in GD format capabilities (PNG, JPEG, GIF, WebP, AVIF and others, depending on build).

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Admin

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


ApproachWhat changes
GD / Imagick / EXIFGD operates on raster images in PHP; Imagick delegates to ImageMagick; EXIF reads metadata and is not an image decoder.
Choosing safelyCheck image dimensions, true decoder availability, and actual output format rather than trusting a filename suffix.

Common Beginner Mistakes


MistakeCorrect approach
Trusting the image extension or MIME header aloneDecode the source and verify the actual format, dimensions, and resource limits.
Forgetting transparency or aspect ratioPreserve alpha for transparent formats and calculate dimensions before resampling.
Skipping operational constraintsThe 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.
Share this resourceSend this page to someone who may find it useful.