function collect_files($dir) { global $files; $handle = opendir($dir); while (false !== ($file = readdir($handle))) { // Skip current and parent directory // also skip other directories which may contain source code // that should not be translated (the directories normally contain // temporary results etc.) // Please note that these directories will be skipped on all levels if ('.' == $file || '..' == $file || 'lang' == $file || 'templates_c' == $file || 'dump' == $file || 'temp' == $file || 'img' == $file || 'cache' == $file) { continue; } $filepath = $dir . '/' . $file; if (preg_match("/.*\\.(tpl|php)\$/", $file)) { print "File: {$filepath}<br />"; $files[] = $filepath; } else { if (is_dir($filepath)) { collect_files($filepath); } } } closedir($handle); }
function collect_files(Filesystem $fs, $path, $already_inside_test_dir = false) { $basename = basename($path); $is_test_dir = $already_inside_test_dir || preg_match('/^(test|spec)/', $basename); $pattern = '/' . ($is_test_dir ? '' : '(_test|_spec|Test|Spec)') . '\\.php$/'; $test_files = []; foreach ($fs->enumerate($path) as $file) { if ($file->isDot()) { continue; } if ($file->isFile() && preg_match($pattern, $file->getBasename())) { $test_files[] = $file->getPathname(); } else { if ($file->isDir()) { $test_files = array_merge($test_files, collect_files($fs, $file->getPathname(), $is_test_dir)); } } } return $test_files; }
function main() { $is_error = false; //no errors yet . . . $continue = false; //continue on to next image in directory? // set directory of images to work in if (isset($_GET['directory']) && isset($_GET['action'])) { $directory = $_GET['directory']; $action = $_GET['action']; } else { // if no directory set, then display form to set the directory return <<<OUTPUT <form action="" method="GET"> \tDirectory: <input type="text" name="directory"> \t<input type="radio" name="action" value="difference_series">Difference series \t<input type="radio" name="action" value="average">Average \t<input type="radio" name="action" value="threshold">Threshold \t<input type="radio" name="action" value="to_data">To Data \t<input type="radio" name="action" value="image_to_hough">To Hough Transform \t<input type="submit" value="GO"> </form> OUTPUT; } // set number of file in directory to start on if (isset($_GET['number'])) { $number = $_GET['number']; } else { $number = 0; } // start at the first file //compile list of files in directory $file_list = collect_files($directory); if (count($file_list) == 0) { return "Directory {$directory} is empty."; } //couldn't find anything $output = ''; switch ($action) { case 'to_data': if (!isset($file_list[$number])) { $output .= 'Finished!'; //all done! break; } //if $image = imagecreatefrompng($directory . $file_list[$number]); $data = image_to_data($image); break; case 'difference_series': if (!isset($file_list[$number]) || !isset($file_list[$number + 1])) { $output .= 'No more files to compare.'; //all done! break; } //if $a = imagecreatefromjpeg($directory . $file_list[$number]); $b = imagecreatefromjpeg($directory . $file_list[$number + 1]); if (!($image = image_combine($a, $b, 'difference'))) { $output .= 'ERROR trying to combine images.'; $is_error = true; break; } //if write_image($image, 'images/output/', '(' . $file_list[$number] . ')-(' . $file_list[$number + 1] . ')', 'png'); $continue = true; break; case 'image_to_hough': if (!isset($file_list[$number])) { $output .= 'No more files.'; //done! break; } //if $image = imagecreatefrompng($directory . $file_list[$number]); $image = image_to_hough($image); write_image($image, 'images/output/', "hough({$file_list[$number]})", 'png'); $continue = true; break; case 'average': if (!isset($file_list[$number])) { $output .= 'No more files to add.'; //all done! break; } //if $temp_path = 'images/temp/'; $temp_filename = 'temp'; //$image = imagecreatefromjpeg($directory . $file_list[$number]); $image = imagecreatefrompng($directory . $file_list[$number]); if (file_exists($temp_path . $temp_filename . '.png')) { $temp = imagecreatefrompng($temp_path . $temp_filename . '.png'); } else { $temp = imagecreatetruecolor(imagesx($image), imagesy($image)); } $num_in_series = count($file_list); $output .= "adding 1/{$num_in_series}th of image to temp.png"; $constant = 1.0 / $num_in_series; $image = image_apply_constant($image, $constant, 'multiply'); $image = image_combine($temp, $image, 'sum'); write_image($image, $temp_path, $temp_filename, 'png'); $continue = true; break; case 'sum_series': break; case 'threshold': if (!isset($file_list[$number])) { $output .= 'No more files.'; //all done! break; } //if $image = imagecreatefrompng($directory . $file_list[$number]); $image = image_apply_constant($image, 34, 'threshold'); write_image($image, 'images/output/', "thresh({$file_list[$number]})", 'png'); $continue = true; break; } //switch $next = $number + 1; if ($is_error) { $output .= <<<OUTPUT <br>Ohhh crap, there was an error performing the action {$action} on directory {$directory}<br><br> Try next pair? <a href="?directory={$directory}&action={$action}&number={$next}">Sure.</a> OUTPUT; $continue = false; } else { $output .= <<<OUTPUT Success.<br> OUTPUT; } //if if ($continue) { $output .= <<<OUTPUT Loading next image . . . <meta http-equiv="refresh" content="0;?number={$next}&action={$action}&directory={$directory}">\t\t OUTPUT; } //if return $output; }
<?php session_start(); require "_config.php"; $line_colors = preg_split("/,\\s*?/", CODE_LINE_COLORS); $char_colors = preg_split("/,\\s*?/", CODE_CHAR_COLORS); $fonts = collect_files(PATH_TTF, "ttf"); $img = imagecreatetruecolor(CODE_WIDTH, CODE_HEIGHT); imagefilledrectangle($img, 0, 0, CODE_WIDTH - 1, CODE_HEIGHT - 1, gd_color(CODE_BG_COLOR)); //$fonts = imageloadfont($fonts); // Draw lines for ($i = 0; $i < CODE_LINES_COUNT; $i++) { imageline($img, rand(0, CODE_WIDTH - 1), rand(0, CODE_HEIGHT - 1), rand(0, CODE_WIDTH - 1), rand(0, CODE_HEIGHT - 1), gd_color($line_colors[rand(0, count($line_colors) - 1)])); } // Draw code $code = ""; $y = CODE_HEIGHT / 2 + CODE_FONT_SIZE / 2; for ($i = 0; $i < CODE_CHARS_COUNT; $i++) { $color = gd_color($char_colors[rand(0, count($char_colors) - 1)]); $angle = rand(-30, 30); $char = substr(CODE_ALLOWED_CHARS, rand(0, strlen(CODE_ALLOWED_CHARS) - 1), 1); $font = PATH_TTF . "/" . $fonts[rand(0, count($fonts) - 1)]; $x = intval(CODE_WIDTH / CODE_CHARS_COUNT * $i) + CODE_FONT_SIZE / 2; $code .= $char; imagettftext($img, CODE_FONT_SIZE, $angle, $x, $y, $color, $font, $char); } $_SESSION['__img_code__'] = md5($code); header("Content-Type: image/png"); imagepng($img); imagedestroy($img); function gd_color($html_color)
function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '') { $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200); foreach ($defaults as $key => $val) { if (!is_array($data)) { if (!isset(${$key}) or ${$key} == '') { ${$key} = $val; } } else { ${$key} = !isset($data[$key]) ? $val : $data[$key]; } } if ($img_path == '' or $img_url == '') { return FALSE; } if (!@is_dir($img_path)) { return FALSE; } if (!is_writable($img_path)) { return FALSE; } if (!extension_loaded('gd')) { return FALSE; } // ----------------------------------- // Remove old images // ----------------------------------- list($usec, $sec) = explode(" ", microtime()); $now = (double) $usec + (double) $sec; $current_dir = @opendir($img_path); while ($filename = @readdir($current_dir)) { if ($filename != "." and $filename != ".." and $filename != "index.html") { $name = str_replace(".jpg", "", $filename); if ($name + $expiration < $now) { @unlink($img_path . $filename); } } } @closedir($current_dir); $line_colors = preg_split("/,\\s*?/", CODE_LINE_COLORS); $char_colors = preg_split("/,\\s*?/", CODE_CHAR_COLORS); $fonts = collect_files(PATH_TTF, "ttf"); $img = imagecreatetruecolor(CODE_WIDTH, CODE_HEIGHT); imagefilledrectangle($img, 0, 0, CODE_WIDTH - 1, CODE_HEIGHT - 1, gd_color(CODE_BG_COLOR)); // Draw lines for ($i = 0; $i < CODE_LINES_COUNT; $i++) { imageline($img, rand(0, CODE_WIDTH - 1), rand(0, CODE_HEIGHT - 1), rand(0, CODE_WIDTH - 1), rand(0, CODE_HEIGHT - 1), gd_color($line_colors[rand(0, count($line_colors) - 1)])); } // Draw code $code = ""; $y = CODE_HEIGHT / 2 + CODE_FONT_SIZE / 2; for ($i = 0; $i < CODE_CHARS_COUNT; $i++) { $color = gd_color($char_colors[rand(0, count($char_colors) - 1)]); $angle = rand(-30, 30); $char = substr(CODE_ALLOWED_CHARS, rand(0, strlen(CODE_ALLOWED_CHARS) - 1), 1); $font = PATH_TTF . "/" . $fonts[rand(0, count($fonts) - 1)]; $x = intval(CODE_WIDTH / CODE_CHARS_COUNT * $i) + CODE_FONT_SIZE / 2; $code .= $char; imagettftext($img, CODE_FONT_SIZE, $angle, $x, $y, $color, $font, $char); } // ----------------------------------- // Generate the image // ----------------------------------- $img_name = $now . '.jpg'; ImageJPEG($img, $img_path . $img_name); $img_width = CODE_WIDTH; $img_height = CODE_HEIGHT; $img = "<img src=\"{$img_url}{$img_name}\" width=\"{$img_width}\" height=\"{$img_height}\" style=\"border:1px solid #021a40;\" alt=\" \" />"; //ImageDestroy($img); return array('word' => $code, 'time' => $now, 'image' => $img); }
function main() { $is_error = false; //no errors yet . . . $continue = false; //continue on to next image in directory? if (isset($_GET['directory'])) { $directory = $_GET['directory']; if (substr($directory, -1) != '/') { $directory = $directory . '/'; } //add trailing slash show_directory($directory); } // set directory of images to work in if (isset($_GET['directory']) && isset($_GET['action'])) { $action = $_GET['action']; } else { // if no directory set, then display form to set the directory return; } echo "performing {$action} on directory {$directory}<br />"; $threshold = $_GET['threshold']; // set number of file in directory to start on if (isset($_GET['number'])) { $number = $_GET['number']; } else { $number = 0; } // start at the first file //compile list of files in directory $file_list = collect_files($directory); if (count($file_list) == 0) { return "Directory {$directory} is empty."; } //couldn't find anything $output_dir = 'images/output/'; $output = ''; $image_name = ''; switch ($action) { case 'to_data': if (!isset($file_list[$number])) { $output .= 'Finished!'; //all done! break; } //if $image = file_to_image($directory . $file_list[$number]); $data = image_to_data($image); break; case 'difference_series': if (!isset($file_list[$number]) || !isset($file_list[$number + 1])) { $output .= 'No more files to compare.'; //all done! break; } //if $a = file_to_image($directory . $file_list[$number]); $b = file_to_image($directory . $file_list[$number + 1]); if (!($image = image_combine($a, $b, 'difference'))) { $output .= 'ERROR trying to combine images ' . $file_list[$number] . ' and ' . $file_list[$number + 1]; $is_error = true; break; } //if $file_name = '(' . $file_list[$number] . ')-(' . $file_list[$number + 1] . ')'; $image_name = $file_name . '.png'; write_image($image, $output_dir, $file_name, 'png'); $continue = true; break; case 'image_to_hough': if (!isset($file_list[$number])) { $output .= 'No more files.'; //done! break; } //if if (!($image = file_to_image($directory . $file_list[$number]))) { return "could not create image from {$directory}{$file_list[$number]}"; } if (isset($_GET['threshold'])) { $threshold = $_GET['threshold']; } else { $threshold = 128; } echo '<img src="' . $directory . $file_list[$number] . '" />'; $image = image_to_hough($image, $threshold); $file_name = "hough({$file_list[$number]})"; $image_name = $file_name . '.png'; write_image($image, $output_dir, $file_name, 'png'); echo hough_to_image($output_dir . $file_name . '.png'); $continue = true; break; case 'average': if (!isset($file_list[$number])) { $output .= 'No more files to add.'; //all done! break; } //if $temp_path = 'images/temp/'; $temp_filename = 'temp'; //$image = imagecreatefromjpeg($directory . $file_list[$number]); if (!file_exists($directory . $file_list[$number])) { return "file {$directory}{$file_list[$number]} does not seem to exist.<br />"; } if (!($image = file_to_image($directory . $file_list[$number]))) { return "{$directory}{$file_list[$number]} is not an image file<br />"; } if (file_exists($temp_path . $temp_filename . '.png')) { echo "temp file found in {$temp_path}{$temp_filename}.png<br />"; if ($number == 0) { //there shouldn't be a temp file echo "temp file is from previous batch: deleting . . . <br />"; if (!unlink($temp_path . $temp_filename . '.png')) { echo "error: could not delete file {$temp_path}{$temp_filename}"; } } $temp = file_to_image($temp_path . $temp_filename . '.png'); } else { echo "{$temp_path}{$temp_filename}.png does not exist, creating . . . <br />"; if (!($temp = imagecreatetruecolor(imagesx($image), imagesy($image)))) { return 'could not create temporary image'; } } $num_in_series = count($file_list); $output .= "adding the {$number}th 1/{$num_in_series}th of image to temp.png"; $constant = 1.0 / $num_in_series; $image = image_apply_constant($image, $constant, 'multiply'); $image = image_combine($temp, $image, 'sum'); write_image($image, $temp_path, $temp_filename, 'png'); $continue = true; break; case 'sum_series': break; case 'threshold': if (!isset($file_list[$number])) { $output .= 'No more files.'; //all done! break; } //if if (isset($_GET['threshold'])) { $threshold = $_GET['threshold']; } else { $threshold = 34; } $image = file_to_image($directory . $file_list[$number]); $image = image_apply_constant($image, $threshold, 'threshold'); $image_name = 'thresh(' . $file_list[$number] . ').png'; write_image($image, $output_dir, "thresh({$file_list[$number]})", 'png'); $continue = true; break; } //switch $next = $number + 1; if ($is_error) { $output .= <<<OUTPUT <br>Ohhh crap, there was an error performing the action {$action} on directory {$directory}<br><br> Try next pair? <a href="?directory={$directory}&action={$action}&number={$next}">Sure.</a> OUTPUT; $continue = false; } else { $output .= <<<OUTPUT Success.<br> <img src="{$output_dir}{$image_name}" /> OUTPUT; } //if if ($continue) { $output .= <<<OUTPUT <a href="?number={$next}&action={$action}&directory={$directory}&threshold={$threshold}">continue</a><br />\t OUTPUT; } //if return $output; }