In daily development, image processing is a very common requirement, such as image flip (mirror), compressed image size, saving optimized images, etc. In PHP, we can easily implement these features using the built-in GD library.
This article will take you step by step to fully grasp the processing flow of image flip, compression and saving through PHP. Whether you are developing an image upload interface or building an image management system, this article can provide you with practical reference.
First make sure your server has GD library enabled. Check whether the GD library is supported in PHP:
<?php
if (extension_loaded('gd')) {
echo "GD The library is enabled";
} else {
echo "Please install and enable GD Library";
}
?>
Let's use JPEG image as an example to load the image file:
<?php
$imagePath = 'uploads/sample.jpg';
$image = imagecreatefromjpeg($imagePath);
if (!$image) {
die('Failed to load the image');
}
?>
<?php
function flipImageHorizontally($image) {
$width = imagesx($image);
$height = imagesy($image);
$flipped = imagecreatetruecolor($width, $height);
for ($x = 0; $x < $width; $x++) {
imagecopy($flipped, $image, $width - $x - 1, 0, $x, 0, 1, $height);
}
return $flipped;
}
$image = flipImageHorizontally($image);
?>
<?php
function flipImageVertically($image) {
$width = imagesx($image);
$height = imagesy($image);
$flipped = imagecreatetruecolor($width, $height);
for ($y = 0; $y < $height; $y++) {
imagecopy($flipped, $image, 0, $height - $y - 1, 0, $y, $width, 1);
}
return $flipped;
}
// $image = flipImageVertically($image); // Uncomment if vertically flipped
?>
Image compression usually refers to adjusting the quality of the image when saved. For JPEG images, compression quality can be set during saving (0-100):
<?php
$compressedPath = 'uploads/compressed.jpg';
$quality = 75; // The smaller the value,The higher the compression,The lower the image quality
if (!imagejpeg($image, $compressedPath, $quality)) {
die('Failed to compress and save the image');
}
?>
After successful compression and saving, an image access link can be given, for example:
<?php
echo 'Image processing succeeded,Visit link:<a href="https://m66.net/uploads/compressed.jpg">Click to view the picture</a>';
?>
You can also encapsulate the entire process into a function to improve reusability:
<?php
function processImage($inputPath, $outputPath, $flipType = 'horizontal', $quality = 80) {
$image = imagecreatefromjpeg($inputPath);
if (!$image) return false;
if ($flipType === 'horizontal') {
$image = flipImageHorizontally($image);
} elseif ($flipType === 'vertical') {
$image = flipImageVertically($image);
}
return imagejpeg($image, $outputPath, $quality);
}
?>
Through PHP's GD library, we can process images very flexibly, including:
Load image files;
flip (horizontal/vertical);
Compress and save;
Output the processing result.
This process is suitable for most web application scenarios, especially the functions that need to be optimized after users upload images.
If you have more needs for image processing, such as cropping, watermarking, and adjusting the size, the GD library is also competent, and we will also bring more advanced image processing articles in the future. Please stay tuned!