Current Location: Home> Latest Articles> How to Enable GD Library in PHP and Work with Images

How to Enable GD Library in PHP and Work with Images

M66 2025-09-24

How to Enable GD Library in PHP and Work with Images

GD (Graphics Draw) library is a set of functions used to create, edit, and manipulate images in PHP. To use these features in PHP, you first need to ensure the GD library is enabled.

Check if GD Library is Enabled

In PHP, you can check if the GD library is enabled by using the following code:

<?php
var_dump(extension_loaded('gd'));
?>

If the output is true, the GD library is enabled.

Enable GD Library

If you find that the GD library is not enabled, you can enable it depending on your operating system and web server. Here’s how:

  • Unix/Linux Systems: Run the following command to install the GD library:
  • sudo apt-get install php-gd
  • Windows Systems: Edit the PHP configuration file php.ini and uncomment the extension=gd line.

Using GD Library

Once the GD library is enabled, you can use various functions to create, load, and save images. Here are some common operations:

  • Create an Image:
  • $image = imagecreate(200, 100);
  • Load an Image:
  • $image = imagecreatefromjpeg('image.jpg');
  • Save an Image:
  • imagejpeg($image, 'new_image.jpg');

Sample Code

Here’s a sample PHP code showing how to use the GD library to create an image with text:

<?php
// Create a 200x100 pixel image
$image = imagecreate(200, 100);

// Set background color to white
imagecolorallocate($image, 255, 255, 255);

// Set text color to black
$text_color = imagecolorallocate($image, 0, 0, 0);

// Write text
imagettftext($image, 20, 0, 50, 50, $text_color, 'arial.ttf', 'Hello, World!');

// Save the image
imagejpeg($image, 'hello_world.jpg');
?>

This is the basic guide to enabling and using the GD library in PHP. With these code examples, you can easily get started with image processing in PHP.