Home  >  Article  >  Backend Development  >  How to use PHP to achieve the black and white effect of pictures

How to use PHP to achieve the black and white effect of pictures

王林
王林Original
2023-08-17 14:25:04858browse

How to use PHP to achieve the black and white effect of pictures

How to use PHP to achieve the black and white effect of pictures

The processing of pictures is very important in many Web applications. Converting color pictures to black and white effects is a common need, which not only increases the artistic sense of the pictures, but also adapts to some specific design requirements. In this article, we will introduce how to use PHP to achieve the black and white effect of pictures.

1. Using PHP’s GD library

PHP’s GD library is an image processing library that can be used to perform various operations on images, including cropping, scaling, watermarking, etc. . Before using it, we need to make sure that the GD library has been installed on our server. We can check the installation of the GD library through the following code:

<?php
// 检查GD库是否已经安装
if (!extension_loaded('gd') && !function_exists('gd_info')) {
    echo 'GD库未安装,无法继续操作!';
    exit;
}

// 获取GD库的版本信息
$gd_info = gd_info();
echo 'GD库版本:' . $gd_info['GD Version'];
?>

If the output GD library version information is empty, it means that the GD library is not installed.

2. Open the picture file

Before we begin, we need to open a color picture and then convert it to black and white. We can use the imagecreatefromjpeg function of the GD library to open a JPEG format image. The following is the sample code:

<?php
// 打开一张图片
$src_image = imagecreatefromjpeg('example.jpg');
?>

The example.jpg here is the image file we actually use, which needs to be replaced with your own image path.

3. Convert to black and white effect

To convert a color picture into a black and white effect, we can achieve this by modifying the RGB value of each pixel. Take the average of the three RGB components, and then assign this average to the RGB component of the pixel to convert the color image into a black and white effect. The following is a sample code:

<?php
// 获取图片的宽度和高度
$width = imagesx($src_image);
$height = imagesy($src_image);

// 创建一个新的黑白图片
$dst_image = imagecreatetruecolor($width, $height);

// 遍历每个像素进行转换
for ($x = 0; $x < $width; $x++) {
    for ($y = 0; $y < $height; $y++) {
        // 获取像素的RGB值
        $rgb = imagecolorat($src_image, $x, $y);
        $r = ($rgb >> 16) & 0xFF;  // 获取红色分量
        $g = ($rgb >> 8) & 0xFF;   // 获取绿色分量
        $b = $rgb & 0xFF;          // 获取蓝色分量

        // 计算RGB的平均值
        $gray = round(($r + $g + $b) / 3);

        // 将平均值赋给像素的RGB分量
        $new_rgb = imagecolorallocate($dst_image, $gray, $gray, $gray);

        // 设置新的像素值
        imagesetpixel($dst_image, $x, $y, $new_rgb);
    }
}
?>

4. Save black and white pictures

After completing the conversion of black and white effects, we can use the imagejpeg function to save the black and white pictures to a file. The following is the sample code:

<?php
// 保存黑白图片
imagejpeg($dst_image, 'example_bw.jpg');
?>

The example_bw.jpg here is the file path to save the black and white image, which can be modified according to the actual situation.

5. Complete sample code

<?php
// 打开一张图片
$src_image = imagecreatefromjpeg('example.jpg');

// 获取图片的宽度和高度
$width = imagesx($src_image);
$height = imagesy($src_image);

// 创建一个新的黑白图片
$dst_image = imagecreatetruecolor($width, $height);

// 遍历每个像素进行转换
for ($x = 0; $x < $width; $x++) {
    for ($y = 0; $y < $height; $y++) {
        // 获取像素的RGB值
        $rgb = imagecolorat($src_image, $x, $y);
        $r = ($rgb >> 16) & 0xFF;  // 获取红色分量
        $g = ($rgb >> 8) & 0xFF;   // 获取绿色分量
        $b = $rgb & 0xFF;          // 获取蓝色分量

        // 计算RGB的平均值
        $gray = round(($r + $g + $b) / 3);

        // 将平均值赋给像素的RGB分量
        $new_rgb = imagecolorallocate($dst_image, $gray, $gray, $gray);

        // 设置新的像素值
        imagesetpixel($dst_image, $x, $y, $new_rgb);
    }
}

// 保存黑白图片
imagejpeg($dst_image, 'example_bw.jpg');

// 销毁图片资源
imagedestroy($src_image);
imagedestroy($dst_image);
?>

6. Summary

Using PHP’s GD library can easily achieve the black and white effect of images. We can easily convert a color image to black and white by opening the image, iterating through the pixels and modifying the RGB values, and finally saving the modified image. The above are the steps and sample code for using PHP to achieve black and white effects on images. Hope this helps!

The above is the detailed content of How to use PHP to achieve the black and white effect of pictures. 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