📅  最后修改于: 2023-12-03 14:45:20.228000             🧑  作者: Mango
The contrastStretchImage()
function is a powerful image processing function in PHP that can be used to enhance the contrast of an image. This function is often used in computer vision and image processing applications to improve the visibility of details in an image.
/**
* Enhances the contrast of an image using contrast stretching.
*
* @param string $imagePath - Path to the source image file.
* @param float $blackPoint - Percentage of pixels to clip to black (0-100).
* @param float $whitePoint - Percentage of pixels to clip to white (0-100).
* @return void
*/
function contrastStretchImage(string $imagePath, float $blackPoint, float $whitePoint)
This function takes three parameters:
$imagePath
: Path to the source image file.$blackPoint
: Percentage of pixels to clip to black (0-100). Lower values increase the darkness of shadows.$whitePoint
: Percentage of pixels to clip to white (0-100). Higher values increase the brightness of highlights.The first step in implementing the contrastStretchImage()
function is to load the source image using the GD extension in PHP.
// Load the source image
$sourceImage = imagecreatefromjpeg($imagePath);
// Get image width and height
$imageWidth = imagesx($sourceImage);
$imageHeight = imagesy($sourceImage);
Next, we calculate the number of pixels to clip from the black and white ends based on the provided black and white point percentages.
// Calculate the number of pixels to clip
$blackPixels = ($blackPoint / 100) * ($imageWidth * $imageHeight);
$whitePixels = ($whitePoint / 100) * ($imageWidth * $imageHeight);
To enhance the contrast of the image, we iterate through each pixel and adjust its intensity based on the black and white point percentages.
// Adjust pixel intensities
for ($y = 0; $y < $imageHeight; $y++) {
for ($x = 0; $x < $imageWidth; $x++) {
// Get the RGB values of the current pixel
$rgb = imagecolorat($sourceImage, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
// Clip pixels to black
if (--$blackPixels >= 0) {
$r = 0;
$g = 0;
$b = 0;
}
// Clip pixels to white
if (--$whitePixels >= 0) {
$r = 255;
$g = 255;
$b = 255;
}
// Set the new RGB values for the current pixel
imagesetpixel($sourceImage, $x, $y, imagecolorallocate($sourceImage, $r, $g, $b));
}
}
Finally, we save the enhanced image and clean up resources.
// Save the enhanced image
imagejpeg($sourceImage, $newImagePath, 100);
// Clean up resources
imagedestroy($sourceImage);
// Apply contrast stretching to an image
contrastStretchImage('path/to/source/image.jpg', 5, 95);
This example will enhance the contrast of the source image, clipping the darkest 5% of pixels to black and the brightest 95% of pixels to white.
The contrastStretchImage()
function in PHP provides a convenient way to enhance the contrast of images using contrast stretching. By adjusting the black and white point percentages, you can control the darkness of shadows and the brightness of highlights. This function can be used in various applications, such as image editing, computer vision, and image analysis.