Home >Backend Development >PHP Tutorial >PHP and GD library tutorial: How to add a nostalgic effect to images
PHP and GD library tutorial: How to add nostalgic effects to pictures
Introduction:
In the modern social media era, people attach great importance to the processing and sharing of pictures. We often see some photos that have a nostalgic feel. Adding some old-fashioned effects to the photos can increase the artistic sense and emotional appeal of the photos. This tutorial will introduce how to use PHP and the GD library to add a nostalgic effect to images.
1. Preparation
Before starting, please make sure that you have installed PHP and the corresponding GD library, and that you have basic knowledge of PHP and GD library.
2. Basic idea
The basic idea of adding a nostalgic effect to a picture is to process the pixel value of the picture to make it look like an old photo. We will use the following algorithm to achieve this effect:
3. Code example
The following is a complete PHP code example that demonstrates how to add a nostalgic effect to an image :
<?php //指定图片路径 $imagePath = 'path_to_your_image.jpg'; //创建一个图像资源 $image = imagecreatefromjpeg($imagePath); //获取图像的宽度和高度 $width = imagesx($image); $height = imagesy($image); //将图像转换为灰度图像 imagefilter($image, IMG_FILTER_GRAYSCALE); //对每个像素进行亮度调整 for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $rgb = imagecolorat($image, $x, $y); $red = ($rgb >> 16) & 0xFF; $green = ($rgb >> 8) & 0xFF; $blue = $rgb & 0xFF; $y = ($red + $green + $blue) / 3; imagecolorset($image, $rgb, $y, $y, $y); } } //对每个像素进行色调调整 for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $rgb = imagecolorat($image, $x, $y); $red = ($rgb >> 16) & 0xFF; $green = ($rgb >> 8) & 0xFF; $blue = $rgb & 0xFF; $red = min(255, max(0, $red - 30)); //减少红色通道的值 $green = min(255, max(0, $green - 20)); //减少绿色通道的值 $blue = min(255, max(0, $blue + 10)); //增加蓝色通道的值 $rgb = ($red << 16) | ($green << 8) | $blue; imagesetpixel($image, $x, $y, $rgb); } } //对每个像素进行饱和度调整 for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $rgb = imagecolorat($image, $x, $y); $red = ($rgb >> 16) & 0xFF; $green = ($rgb >> 8) & 0xFF; $blue = $rgb & 0xFF; $max = max($red, $green, $blue); $min = min($red, $green, $blue); $avg = ($max + $min) / 2; $red = min(255, max(0, $avg + 10)); //增加红色通道的值 $green = min(255, max(0, $avg + 5)); //增加绿色通道的值 $blue = min(255, max(0, $avg)); //保持蓝色通道不变 $rgb = ($red << 16) | ($green << 8) | $blue; imagesetpixel($image, $x, $y, $rgb); } } //输出图像 header('Content-type: image/jpeg'); imagejpeg($image); //释放图像资源 imagedestroy($image); ?>
4. Summary
This tutorial demonstrates how to use PHP and GD library to add nostalgic effects to pictures. We can easily achieve this effect by converting the image to grayscale and adjusting the brightness, hue, and saturation of the pixels. I hope this tutorial will be helpful for you to learn and master PHP and GD libraries. I wish you enjoy programming!
The above is the detailed content of PHP and GD library tutorial: How to add a nostalgic effect to images. For more information, please follow other related articles on the PHP Chinese website!