Image processing is a very common requirement in web development. As a backend language, PHP provides a powerful GD library to support various operations of images. This article will introduce how to use PHP's built-in imageflip function to flip images and combine compression technology to effectively reduce the size of image files.
imageflip is a function introduced in PHP 5.5.0 and above, used to flip image resources. It supports horizontal flip, vertical flip, and simultaneously horizontal and vertical flip.
Function prototype:
bool imageflip ( resource $image , int $mode )
$image : Image resource that needs to be flipped.
$mode : flip mode, optional value:
IMG_FLIP_HORIZONTAL (Horizontal Flip)
IMG_FLIP_VERTICAL (Vertical Flip)
IMG_FLIP_BOTH (simultaneously flip horizontally and vertically)
Suppose there is an image called input.jpg , and the following shows how to flip the image horizontally:
<?php
// 1. Read image files
$imagePath = 'input.jpg';
$image = imagecreatefromjpeg($imagePath);
if (!$image) {
die('Unable to load the picture');
}
// 2. use imageflip Flip the image(Horizontal flip)
if (!imageflip($image, IMG_FLIP_HORIZONTAL)) {
imagedestroy($image);
die('Image flip failed');
}
// 3. Save the flipped image
$outputPath = 'flipped.jpg';
if (!imagejpeg($image, $outputPath)) {
imagedestroy($image);
die('Failed to save the image');
}
// 4. Free up resources
imagedestroy($image);
echo "Picture flip successfully,Saved as:{$outputPath}";
?>
The imagejpeg function in PHP supports setting compression quality, ranging from 0 (worst quality, minimum file) to 100 (best quality, maximum file). Appropriate adjustment of compression quality can effectively reduce the size of the picture while maintaining a good visual effect.
Modify the saved part in the above code:
// Set the compression quality to 75
$quality = 75;
if (!imagejpeg($image, $outputPath, $quality)) {
imagedestroy($image);
die('Failed to save the image');
}
Here is a comprehensive example that demonstrates the reading of an image, and after being flipped horizontally, compressed and saved:
<?php
$imagePath = 'input.jpg';
$outputPath = 'flipped_compressed.jpg';
// 1. Loading the image
$image = imagecreatefromjpeg($imagePath);
if (!$image) {
die('Unable to load the picture');
}
// 2. Flip the image(Vertical flip example)
if (!imageflip($image, IMG_FLIP_VERTICAL)) {
imagedestroy($image);
die('Image flip failed');
}
// 3. Save the image by setting compression quality
$quality = 70; // Can be adjusted according to requirements
if (!imagejpeg($image, $outputPath, $quality)) {
imagedestroy($image);
die('Failed to save the image');
}
// 4. Free up resources
imagedestroy($image);
echo "The image has been flipped successfully and compressed,Save the path:{$outputPath}";
?>
Using the imageflip function, you can quickly achieve horizontal, vertical or bidirectional flip of an image.
Through the third parameter of the imagejpeg function, the compression quality of the output JPEG image can be controlled, thereby reducing the file size.
Rationally adjusting the compression quality can significantly reduce file size while maintaining the visual effect of the picture and improving website loading speed.
For more PHP image processing tutorials, please visit https://gitbox.net/tutorials/php-image-processing