Home  >  Article  >  Backend Development  >  PHP and GD library tutorial: How to add inverse color effect to images

PHP and GD library tutorial: How to add inverse color effect to images

王林
王林Original
2023-07-13 12:37:09943browse

PHP and GD library tutorial: How to add an inverse color effect to pictures

Introduction:
In web development, it is often necessary to perform some special effects processing on pictures to achieve better visual effects. Among them, adding an inverse color effect to an image is a common processing method. In this tutorial, we will use PHP and the GD library to achieve this effect. The GD library is a powerful library that can be used to process images. By studying this tutorial, you will learn how to use the GD library to convert a given image into an inverse color effect.

Step 1: Preparation
Before starting, please make sure your server has the GD library installed. Use the following command to check:

php -m | grep gd

Step 2: Create a PHP file
First, we need to create a PHP file for writing code. You can name it reverse_color.php.

Step 3: Import and display the original image
Before further processing, we need to import the original image and display it on the web page. Use the following code to complete this step:

<?php
// 创建一个画布
$canvas = imagecreatetruecolor(400, 300);
// 导入原始图片
$source = imagecreatefromjpeg('original.jpg');
// 将原始图片复制到画布上
imagecopy($canvas, $source, 0, 0, 0, 0, 400, 300);
// 在浏览器上显示画布
header('Content-Type: image/jpeg');
imagejpeg($canvas);
?>

NOTE: Please replace original.jpg with your own image path.

Step 4: Convert the image to inverse color effect
Now, let’s get into the topic. The following code snippet demonstrates how to convert an image into a reverse color effect:

<?php
// 创建一个画布
$canvas = imagecreatetruecolor(400, 300);
// 导入原始图片
$source = imagecreatefromjpeg('original.jpg');
// 将原始图片复制到画布上
imagecopy($canvas, $source, 0, 0, 0, 0, 400, 300);
// 遍历画布的每个像素,并将颜色取反
for($x = 0; $x < 400; $x++) {
    for($y = 0; $y < 300; $y++) {
        $color = imagecolorat($canvas, $x, $y);
        $red = 255 - ($color >> 16) & 0xFF;
        $green = 255 - ($color >> 8) & 0xFF;
        $blue = 255 - $color & 0xFF;
        $newColor = imagecolorallocate($canvas, $red, $green, $blue);
        imagesetpixel($canvas, $x, $y, $newColor);
    }
}
// 在浏览器上显示画布
header('Content-Type: image/jpeg');
imagejpeg($canvas);
?>

Step 5: Save and test the effect
Save the above code into the reverse_color.php file, and Replace original.jpg with your own image path. The file is then placed on the server and accessed through the browser. You'll see the original image displayed on the page, converted to an inverted color effect. Now you can try using different images and see how the effect changes.

Summary:
Through this tutorial, we learned how to use PHP and the GD library to add an inverse color effect to images. First, we imported and displayed the original image, and then used the functions of the GD library to convert the image into an inverse color effect. By modifying the color of each pixel, we can achieve the desired effect. I believe that through studying this tutorial, you have understood the basic use of the GD library and can flexibly use its functions for image processing. I wish you more satisfactory results in web development!

The above is the detailed content of PHP and GD library tutorial: How to add inverse color effect to images. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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