Created
May 29, 2012 07:59
-
-
Save keiya/2823192 to your computer and use it in GitHub Desktop.
ImageMagick Filters Test
This file contains hidden or 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 | |
| /* image_filter_test.php | |
| * by Keiya Chinen | |
| * | |
| * https://gist.github.com/2823192 | |
| * | |
| * (MIT Licence) | |
| * 汚いコードだなぁ | |
| * | |
| * Usage: php image_filter_test.php <input_filename> <scale> [result_html_filename] | |
| * ex) php image_filter_test.php testimg.png 0.3 | |
| * | |
| * */ | |
| $usage = "Usage: php image_filter_test.php <input_filename> <scale> [result_html_filename]\n"; | |
| $filename = isset($argv[1]) ? $argv[1] : die("Please specify image filename\n{$usage}"); | |
| $scale = isset($argv[2]) ? $argv[2] : die("Please specify scale\n{$usage}"); | |
| $savehtml = isset($argv[3]) ? $argv[3] : 'image_filter_test.html'; | |
| // array of blur numbers | |
| $blurs = array(0.8,1.0,1.2); | |
| /*-------------- | |
| * main program | |
| *-------------*/ | |
| ini_set('zend.enable_gc',true); | |
| ini_set('memory_limit',-1); | |
| set_time_limit(0); | |
| $filefrag = explode('.',$filename); | |
| $basedir = dirname(__FILE__); | |
| $i = new Imagick(); | |
| $filters = array( | |
| 'FILTER_UNDEFINED'=>imagick::FILTER_UNDEFINED, | |
| 'FILTER_POINT'=>imagick::FILTER_POINT, | |
| 'FILTER_BOX'=>imagick::FILTER_BOX, | |
| 'FILTER_TRIANGLE'=>imagick::FILTER_TRIANGLE, | |
| 'FILTER_HERMITE'=>imagick::FILTER_HERMITE, | |
| 'FILTER_HANNING'=>imagick::FILTER_HANNING, | |
| 'FILTER_HAMMING'=>imagick::FILTER_HAMMING, | |
| 'FILTER_BLACKMAN'=>imagick::FILTER_BLACKMAN, | |
| 'FILTER_GAUSSIAN'=>imagick::FILTER_GAUSSIAN, | |
| 'FILTER_QUADRATIC'=>imagick::FILTER_QUADRATIC, | |
| 'FILTER_CUBIC'=>imagick::FILTER_CUBIC, | |
| 'FILTER_CATROM'=>imagick::FILTER_CATROM, | |
| 'FILTER_MITCHELL'=>imagick::FILTER_MITCHELL, | |
| 'FILTER_LANCZOS'=>imagick::FILTER_LANCZOS, | |
| 'FILTER_BESSEL'=>imagick::FILTER_BESSEL, | |
| 'FILTER_SINC'=>imagick::FILTER_SINC | |
| ); | |
| $i->readImage($basedir.'/'.$filename); | |
| $iden = $i->identifyImage(); | |
| print_r($iden); | |
| $dir = $basedir.'/'.'images'; | |
| if (!file_exists($dir) || !is_dir($dir)) { | |
| if (!mkdir($dir)) die("Could not create directory: {$dir}\n"); | |
| } | |
| echo "---Process Start---\n"; | |
| // scale | |
| resize($scale); | |
| function resize($scale) { | |
| global $i,$blurs,$filters,$filefrag,$iden,$savehtml,$basedir,$filename; | |
| $html = "<!DOCTYPE html><html><head></head><body>\n"; | |
| if ($scale > 1) | |
| $html .= "<h1>Scale Up x{$scale}</h1>\n<table>\n<tr>\n"; | |
| else if ($scale < 1) | |
| $html .= "<h1>Scale Down x{$scale}</h1>\n<table>\n<tr>\n"; | |
| else | |
| return false; | |
| $html .= "<th>filter name</th>"; | |
| foreach ($blurs as $blur) { | |
| $html .= "<th>blur={$blur}</th>\n"; | |
| } | |
| $html .= "</tr>\n"; | |
| $benchmark = array(); | |
| foreach ($filters as $filname => $filval) { | |
| $i->readImage($basedir.'/'.$filename); | |
| $html .= "<tr>\n\t<th>{$filname}</th>\n"; | |
| echo "{$filname}:\n"; | |
| foreach ($blurs as $blur) { | |
| echo " x{$scale} @ blur {$blur}"; | |
| $target_w = $iden['geometry']['width']*$scale; | |
| $target_h = $iden['geometry']['height']*$scale; | |
| $start_time = microtime(true); | |
| $i->resizeImage( | |
| $target_w, | |
| $target_h, | |
| $filval, | |
| $blur | |
| ); | |
| $end_time = microtime(true); | |
| $imgtag_w = $target_w; | |
| if ($target_w > 400) { | |
| $imgtag_w = 400; | |
| $info = 'Click to Open Original'; | |
| } | |
| else { | |
| $info = '(Original)'; | |
| } | |
| $savefilename = $filefrag[0].'-x'.$scale.'-'.strtolower($filname).'-blur_'.$blur.'.'.$filefrag[1]; | |
| $proctime = sprintf("%.3f",((float)$end_time - (float)$start_time) * 1000); | |
| echo "\t[OK] {$proctime}ms\n Saving..."; | |
| $benchmark[$filname][] = $proctime; | |
| $i->writeImage('images/'.$savefilename); | |
| echo "done\n"; | |
| $html .= "\t<td><a href='images/{$savefilename}' target='iftzoom'><img src='images/{$savefilename}' alt='{$filname} {$blur}' width='{$imgtag_w}' /><br />{$info}</a></td>\n"; | |
| } | |
| $html .= "</tr>\n"; | |
| } | |
| $html .= "</table>\n"; | |
| $html .= "<h1>Benchmark</h1>\n<table>\n"; | |
| foreach ($benchmark as $filname => $times) { | |
| $sum = array_sum($times); | |
| $avg = sprintf("%3f",$sum / count($times)); | |
| $html .= "<tr><th>{$filname}</th><td>{$avg}ms</td></tr>\n"; | |
| } | |
| $html .= "</table>\n<hr /><address>Generated with <a href='https://gist.github.com/2823192' target='_blank'>image_filter_tester.php</a> by <a href='http://www.keiyac.org/' target='_blank'>Keiya Chinen</a></address></body></html>"; | |
| file_put_contents($savehtml,$html); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment