Home > Backend Development > PHP Tutorial > How to use PHP to implement image filter effects

How to use PHP to implement image filter effects

WBOY
Release: 2023-08-17 22:52:02
Original
824 people have browsed it

How to use PHP to implement image filter effects

How to use PHP to achieve filter effects on images

Abstract:
In web development, in order to improve user experience, images are often filtered . This article will introduce how to use PHP to implement image filter effects, and help readers get started quickly through simple code examples.

1. Preparation
Before starting, you need to make sure that PHP and GD libraries have been installed. The GD library is a PHP extension library for processing images. You can install it through the following command:

sudo apt-get install php-gd
Copy after login

After the installation is completed, you need to enable the GD library in the php.ini file and uncomment the following line:

extension=gd2
Copy after login

Restart the web server to make the changes take effect.

2. Image processing

  1. Load the original image
    First, we need to load the original image. You can use PHP's imagecreatefromjpeg, imagecreatefrompng or imagecreatefromgif function to select the corresponding function according to the format of the image. The following is a sample code:

    $photo = imagecreatefromjpeg('original.jpg');
    Copy after login
  2. Create filter effects
    Next, we can create different filter effects. The following are several common filter effect sample codes:
    (1) Grayscale filter

    imagefilter($photo, IMG_FILTER_GRAYSCALE);
    Copy after login

    (2) Inversion filter

    imagefilter($photo, IMG_FILTER_NEGATE);
    Copy after login

    (3) Brightness adjustment

    imagefilter($photo, IMG_FILTER_BRIGHTNESS, 30);
    Copy after login

    (4) Contrast adjustment

    imagefilter($photo, IMG_FILTER_CONTRAST, -20);
    Copy after login

    (5) Blur filter

    imagefilter($photo, IMG_FILTER_GAUSSIAN_BLUR);
    Copy after login
  3. Save the processed image
    After processing the filter effect , you can use the imagejpeg, imagepng or imagegif function to save the image in the corresponding format. The following is a sample code saved in JPEG format:

    imagejpeg($photo, 'filtered.jpg');
    Copy after login
  4. Output the processed image
    If you want to display the processed image directly in the browser, you can use Functions such as header and imagejpeg are output. The following is a sample code:

    header('Content-Type: image/jpeg');
    imagejpeg($photo);
    Copy after login
  5. Destroy image resources
    After processing the filter effect, you need to use the imagedestroy function to destroy the image resource to release memory . The following is a sample code:

    imagedestroy($photo);
    Copy after login

    3. Summary
    This article introduces how to use PHP to achieve the filter effect of images. By loading the original image, creating different filter effects, saving the processed image, and exporting the processed image, you can easily achieve the effect of image filters. Readers can adjust the filter parameters as needed to achieve satisfactory results.

    Reference materials:

    1. PHP official documentation: http://php.net/manual/zh/ref.image.php
    2. GD library official documentation: http://php.net/manual/zh/book.image.php

    The above is the detailed content of How to use PHP to implement image filter effects. 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