In daily life, selfies have become a way for many people to record their lives. However, the photos taken by the front camera of the mobile phone are often mirrored, which may be inconvenient for some users. This article will teach you how to use PHP's built-in imageflip() function to quickly develop a "flip selfie" gadget to help users restore real perspectives.
First, you need to have a PHP-enabled server environment and enable the GD library (PHP image processing extension). You can check whether the GD library is enabled using the following method:
<?php
phpinfo();
?>
Search for "GD Support" in the output page to confirm whether it is "enabled".
We will build a simple web page where users can upload selfies, the system will automatically flip horizontally and display the results.
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Flip selfie tool</title>
</head>
<body>
<h2>Upload selfies and flip them</h2>
<form action="flip.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" accept="image/*" required>
<button type="submit">Upload and flip</button>
</form>
</body>
</html>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$file = $_FILES['image']['tmp_name'];
$mime = mime_content_type($file);
switch ($mime) {
case 'image/jpeg':
$image = imagecreatefromjpeg($file);
$ext = 'jpg';
break;
case 'image/png':
$image = imagecreatefrompng($file);
$ext = 'png';
break;
case 'image/gif':
$image = imagecreatefromgif($file);
$ext = 'gif';
break;
default:
die("Unsupported image formats!");
}
// use imageflip Perform horizontal flip
imageflip($image, IMG_FLIP_HORIZONTAL);
// Generate unique file name
$outputName = 'flipped_' . time() . '.' . $ext;
$outputPath = __DIR__ . '/uploads/' . $outputName;
// Make sure the upload directory exists
if (!is_dir(__DIR__ . '/uploads')) {
mkdir(__DIR__ . '/uploads', 0755, true);
}
// Save the flipped image
switch ($ext) {
case 'jpg':
imagejpeg($image, $outputPath);
break;
case 'png':
imagepng($image, $outputPath);
break;
case 'gif':
imagegif($image, $outputPath);
break;
}
imagedestroy($image);
// Show results
$url = 'https://m66.net/uploads/' . $outputName;
echo "<h3>Flipped selfie:</h3>";
echo "<img src='$url' alt='Flipped Image' style='max-width: 100%; height: auto;'>";
echo "<p><a href='index.html'>Do it again</a></p>";
} else {
echo "Please upload pictures through the form。";
}
?>
For security reasons, more verifications can be made on uploaded file size, MIME type, suffix, etc.
It can combine JavaScript to achieve real-time preview and front-end flip to improve user experience.
You can allow more users to access your tool by deploying it on https://m66.net/ .
Using PHP's imageflip() function, we can easily implement horizontal or vertical flip of the image. This "flip selfie" gadget is not only practical, but also very easy to expand. Whether it is used for personal websites or integrated into larger systems, it can bring users a more considerate experience.
I hope this article can help you easily create your own image processing widget! If you have other ideas, such as adding filters, watermarks, etc., you can continue to expand on this basis.