Skip to content
GPLWP Guides

How to generate GIF images in PHP

Developer Tools Time About 20 minutes Updated 30 Oct 2026

DEVELOPER TOOLS
The short answer

PHP can generate a static GIF with the GD extension by creating an image canvas, drawing on it, and saving it with imagegif(). For animated GIFs or frame editing, use Imagick or a dedicated GIF library because GD reads only the first frame.

What you need

  • PHP 8 or later
  • The GD extension enabled for static GIFs
  • A writable output directory
  • WordPress access if the GIF must be added to the Media Library
The fast route

Let gif-php do it

A lightweight PHP utility for generating GIF images, suited to developers who need direct server-side control.

Get gif-php

Tools that do this job

All of these solve the task above. They are listed as we have covered them.

Our pick
gif-php

A lightweight PHP utility for generating GIF images, suited to developers who need direct server-side control.

Get it
pure-gif

Lightweight GIF-generation utility for PHP developers who need programmatic image creation without a larger media-processing framework.

Get it

Sources

  1. php.net /manual/en/function.imagegif.php?utm_source=openai
  2. php.net /manual/en/function.imagecreatefromgif.php?utm_source=openai
  3. php.net /function.imagegif.php?utm_source=openai
  4. developer.wordpress.org /reference/functions/media_handle_sideload/?utm_source=opena…
  5. php.net /manual/en/book.image.php/?utm_source=openai

Questions

Can PHP create a GIF without a plugin?
Yes. PHP can create a static GIF without a WordPress plugin by using the GD extension and <code>imagegif()</code>. You create a GD image, draw the background and content, then save it to a file or send it with the <code>image/gif</code> content type. A plugin or library becomes useful when you need animation, frame editing, or WordPress Media Library integration.
Why does my animated GIF become a still image?
Your animated GIF becomes a still image because GD's <code>imagecreatefromgif()</code> loads only the first frame. Saving that GD image with <code>imagegif()</code> therefore writes one frame rather than the original animation. Use Imagick or a dedicated GIF encoder when you need to preserve or create multiple frames and their delays.
Why is the generated GIF corrupted in the browser?
A generated GIF is often corrupted because PHP sent other output before the image bytes. Remove notices, warnings, whitespace, debug text, and accidental HTML, then send the <code>Content-Type: image/gif</code> header before calling <code>imagegif()</code>. If you save to disk instead, check that the directory is writable and that the resulting file is non-empty.
Can I add a PHP-generated GIF to the WordPress Media Library?
Yes. Save the GIF to a temporary file, build a file array, and pass it through WordPress's <code>media_handle_sideload()</code> workflow. This moves the file into the uploads directory, creates an attachment, and generates metadata. Include the relevant WordPress admin files when the code runs from a custom script or another context outside the admin area.
Should I use GD, Imagick, or a GIF library?
Use GD for simple static GIFs and basic drawing. Use Imagick when you need to manipulate animated frames and delays, or use a dedicated GIF library when you want a focused encoder without depending on an image-processing extension. The right choice depends on whether your task is drawing one image, converting files, or assembling an animation.