Welcome to your PHP Image gamma correct() tutorial! In this lesson, we'll explore how to adjust the brightness and contrast of an image using gamma correction in PHP.
By the end of this tutorial, you'll be able to:
imagegammacorrect() functionLet's dive in! π―
Gamma correction is a method used to adjust the perceived brightness and contrast of an image to more closely match human vision. Our eyes perceive light non-linearly, meaning that small increases in light levels don't appear as large as larger increases. Gamma correction compensates for this by adjusting the brightness and contrast of an image to better match human perception.
π‘ Pro Tip: Gamma correction is crucial for ensuring images look consistent across different devices and platforms.
imagegammacorrect() FunctionThe PHP imagegammacorrect() function allows you to adjust the gamma value of an image, which in turn changes the image's brightness and contrast. The gamma value is a multiplier that affects the brightness of the image. A gamma value greater than 1 will make the image brighter, while a value less than 1 will make it darker.
Here's the syntax for the imagegammacorrect() function:
int imagegammacorrect ( resource $image, float $gamma )$image: The image resource you want to adjust$gamma: The new gamma value (between 0 and 2)Let's create a simple PHP script to adjust the gamma correction of an image.
<?php
// Include the GD library
ini_set('allow_url_fopen', true);
$image = imagecreatefromjpeg('example.jpg');
// Set the new gamma value (between 0 and 2)
$gamma = 1.5;
// Adjust the gamma correction
imagegammacorrect($image, $gamma);
// Save the adjusted image
imagejpeg($image, 'gamma_example.jpg');
// Free memory
imagedestroy($image);
?>
π Note: Replace example.jpg with the path to your own image file.
This script loads an image, sets a new gamma value, applies the gamma correction, and saves the adjusted image as gamma_example.jpg.
In a real-world project, you might want to create an image upload form that allows users to adjust the gamma correction before saving the image. Here's a simple example using HTML, PHP, and JavaScript:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- ... -->
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" id="image">
<label for="gamma">Adjust gamma:</label>
<input type="range" id="gamma" name="gamma" min="0" max="2" value="1" step="0.1">
<input type="submit" value="Upload & Adjust">
</form>
<script>
// Add event listener for gamma input change
document.getElementById('gamma').addEventListener('change', function() {
document.getElementById('image').setAttribute('data-gamma', this.value);
});
</script>
</body>
</html>This HTML includes a simple file upload form with an input range for gamma correction. When the gamma value is changed, the new value is stored as a data attribute on the image input.
<?php
// ...
$image = imagecreatefromjpeg($_FILES['image']['tmp_name']);
$gamma = $_FILES['image']['data-gamma'] ?? 1;
// Apply gamma correction
imagegammacorrect($image, $gamma);
// Save the adjusted image
// ...
?>In the PHP script, we retrieve the gamma value from the image input's data attribute and apply the gamma correction as before.
π‘ Pro Tip: This simple example can be expanded to include error handling, form validation, and more advanced gamma correction options.
What does the PHP `imagegammacorrect()` function do?
By now, you should have a solid understanding of PHP image gamma correction. Happy coding! π