When working with images, sometimes we encounter problems with incorrect orientation of the image. Especially when taking photos with a smartphone, the direction information of the picture is often stored in the EXIF data. In order to display the picture correctly, we need to adjust the orientation of the picture based on this information. PHP provides an imageflip function to handle image flip operations, and there is also an exif_read_data function that can read EXIF data to obtain the direction of the image. This article will introduce how to combine these two functions to fix the flip direction of the image.
EXIF (Exchangeable Image File Format) is part of the image file, which contains a variety of information about image shooting conditions, camera settings, etc. In JPEG format pictures, the EXIF data usually stores the direction information of the picture, specifically in the Orientation tag. The value of Orientation represents the rotation angle or flip of the picture relative to the normal shooting direction.
Common Orientation values are as follows:
1: Normal (no rotation or flip)
3: Rotate 180 degrees
6: Rotate 90 degrees clockwise
8: Rotate 90 degrees counterclockwise
2: Vertical flip
4: Horizontal flip
5: Flip vertically and rotate 90 degrees clockwise
7: Flip horizontally and rotate 90 degrees clockwise
Therefore, we can judge whether the picture needs to be flipped and how to flip based on the Orientation value in the EXIF data.
The exif_read_data function can read EXIF data in the image file. We can obtain Orientation information through this function to judge the direction of the picture.
Here is an example code for reading EXIF data:
<?php
$file = 'path/to/your/image.jpg';
// Check if the file exists
if (file_exists($file)) {
// ReadEXIFdata
$exif = exif_read_data($file);
// ifEXIFdata包含OrientationFields
if (isset($exif['Orientation'])) {
echo 'Orientation: ' . $exif['Orientation'];
} else {
echo 'Not foundOrientationFields';
}
}
?>
This code reads the EXIF data in the image file and outputs the value of the Orientation field. If this field does not exist in the EXIF data, it means that the image does not contain direction information.
imageflip is a GD library function in PHP that can be used to flip images. Based on the Orientation value in the EXIF data, we can decide how to use imageflip to fix the image orientation.
Here is a complete code example that combines exif_read_data and imageflip functions to fix image orientation:
<?php
function fixImageOrientation($file) {
// Check if the file exists
if (file_exists($file)) {
// Get the pictureEXIFdata
$exif = exif_read_data($file);
// ifEXIFdata包含OrientationFields
if (isset($exif['Orientation'])) {
// GetOrientationValue of
$orientation = $exif['Orientation'];
// Loading pictures
$image = imagecreatefromjpeg($file);
// according toOrientationValue to fix image orientation
switch ($orientation) {
case 3:
// Rotate180Degree
imageflip($image, IMG_FLIP_BOTH);
break;
case 6:
// 顺时针Rotate90Degree
$image = imagerotate($image, -90, 0);
break;
case 8:
// 逆时针Rotate90Degree
$image = imagerotate($image, 90, 0);
break;
case 2:
// Vertical flip
imageflip($image, IMG_FLIP_VERTICAL);
break;
case 4:
// Horizontal flip
imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 5:
// Vertical flip并Rotate90Degree顺时针
imageflip($image, IMG_FLIP_VERTICAL);
$image = imagerotate($image, -90, 0);
break;
case 7:
// Horizontal flip并Rotate90Degree顺时针
imageflip($image, IMG_FLIP_HORIZONTAL);
$image = imagerotate($image, -90, 0);
break;
default:
// No repair required
break;
}
// Save the repaired image
imagejpeg($image, 'path/to/save/fixed_image.jpg');
// Free image memory
imagedestroy($image);
echo "The image direction is successfully repaired!";
} else {
echo "NoOrientationFields,No repair required图片方向。";
}
} else {
echo "The image file does not exist。";
}
}
// Calling a function to fix the image direction
fixImageOrientation('path/to/your/image.jpg');
?>
In this code, first read the EXIF data of the picture through the exif_read_data function and get the value of the Orientation field. Then based on this value, determine how to use imageflip and imagerotate to repair the orientation of the image. The fixed image will be saved to the specified path.