Last active
March 14, 2019 01:40
-
-
Save justincarlson/1d104bc139310250d2039468eb57f191 to your computer and use it in GitHub Desktop.
Example PHP Code that tries to detect image sharpness and displays point of max contrast.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Sample code to evaluate the sharpness of an image in PHP | |
// @author https://pastebin.com/u/ossifrage | |
// Note: this code has not been thoroughly tested, and does not | |
// perform any kind of input validation. Please don't run this | |
// on your website and then complain when someone uses it to | |
// break your server. | |
// For testing purposes only, access the name of the input image | |
// from the query string (blur-eval.php?f=my_file.jpg) | |
$image = imagecreatefromjpeg($_GET['f']); | |
$w = imagesx($image); | |
$h = imagesy($image); | |
// It would take too long to examine the sharpness of every pixel. | |
// This variable specifies the size of the gaps between rows and | |
// columns of the pixels we actually check | |
$s = 10; // grid size | |
$start = microtime(true); | |
$maxdiff = 0; | |
// Check every $s-th row of the image and store the max difference | |
// between adjacent pixels | |
// Note: the formula 0.35*$r + 0.50*$g + 0.15*$b converts RGB values into | |
// perceptual brightness | |
for ($x=$s/2;$x<=$w-$s/2;$x+=$s) { | |
$r = (imagecolorat($image, $x, $s/2) >> 16) & 0xff; | |
$g = (imagecolorat($image, $x, $s/2) >> 8) & 0xff; | |
$b = (imagecolorat($image, $x, $s/2)) & 0xff; | |
$v0 = floor(0.35*$r + 0.50*$g + 0.15*$b); | |
for ($y=$s/2+1;$y<=$h-$s/2;$y++) { | |
$r = (imagecolorat($image, $x, $y) >> 16) & 0xff; | |
$g = (imagecolorat($image, $x, $y) >> 8) & 0xff; | |
$b = (imagecolorat($image, $x, $y)) & 0xff; | |
$v = floor(0.35*$r + 0.50*$g + 0.15*$b); | |
if ($maxdiff<abs($v-$v0)) { | |
$maxdiff=abs($v-$v0); | |
$xmax = $x; | |
$ymax = $y; | |
} | |
$v0 = $v; | |
} | |
} | |
// Do the same thing on the columns | |
for ($y=$s/2;$y<=$h-$s/2;$y+=$s) { | |
$r = (imagecolorat($image, $s/2, $y) >> 16) & 0xff; | |
$g = (imagecolorat($image, $s/2, $y) >> 8) & 0xff; | |
$b = (imagecolorat($image, $s/2, $y)) & 0xff; | |
$v0 = floor(0.35*$r + 0.50*$g + 0.15*$b); | |
for ($x=$s/2+1;$x<=$w-$s/2;$x++) { | |
$r = (imagecolorat($image, $x, $y) >> 16) & 0xff; | |
$g = (imagecolorat($image, $x, $y) >> 8) & 0xff; | |
$b = (imagecolorat($image, $x, $y)) & 0xff; | |
$v = floor(0.35*$r + 0.50*$g + 0.15*$b); | |
if ($maxdiff<abs($v-$v0)) { | |
$maxdiff=abs($v-$v0); | |
$xmax = $x; | |
$ymax = $y; | |
} | |
$v0 = $v; | |
} | |
} | |
// Now calculate the range of brightness in the 9x9 block of pixels | |
// centered on the location of maximum contrast found above | |
$maxv = 0; | |
$minv = 255; | |
for ($x=$xmax-4;$x<=$xmax+4;$x++) { | |
for ($y=$ymax-4;$y<=$ymax+4;$y++) { | |
$r = (imagecolorat($image, $x, $y) >> 16) & 0xff; | |
$g = (imagecolorat($image, $x, $y) >> 8) & 0xff; | |
$b = (imagecolorat($image, $x, $y)) & 0xff; | |
$v = floor(0.35*$r + 0.50*$g + 0.15*$b); | |
if ($v>$maxv) $maxv=$v; | |
if ($v<$minv) $minv=$v; | |
} | |
} | |
// Calculate a sharpness value based on this brightness range | |
$sharpness = ($maxdiff / (15 + $maxv - $minv)) * 27000 / 255; | |
$end = microtime(true); | |
$howlong = $end - $start; | |
// Draw on the image to show where the maximum contrast was found, etc. | |
$bg = imagecolorallocatealpha($image,255,255,255,32); | |
$fg = imagecolorallocate($image,0,0,0); | |
imagerectangle($image,$xmax-4,$ymax-4,$xmax+4,$ymax+4,$bg); | |
imageline($image,$xmax-5,$ymax,0,$ymax,$bg); | |
imageline($image,$xmax+5,$ymax,$w-1,$ymax,$bg); | |
imageline($image,$xmax,$ymax-5,$xmax,0,$bg); | |
imageline($image,$xmax,$ymax+5,$xmax,$h-1,$bg); | |
$s = "Sharpness: ".number_format($sharpness,1)."%, " . number_format($howlong,3)." seconds"; | |
imagealphablending($image,true); | |
imagefilledrectangle($image,4,$h-20,10+strlen($s)*7,$h-4,$bg); | |
imagestring($image, 2, 8, $h-18, $s,$fg); | |
header('Content-Type: image/jpeg'); | |
imagejpeg($image); | |
imagedestroy($image); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment