Home Backend Development PHP Tutorial How to use the image processing library GD in PHP

How to use the image processing library GD in PHP

Jun 27, 2023 am 08:46 AM
php Image Processing gd

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; 
Copy after login

} else {

$newwidth = $desiredheight * $ratio; 
$newheight = $desiredheight; 
Copy after login

}

$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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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 Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

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.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

See all articles