In PHP, the imageflip() function is a useful tool for flipping an image. It can flip the image horizontally, vertically, or both, which is very helpful for image processing tasks such as creating mirror effects, image correction, etc. But looking more deeply, how exactly does it change the pixel points of an image? This article will reveal its specific impact through examples and illustrations.
imageflip() is part of the PHP GD library and is used to flip an image. The function prototype is as follows:
bool imageflip(GdImage $image, int $mode)
where $image is the target image resource, $mode is the flipped mode, and the supported constants are as follows:
IMG_FLIP_HORIZONTAL : Horizontal flip (from left to right)
IMG_FLIP_VERTICAL : Flip vertically (from top to bottom)
IMG_FLIP_BOTH : Flip horizontally and vertically simultaneously
Here is a simple example of how to create an image, flip it, and see the pixel changes before and after flip:
<?php
// Create a simple image resource (3x3Pixels)
$image = imagecreatetruecolor(3, 3);
// Fill color is convenient to distinguish
$colors = [
imagecolorallocate($image, 255, 0, 0), // red
imagecolorallocate($image, 0, 255, 0), // green
imagecolorallocate($image, 0, 0, 255), // blue
];
// 设置Pixels点(Can be regarded as a two-dimensional matrix)
imagesetpixel($image, 0, 0, $colors[0]); // Top left
imagesetpixel($image, 2, 0, $colors[1]); // Top right
imagesetpixel($image, 1, 2, $colors[2]); // Middle and lower
// 输出翻转前的Pixels颜色(For debugging)
function printPixelColors($img) {
for ($y = 0; $y < imagesy($img); $y++) {
for ($x = 0; $x < imagesx($img); $x++) {
$rgb = imagecolorat($img, $x, $y);
$colors = imagecolorsforindex($img, $rgb);
echo "($x,$y): R={$colors['red']} G={$colors['green']} B={$colors['blue']}\n";
}
}
echo "----\n";
}
echo "翻转前Pixels:\n";
printPixelColors($image);
// Perform horizontal flip
imageflip($image, IMG_FLIP_HORIZONTAL);
echo "水平翻转后Pixels:\n";
printPixelColors($image);
// Output flipped image to file
imagepng($image, 'https://m66.net/output/flipped_image.png');
imagedestroy($image);
?>
Assuming that the upper left corner of the original image is a red pixel (0,0) and the upper right corner is a green pixel (2,0) , then after flipping horizontally, they will adjust the position, that is, red will move to (2,0) and green will move to (0,0) . The blue color (1,2) below the middle will also become a symmetrical position (1,2) (the vertical position remains unchanged when flipping horizontally, so the position remains unchanged).
Visual understanding is to reconcile the entire image in front of a mirror.
Image Mirroring Effect : Can be used for preview of images when taking pictures
Image correction : Some image capture devices save images inverted, and can be corrected by imageflip().
Image data augmentation : Flipped images can increase data diversity when training machine learning models
PHP's imageflip() requires GD library support (PHP 5.5.0+ is built-in)
The flip operation directly modifys the original image resource and does not return a new resource.