Current Location: Home> Latest Articles> How to keep the image coordinate system consistent after flipping an image

How to keep the image coordinate system consistent after flipping an image

M66 2025-05-31

When processing images using PHP's GD library, imageflip() is a very convenient function that quickly flips images horizontally, vertically, or simultaneously. However, after flipping the image, if there are labels, coordinate points, or other position marks based on the image coordinate system on the image, a problem will be faced: the original point no longer corresponds to the same position on the image.

This article will introduce how to correctly adjust the coordinate system after using the imageflip() function so that the annotations or elements on the image are still displayed in the expected position.

imageflip introduction

PHP's imageflip() function is provided since PHP 5.5 and is used to flip images:

 bool imageflip ( GdImage $image , int $mode )

where $mode can be one of the following:

  • IMG_FLIP_HORIZONTAL : Horizontal flip

  • IMG_FLIP_VERTICAL : vertical flip

  • IMG_FLIP_BOTH : Flip horizontally and vertically simultaneously

Problem Scenario Description

Suppose we have a picture marked with a coordinate point (x, y) . We use imageflip() to flip the image horizontally ( IMG_FLIP_HORIZONTAL ), and at this time the image pixel where (x, y) is also flipped to the other side, and the original coordinates are inaccurate.

For example:

  • The width of the original image is 500px

  • The original coordinate point is (100, 200)

  • After flipping, the point should actually be (500 - 100 = 400, 200)

Solution: Remap coordinate points

New coordinate calculation method after horizontal flip:

 $newX = $imageWidth - $oldX;
$newY = $oldY;

New coordinates after vertical flip:

 $newX = $oldX;
$newY = $imageHeight - $oldY;

Flip at the same time:

 $newX = $imageWidth - $oldX;
$newY = $imageHeight - $oldY;

Sample code

Here is a complete example showing how to flip an image and correct coordinates:

 <?php
// Load the original image
$imagePath = 'https://m66.net/images/sample.jpg';
$image = imagecreatefromjpeg($imagePath);

// Get image width and height
$width = imagesx($image);
$height = imagesy($image);

// Original coordinate point
$oldX = 100;
$oldY = 200;

// Perform horizontal flip
imageflip($image, IMG_FLIP_HORIZONTAL);

// New coordinate calculation
$newX = $width - $oldX;
$newY = $oldY;

// Mark new coordinate points(red)
$red = imagecolorallocate($image, 255, 0, 0);
imagefilledellipse($image, $newX, $newY, 10, 10, $red);

// Output image
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
?>

Tips

  • The coordinate conversion logic is closely related to the image flip pattern and must be matched accurately.

  • When multiple coordinate points need to be converted in batches, it is recommended to encapsulate them into functions for processing.

  • If the image content is from a user upload, be sure to verify the image size and type first to avoid security issues.

Conclusion

imageflip() is a very efficient tool for processing images, but to keep the "coordinate meaning" in the image consistent, we must actively calculate and update the coordinate position after flip. By mastering this technique, you can implement more flexible image processing functions in PHP, such as interactive marking, image annotation tools or graphical positioning systems.