Current Location: Home> Latest Articles> How to adjust the contrast of images after imageflip flip?

How to adjust the contrast of images after imageflip flip?

gitbox 2025-05-19

In PHP, the image processing library GD provides rich image operation functions, including image flip, contrast adjustment, etc. This article will introduce you to how to adjust the contrast after flipping the image using PHP's imageflip() function.

1. Preparation

First, make sure your PHP environment supports GD libraries. You can check whether the GD library has been installed by:

 php -m | grep gd

If there is gd in the return result, it means that the GD library has been installed. If not installed, you can install it through the following command:

 sudo apt-get install php-gd

After the installation is complete, restart the PHP service.

2. Load the picture and flip it

First, we want to load an image and flip it using the imageflip() function. There are several directional options for flips, including horizontal flips, vertical flips, etc. The function prototype of imageflip() is as follows:

 imageflip(resource $image, int $mode): bool
  • $image is the resource of the target image.

  • $mode is the direction of flip, which can be the following:

    • IMG_FLIP_HORIZONTAL : Horizontal flip

    • IMG_FLIP_VERTICAL : vertical flip

    • IMG_FLIP_BOTH : horizontal and vertical flip

Sample code:

 <?php
// Loading the image
$imagePath = 'path_to_your_image.jpg';
$image = imagecreatefromjpeg($imagePath);

// Flip the image,Select horizontal flip
if ($image) {
    imageflip($image, IMG_FLIP_HORIZONTAL);

    // Save the flipped image
    imagejpeg($image, 'flipped_image.jpg');
    imagedestroy($image);
}
?>

3. Adjust image contrast

After flipping the image, we may need to adjust the contrast of the image. PHP's GD library provides the imagefilter() function to apply various image filters, including contrast adjustments. The function prototype is as follows:

 imagefilter(resource $image, int $filtertype, int $arg1 = 0, int $arg2 = 0, int $arg3 = 0, int $arg4 = 0): bool
  • $filtertype is the type of filter, and IMG_FILTER_CONTRAST is used to adjust the contrast.

  • $arg1 is the value of contrast. The value can be a negative (reduce contrast) or a positive (reduce contrast).

Code example:

 <?php
// Loading the image
$image = imagecreatefromjpeg('flipped_image.jpg');

// Adjust the contrast,The value is-50Reduce contrast,The value is50Improve contrast
if ($image) {
    imagefilter($image, IMG_FILTER_CONTRAST, -50);

    // Save the adjusted image
    imagejpeg($image, 'adjusted_contrast_image.jpg');
    imagedestroy($image);
}
?>

4. Complete code example

Combining the above-mentioned flip and adjust contrast functions, here is a complete code example that will first load the image, flip the image, then adjust the contrast of the image, and finally save the final result.

 <?php
// Loading the image
$imagePath = 'path_to_your_image.jpg';
$image = imagecreatefromjpeg($imagePath);

// If loading successfully
if ($image) {
    // Flip the image(Horizontal flip)
    imageflip($image, IMG_FLIP_HORIZONTAL);

    // Adjust the contrast(这里Reduce contrast,The value is负数)
    imagefilter($image, IMG_FILTER_CONTRAST, -50);

    // Save the final image
    imagejpeg($image, 'final_image.jpg');
    
    // Free up resources
    imagedestroy($image);
} else {
    echo '无法Loading the image';
}
?>

5. Conclusion

With the above example, you can easily flip the image using PHP's imageflip() function and adjust the contrast of the image in combination with the imagefilter() function. The GD library provides rich functions for image processing and can be widely used in web development. Hope this article helps you!