How to blur an image using PHP

WBOY
Release: 2023-08-18 14:16:01
Original
1891 people have browsed it

How to blur an image using PHP

How to use PHP to blur images

Image blur processing is a common operation in image processing, which can add a blur effect to the image to make it It looks softer and artistic. In PHP, we can use the GD library to blur images. The following will introduce how to use PHP to blur images, and attach corresponding code examples.

  1. Install the GD library

Before you start, you need to make sure that your server has the GD library installed. You can check whether the GD library is installed by adding the phpinfo() function in the PHP file. If not, you need to install it manually.

  1. Create a function for blur processing

Before starting blur processing, we need to write a function to process the image. Below is a basic obfuscation function that you can modify and extend as needed.

function blurImage($source, $radius = 10, $sigma = 10) {
    $image = imagecreatefromjpeg($source);
    imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR, $radius);
    imagejpeg($image, $source, $sigma);
    imagedestroy($image);
}
Copy after login

In the above function, we use the imagecreatefromjpeg() function to create a new image resource, and then use the imagefilter() function to Gaussian the image Blurring. Finally, use the imagejpeg() function to save the processed image in JPEG format and specify the blur coefficient $sigma. Finally, use the imagedestroy() function to destroy the image resource.

  1. Usage Example

The following is a simple usage example that demonstrates how to blur an image by calling the above blur function.

$source = 'image.jpg';
$radius = 10;
$sigma = 10;

blurImage($source, $radius, $sigma);
Copy after login

In the above example, we assume there is a picture named "image.jpg", we will use a radius of 10 and a blur coefficient of 10 to blur the picture. You can modify these parameters as needed.

  1. Notes

When blurring images, you need to pay attention to the following points:

  • The path and name of the image must be correct. Make sure PHP has sufficient permissions to read and write images.
  • If the image to be processed is in PNG format or other formats, you need to use imagecreatefrompng() or other corresponding functions to create image resources.
  • Adjust the blur radius and blur coefficient to obtain different degrees of blur effects. You can adjust them according to your own needs.

Summary:

Using PHP to blur images is a common image processing operation. By using the API provided by the GD library, we can easily implement blurring of images. Blurring. Through the introduction and sample code of this article, I hope it can help you quickly achieve the blur effect of pictures and achieve artistic purposes.

The above is the detailed content of How to blur an image using PHP. 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