How to use the image processing library GD in PHP
Image processing is very common in modern Internet applications, ranging from website design to graphical data visualization and computer vision applications that require image processing. PHP provides an image processing library called GD, which supports most common image format processing and provides conventional operations and conversion methods, such as rotation, scaling, cropping, filters, etc. Today we will introduce how to use the GD image processing library for image processing in PHP applications.
1. Install the GD extension
Before you start using the GD library, you need to ensure that the extension has been enabled in the php.ini configuration file. If you are using a Linux server, you can check whether the GD extension has been installed by running the following command:
sudo apt-get install php-gd
For other operating systems and PHP versions, please Read the installation documentation carefully.
2. Loading images
In PHP, it is very simple to load images using the GD library:
$filename = "image.jpg";
$image = imagecreatefromjpeg ($filename);
In this example, we first specify the path to an image file, and then load the image using the imagecreatefromjpeg() function. You can also load images in PNG and GIF formats using the imagecreatefrompng() or imagecreatefromgif() functions.
3. Resizing
For most image processing applications, one of the common tasks is to resize the image. Using the GD library, images can be scaled by calling the imagecopyresampled() function, for example:
$desiredwidth = 800;
$desiredheight = 600;
$origwidth = imagesx($image);
$origheight = imagesy($image);
$ratio = $origwidth / $origheight;
if ($desiredwidth / $desiredheight > $ratio) {
$newheight = $desiredwidth / $ratio; $newwidth = $desiredwidth;
} else {
$newwidth = $desiredheight * $ratio; $newheight = $desiredheight;
}
$newimage = imagecreatetruecolor($desiredwidth, $desiredheight);
imagecopyresampled($newimage, $image, 0, 0, 0, 0, $ newwidth, $newheight, $origwidth, $origheight);
$image = $newimage;
In this code, we first define the width and height we want to adjust. Then, we get the width and height of the original image through the imagesx() and imagesy() functions. We calculate the ratio of the original height and width to ensure that the scaled image maintains the same aspect ratio. Next, create a new canvas and use the imagecopyresampled() function to copy the image from the old canvas to the new canvas.
4. Rotate and flip
The GD library also supports rotation and flipping of images, for example:
$image = imagecreatefromjpeg($filename);
$image = imagerotate($image, 45, 0);
In this example, we use the imagerotate() function to rotate the loaded image 45 degrees. If you want to flip horizontally or vertically, you can use the imageflip() function, as follows:
$image = imagecreatefromjpeg($filename);
imageflip($image, IMG_FLIP_VERTICAL);
5. Crop the image
If you need to crop the image, you can use the imagecrop() function. For example:
$image = imagecreatefromjpeg($filename);
$x = 20;
$y = 20;
$width = 200;
$height = 200;
$crop = imagecrop($image, ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height]);
$image = $crop;
In this example, we crop by defining the horizontal and vertical coordinates, width and height of the crop. The crop() function then returns a new canvas and assigns it to the $image variable.
6. Add filters
The GD library makes images more interesting and vivid by providing some built-in filter effects. For example, the following code can turn an image into grayscale:
$image = imagecreatefromjpeg($filename);
imagefilter($image, IMG_FILTER_GRAYSCALE);
$crop = imagecrop( $image, ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height]);
$image = $crop ;
Use the following constants to use other built-in filters:
- IMG_FILTER_NEGATE - Negative effect
- IMG_FILTER_EMBOSS - Embossing effect
- IMG_FILTER_EDGEDETECT - Edge detection Effect
- IMG_FILTER_GAUSSIAN_BLUR - Gaussian blur effect
- IMG_FILTER_SELECTIVE_BLUR - Non-linear blur effect
7. Save the image
After completing the image processing, the last step That is to save the processed image to a file or output it to the browser. Use the imagejpeg() function to save the image in JPEG format:
$destination = "new-image.jpg";
imagejpeg($image, $destination);
can be used The imagepng() or imagegif() function saves the image in PNG or GIF format respectively.
Summary
The GD library is a powerful image processing library that can make PHP applications richer and more vivid. In this article, we covered how to load, scale, crop, filter, and save images using the GD library. Using these techniques, you can take the graphical effects of your PHP applications to the next level!
The above is the detailed content of How to use the image processing library GD in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
