How to use GD docker php ext install and configure GD CMD

Posted October 23, 2023
How to use GD docker php ext install and configure CMD

GD Extension dynamically manipulates images in PHP. It allows you to resize, cropping, watermark, and generate PHP thumbnails. When Working with Docker, you must run docker php ext install gd and docker php ext configure gd commands. They allow you to install and configure GD when running PHP on a Docker environment.

This guide teaches you how to use docker php ext install gd and docker php ext configure gd commands with freetype and jpeg. This way, you will be able to run imagecreatefromjpeg(), imagecreatetruecolor(), imagecopyresampled(), etc. functions for PHP image manipulation on Docker.

Dive into this guide and run the docker php ext command to install and configure GD within Docker.

Related: Mastering docker php ext install and enable Imagick Commands

Step 1: Setting up Dockerfile with PHP and GD Extension

The right way to install and configure any PHP extensions is to use a Dockerfile. It allows you to write all the instructions your app needs to run on Docker.

Assuming you have the following PHP Docker structure:

php-gd/
β”‚ β”œβ”€β”€ gd.dockerfile
β”‚ └── docker-compose.yml
└── app/
    └── index.php

Your Dockerfile will install and configure GD. Create A Dockerfile illustrating how to install GD Extension for PHP as follows:

FROM php:8.2-apache

# Install GD extension with JPEG and PNG support
RUN apt-get update \
    && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev \
    # configure the GD extension to include support for JPEG and PNG image formats
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd

Here:

  • You are running PHP 8.2 on Apache, choose the PHP version needed for the project as well.
  • libfreetype6-dev, libjpeg62-turbo-dev, and libpng-dev packages handle various image formats (FreeType for fonts, JPEG for JPEG images, and libpng for PNG images) within the GD extension.
  • The docker-php-ext-configure command enables GD extension with with-freetype –with-jpeg. docker-php-ext-configure adds specific settings and features tailored to the project’s needs.
  • docker-php-ext-install compiles and installs the GD extension into your PHP Docker environment.

Once these commands are ready, Docker will run PHP and allow you to write GD-related scripts.

Step 2: Creating Container PHP with GD Extension and Docker Compose

Once Docker files get the GD PHP extension installed and configured, you can spin up the Docker Compose Container and expose yourself to the PHP app. Create a docker-compose.yml file as follows:

version: '3.9'

services:
  php-apache:
    build:
      context: .
      dockerfile: gd.dockerfile
    ports:
      - "8080:80"
    # Docker Compose Working directory path
    volumes:
      - ./app:/var/www/html 

You must note that volumes will Copy your PHP code on app and share it with Docker. This way, your index.php file will be executed and served on the web.

Let’s create a simple GD PHP script on app/index.php and compile a blank image.

Step 3: Illustrate the practical use of docker php ext configure and enable GD with PHP

To test if the GD extension is correctly working on Docker, we will create a simple blank image with a white background and Draw a red rectangle. Add the following code to your app/index.php:

<?php
// Create a blank image with a white background
$image = imagecreatetruecolor(400, 200);
$white = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $white);

// Draw a red rectangle
$red = imagecolorallocate($image, 255, 0, 0);
imagefilledrectangle($image, 50, 50, 350, 150, $red);

// Output the image
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>

Step 4: Creating a GD PHP Docker Container.

Now, use the docker-compose command to run your GD PHP app. Use the following command to build and run your containers:

docker-compose up -d --build

How to use GD docker php ext install and configure GD CMD

Confirm your container is running:

How to use GD docker php ext install and configure GD CMD

Open http://localhost:8000 on your browser and access your app:

How to use GD docker php ext install and configure GD CMD

And yes, GD Extension is correctly installed on Docker, and your PHP script works.

Conclusion

You have created a simple GD PHP container. The main goal was to demonstrate how to use docker php ext install gd and docker php ext configure gd commands with Docker. I hope you found this helpful.

Also check this docker php ext install intl Commands with Docker guide and dive deeper.

How to use GD docker php ext install and configure GD CMD

Written By:

Joseph Chege