Home >Backend Development >PHP Tutorial >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.
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.
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); }
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.
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);
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.
When blurring images, you need to pay attention to the following points:
imagecreatefrompng()
or other corresponding functions to create image resources. 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!