Mirroring or symmetrical effects are a common and artistic technique when performing image processing. PHP provides a concise function - imageflip() , which can easily flip horizontally, vertically or simultaneously images, creating a symmetrical artistic effect. This article will take you through the basic usage of imageflip() and demonstrate with a complete example how to use it to create a symmetric image.
imageflip() is a function used for image flipping in PHP's GD library. Its syntax is as follows:
bool imageflip(GdImage $image, int $mode)
$image : The image resource to be processed.
$mode : Flip mode, which can be one of the following three:
IMG_FLIP_HORIZONTAL : Flip horizontally (mirror left and right).
IMG_FLIP_VERTICAL : Flip vertically (mirror up and down).
IMG_FLIP_BOTH : Flip horizontally and vertically at the same time.
This function is introduced from PHP 5.5.0, so make sure your running environment supports it.
Suppose we have an image and we want to make a left-right symmetrical image by flipping horizontally, by:
Divide the original image into half.
Copy this half of the image.
Use imageflip() to flip horizontally.
Splice the flipped part next to the original image to create a symmetric effect.
Here is a complete PHP sample code that implements the above operation:
<?php
// Load the original image
$sourcePath = 'https://m66.net/images/sample.jpg';
$sourceImage = imagecreatefromjpeg($sourcePath);
$width = imagesx($sourceImage);
$height = imagesy($sourceImage);
// Create half the canvas
$halfWidth = (int)($width / 2);
$halfImage = imagecreatetruecolor($halfWidth, $height);
// Copy the left half
imagecopy($halfImage, $sourceImage, 0, 0, 0, 0, $halfWidth, $height);
// Create a flipped copy
$flippedHalf = imagecreatetruecolor($halfWidth, $height);
imagecopy($flippedHalf, $halfImage, 0, 0, 0, 0, $halfWidth, $height);
imageflip($flippedHalf, IMG_FLIP_HORIZONTAL);
// Create a new canvas,Used to place symmetrical images
$symmetryImage = imagecreatetruecolor($width, $height);
// Merge the original left half and flip right half
imagecopy($symmetryImage, $halfImage, 0, 0, 0, 0, $halfWidth, $height);
imagecopy($symmetryImage, $flippedHalf, $halfWidth, 0, 0, 0, $halfWidth, $height);
// Output to browser
header('Content-Type: image/jpeg');
imagejpeg($symmetryImage);
// Clean up resources
imagedestroy($sourceImage);
imagedestroy($halfImage);
imagedestroy($flippedHalf);
imagedestroy($symmetryImage);
?>
After running the above code, you will get an image that is symmetrical left and right. For example, if the original image is a portrait of a character, the output image will show a perfectly symmetrical "mirror face", which is very artistic.
If you want to achieve up and down symmetry, just replace IMG_FLIP_HORIZONTAL with IMG_FLIP_VERTICAL .
Imageflip() image processing is performed in place, so it is recommended to copy the original image and then process it to avoid modifying the source file.
imagecreatefromjpeg() can also be replaced with imagecreatefrommpng() or imagecreatefromgif() , depending on your image format.
imageflip() is an extremely practical PHP image processing tool. With the basic GD library functions, you can easily achieve various symmetrical artistic effects, suitable for scenes such as avatar processing, creative images, puzzle games, and even generating AI visual materials. Next time you want to add a little "mirror magic" to your image, try it!