Advertisement
ossifrage

blur-eval.php

Dec 15th, 2016
1,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.57 KB | None | 0 0
  1. <?php
  2.  
  3. // Sample code to evaluate the sharpness of an image in PHP
  4.  
  5. // Note: this code has not been thoroughly tested, and does not
  6. // perform any kind of input validation. Please don't run this
  7. // on your website and then complain when someone uses it to
  8. // break your server.
  9.  
  10. // For testing purposes only, access the name of the input image
  11. // from the query string (blur-eval.php?f=my_file.jpg)
  12. $image = imagecreatefromjpeg($_GET['f']);
  13.  
  14. $w = imagesx($image);
  15. $h = imagesy($image);
  16.  
  17. // It would take too long to examine the sharpness of every pixel.
  18. // This variable specifies the size of the gaps between rows and
  19. // columns of the pixels we actually check
  20. $s = 10; // grid size
  21.  
  22. $start = microtime(true);
  23.  
  24. $maxdiff = 0;
  25.  
  26. // Check every $s-th row of the image and store the max difference
  27. // between adjacent pixels
  28. // Note: the formula 0.35*$r + 0.50*$g + 0.15*$b converts RGB values into
  29. // perceptual brightness
  30. for ($x=$s/2;$x<=$w-$s/2;$x+=$s) {
  31.   $r = (imagecolorat($image, $x, $s/2) >> 16) & 0xff;
  32.   $g = (imagecolorat($image, $x, $s/2) >> 8) & 0xff;
  33.   $b = (imagecolorat($image, $x, $s/2)) & 0xff;
  34.   $v0 = floor(0.35*$r + 0.50*$g + 0.15*$b);
  35.   for ($y=$s/2+1;$y<=$h-$s/2;$y++) {
  36.     $r = (imagecolorat($image, $x, $y) >> 16) & 0xff;
  37.     $g = (imagecolorat($image, $x, $y) >> 8) & 0xff;
  38.     $b = (imagecolorat($image, $x, $y)) & 0xff;
  39.     $v = floor(0.35*$r + 0.50*$g + 0.15*$b);
  40.     if ($maxdiff<abs($v-$v0)) {
  41.       $maxdiff=abs($v-$v0);
  42.       $xmax = $x;
  43.       $ymax = $y;
  44.     }
  45.     $v0 = $v;
  46.   }
  47. }
  48.  
  49. // Do the same thing on the columns
  50. for ($y=$s/2;$y<=$h-$s/2;$y+=$s) {
  51.   $r = (imagecolorat($image, $s/2, $y) >> 16) & 0xff;
  52.   $g = (imagecolorat($image, $s/2, $y) >> 8) & 0xff;
  53.   $b = (imagecolorat($image, $s/2, $y)) & 0xff;
  54.   $v0 = floor(0.35*$r + 0.50*$g + 0.15*$b);
  55.   for ($x=$s/2+1;$x<=$w-$s/2;$x++) {
  56.     $r = (imagecolorat($image, $x, $y) >> 16) & 0xff;
  57.     $g = (imagecolorat($image, $x, $y) >> 8) & 0xff;
  58.     $b = (imagecolorat($image, $x, $y)) & 0xff;
  59.     $v = floor(0.35*$r + 0.50*$g + 0.15*$b);
  60.     if ($maxdiff<abs($v-$v0)) {
  61.       $maxdiff=abs($v-$v0);
  62.       $xmax = $x;
  63.       $ymax = $y;
  64.     }
  65.     $v0 = $v;
  66.   }
  67. }
  68.  
  69. // Now calculate the range of brightness in the 9x9 block of pixels
  70. // centered on the location of maximum contrast found above
  71. $maxv = 0;
  72. $minv = 255;
  73. for ($x=$xmax-4;$x<=$xmax+4;$x++) {
  74.   for ($y=$ymax-4;$y<=$ymax+4;$y++) {
  75.     $r = (imagecolorat($image, $x, $y) >> 16) & 0xff;
  76.     $g = (imagecolorat($image, $x, $y) >> 8) & 0xff;
  77.     $b = (imagecolorat($image, $x, $y)) & 0xff;
  78.     $v = floor(0.35*$r + 0.50*$g + 0.15*$b);
  79.     if ($v>$maxv) $maxv=$v;
  80.     if ($v<$minv) $minv=$v;
  81.   }
  82. }
  83.  
  84. // Calculate a sharpness value based on this brightness range
  85. $sharpness = ($maxdiff / (15 + $maxv - $minv)) * 27000 / 255;
  86.    
  87. $end =  microtime(true);
  88. $howlong = $end - $start;
  89.  
  90. // Draw on the image to show where the maximum contrast was found, etc.
  91. $bg = imagecolorallocatealpha($image,255,255,255,32);
  92. $fg = imagecolorallocate($image,0,0,0);
  93.  
  94. imagerectangle($image,$xmax-4,$ymax-4,$xmax+4,$ymax+4,$bg);
  95. imageline($image,$xmax-5,$ymax,0,$ymax,$bg);
  96. imageline($image,$xmax+5,$ymax,$w-1,$ymax,$bg);
  97. imageline($image,$xmax,$ymax-5,$xmax,0,$bg);
  98. imageline($image,$xmax,$ymax+5,$xmax,$h-1,$bg);
  99.  
  100.  
  101. $s = "Sharpness: ".number_format($sharpness,1)."%, " . number_format($howlong,3)." seconds";
  102. imagealphablending($image,true);
  103. imagefilledrectangle($image,4,$h-20,10+strlen($s)*7,$h-4,$bg);
  104.  
  105.  
  106. imagestring($image, 2, 8, $h-18, $s,$fg);
  107.  
  108. header('Content-Type: image/jpeg');
  109. imagejpeg($image);
  110. imagedestroy($image);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement