PHP and GD library tutorial: How to add blur edge effect to images

WBOY
Release: 2023-07-12 19:44:01
Original
1262 people have browsed it

PHP and GD library tutorial: How to add blur edge effects to pictures

GD library is a PHP extension library for processing images. It provides a series of functions that can edit and process images. and generate. In this tutorial, we will introduce how to use the GD library to add a blurred edge effect to an image.

Step 1: Install the GD library
First, make sure your PHP environment has the GD library installed. You can find the following line in the php.ini file and uncomment it (remove the preceding semicolon):

;extension=gd
Copy after login

After uncommenting, save and restart your web server.

Step 2: Create a PHP file
In your project directory, create a PHP file named blur.php and open the editor.

Step 3: Add code
In the blur.php file, we first need to process the input image file. Let's say we have an image called "input.jpg" and place it in the same directory as the blur.php file. Next, we will use the imagecreatefromjpeg() function to load the image into memory.

// 载入图片
$input_image = imagecreatefromjpeg('input.jpg');
Copy after login

Next, we will create a filter for rendering the blur effect. We can add a Gaussian blur effect by using the imagefilter() function and setting the first parameter to IMG_FILTER_GAUSSIAN_BLUR.

// 添加模糊效果
imagefilter($input_image, IMG_FILTER_GAUSSIAN_BLUR);
Copy after login

With the blurred image, we can save it as a new image file. By using the imagejpeg() function, we can save the image as a JPEG format file.

// 保存图像
imagejpeg($input_image, 'output.jpg');
Copy after login

Finally, we need to display the final image in the browser. By setting the response header using the header() function, we can tell the browser that the returned content is an image. Then, use the imagejpeg() function to output the image to the browser.

// 在浏览器中显示图像
header('Content-Type: image/jpeg');
imagejpeg($input_image);
Copy after login

The complete code is as follows:

// 载入图片
$input_image = imagecreatefromjpeg('input.jpg');

// 添加模糊效果
imagefilter($input_image, IMG_FILTER_GAUSSIAN_BLUR);

// 保存图像
imagejpeg($input_image, 'output.jpg');

// 在浏览器中显示图像
header('Content-Type: image/jpeg');
imagejpeg($input_image);
Copy after login

Step 4: Run the code
Save and close the blur.php file. Now, you can run the code in your browser by visiting http://localhost/blur.php.

After opening blur.php in your browser, you will see a picture with a blurred edge effect. At the same time, the output.jpg file will also be created in your project directory.

Conclusion
Through this tutorial, we learned how to use PHP and the GD library to add a blurred edge effect to an image. You can further explore other features of the GD library for more image processing and editing. Good luck applying this knowledge to your projects!

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template