3v4l.org

run code in 300+ PHP versions simultaneously
<?php // (c) 2009 P_r_i_m_a_t class Yandex_CAPTCHA { protected $image; protected $width; protected $height; protected $bg_color; protected $mask; protected $digits_quantity; protected $digits; protected $patterns; protected $ann; function __construct($filename, $ann_file, $digits_quantity) { $this->image = imagecreatefromgif($filename); $size = getimagesize($filename); $this->width = $size[0]; $this->height = $size[1]; $this->bg_color = imagecolorat($this->image, 0, 0); $this->update_mask(); $this->digits_quantity = $digits_quantity; $this->ann = fann_create($ann_file); } protected function update_mask() { $this->mask = array(); for ($i = 0; $i < $this->width; $i++) { for ($j = 0; $j < $this->height; $j++) { $this->mask[$i][$j] = $this->colordist(imagecolorat($this->image, $i, $j), $this->bg_color) > 30 ? 1 : 0; } } } protected function colordist($color1, $color2) { return sqrt(pow((($color1 >> 16) & 0xFF) - (($color2 >> 16) & 0xFF), 2) + pow((($color1 >> 8) & 0xFF) - (($color2 >> 8) & 0xFF), 2) + pow(($color1 & 0xFF) - ($color2 & 0xFF), 2)); } protected function get_brightness($color) { return ((($color >> 16) & 0xFF) + (($color >> 8) & 0xFF) + ($color & 0xFF)) / 765; } protected function remove_logo() { imagefilledrectangle($this->image, $this->width - 37, 0, $this->width - 1, 14, $this->bg_color); } protected function crop($min_left, $min_right, $min_top, $min_bottom) { $top_border = 0; while ($top_border < $this->height - 1) { $count = 0; for ($i = 0; $i < $this->width; $i++) { if ($this->colordist(imagecolorat($this->image, $i, $top_border), $this->bg_color) > 10) $count++; } if ($count > $min_top) break; $top_border++; } $bottom_border = $this->height - 1; while ($bottom_border > 0) { $count = 0; for ($i = 0; $i < $this->width; $i++) { if ($this->colordist(imagecolorat($this->image, $i, $bottom_border), $this->bg_color) > 10) $count++; } if ($count > $min_bottom) break; $bottom_border--; } $left_border = 0; while ($left_border < $this->width - 1) { $count = 0; for ($j = 0; $j < $this->height; $j++) { if ($this->colordist(imagecolorat($this->image, $left_border, $j), $this->bg_color) > 10) $count++; } if ($count > $min_left) break; $left_border++; } $right_border = $this->width - 1; while ($right_border > 0) { $count = 0; for ($j = 0; $j < $this->height; $j++) { if ($this->colordist(imagecolorat($this->image, $right_border, $j), $this->bg_color) > 10) $count++; } if ($count > $min_right) break; $right_border--; } $this->width = $right_border - $left_border + 1; $this->height = $bottom_border - $top_border + 1; $cropped_image = imagecreatetruecolor($this->width, $this->height); imagecopy($cropped_image, $this->image, 0, 0, $left_border, $top_border, $this->width, $this->height); imagedestroy($this->image); $this->image = $cropped_image; $this->bg_color = imagecolorat($this->image, 0, 0); $this->update_mask(); } public function test_dna($array) { $fitness = 0; $points = 0; for ($i = 0; $i < $this->width; $i++) { $y = $array['s'] + floor($array['h'] * sin(($i + $array['o']) / max(1, $array['w'])) + $array['a'] * ($i / $this->width)); if ($y < 0 || $y >= $this->height) return 0; if ($this->mask[$i][$y]) $points += 5; elseif ($y - 1 >= 0 && $this->mask[$i][$y - 1]) $points += 2; elseif ($y + 1 < $this->height && $this->mask[$i][$y + 1]) $points += 2; else $points = 0; if ($i % 3 == 0) { $fitness += $points; $points = 0; } } return $fitness; } protected function remove_line($array) { for ($i = 0; $i < $this->width; $i++) { $y = $array['s'] + floor($array['h'] * sin(($i + $array['o']) / max(1, $array['w'])) + $array['a'] * ($i / $this->width)); if ($y < 0 || $y >= $this->height) continue; imagesetpixel($this->image, $i, $y - 1, $this->bg_color); imagesetpixel($this->image, $i, $y, $this->bg_color); imagesetpixel($this->image, $i, $y + 1, $this->bg_color); } } protected function divide_digits() { $this->digits = array(); $digit_width = ceil($this->width / $this->digits_quantity); for ($i = 0; $i < $this->digits_quantity; $i++) { $offset = floor($i * ($this->width / $this->digits_quantity)); $offset = min($offset, $this->width - $digit_width); $top_border = 0; while ($top_border < $this->height - 1) { $count = 0; for ($j = $offset; $j < $offset + $digit_width; $j++) { if ($this->colordist(imagecolorat($this->image, $j, $top_border), $this->bg_color) > 30) $count++; } if ($count > 3) break; $top_border++; } $bottom_border = $this->height - 1; while ($bottom_border > 0) { $count = 0; for ($j = $offset; $j < $offset + $digit_width; $j++) { if ($this->colordist(imagecolorat($this->image, $j, $bottom_border), $bg_color) > 30) $count++; } if ($count > 3) break; $bottom_border--; } $this->digits[$i]['image'] = imagecreatetruecolor($digit_width, $bottom_border - $top_border + 1); $this->digits[$i]['width'] = $digit_width; $this->digits[$i]['height'] = $bottom_border - $top_border + 1; imagecopy($this->digits[$i]['image'], $this->image, 0, 0, $offset, $top_border, $this->digits[$i]['width'], $this->digits[$i]['height']); } } protected function recognize_digit($digit) { $temp_width = 20; $temp_height = 30; $temp_image = imagecreatetruecolor($temp_width, $temp_height); imagecopyresampled($temp_image, $this->digits[$digit]['image'], 0, 0, 0, 0, $temp_width, $temp_height, $this->digits[$digit]['width'], $this->digits[$digit]['height']); $sample = array(); for ($i = 0; $i < $temp_width; $i++) { for ($j = 0; $j < $temp_height; $j++) { $sample []= $this->get_brightness(imagecolorat($temp_image, $i, $j)); } } imagedestroy($temp_image); global $answer; global $set; $out = array_fill(0, 10, 0); $out[((int) substr($answer, $digit, 1))] = 1; $set []= array($sample, $out); $output = fann_run($this->ann, $sample); $max = 0; $best_match = 0; foreach ($output as $key => $val) { if ($val > $max) { $best_match = $key; $max = $val; } } if ($max == 0) $best_match = rand(0, 9); return $best_match; } public function parse() { $this->remove_logo(); $this->crop(2, 2, 2, 2); $ga = new GA(100, $this, 0, $this->height - 1, $this->width); $result = $ga->run(100, floor($this->width * 5 * 0.9)); $this->remove_line($result['best_dna']); $this->update_mask(); $ga = new GA(100, $this, 0, $this->height - 1, $this->width); $result = $ga->run(100, floor($this->width * 5 * 0.9)); $this->remove_line($result['best_dna']); $this->crop(5, 8, 5, 5); $this->divide_digits(); $result = ''; for ($i = 0; $i < $this->digits_quantity; $i++) { $result .= $this->recognize_digit($i); } return $result; } public function show($digit = false) { header('Content-type: image/gif'); if ($digit === false) imagegif($this->image); else imagegif($this->digits[$digit]['image']); exit(); } } ?>

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0120.00318.18
8.3.50.0120.00921.14
8.3.40.0120.00318.62
8.3.30.0100.01020.46
8.3.20.0090.00018.83
8.3.10.0040.00420.92
8.3.00.0040.00422.38
8.2.180.0140.00316.63
8.2.170.0120.00322.96
8.2.160.0100.00320.65
8.2.150.0050.00324.18
8.2.140.0060.00324.66
8.2.130.0050.00326.16
8.2.120.0080.00021.00
8.2.110.0030.00719.54
8.2.100.0120.00017.91
8.2.90.0000.00819.08
8.2.80.0080.00017.97
8.2.70.0030.00617.63
8.2.60.0000.00817.68
8.2.50.0000.00818.07
8.2.40.0050.00318.22
8.2.30.0030.00619.66
8.2.20.0050.00317.61
8.2.10.0060.00317.73
8.2.00.0040.00717.70
8.1.280.0070.00725.92
8.1.270.0040.00423.99
8.1.260.0050.00326.35
8.1.250.0040.00428.09
8.1.240.0030.00623.79
8.1.230.0060.00621.09
8.1.220.0000.00918.77
8.1.210.0030.00519.23
8.1.200.0030.00617.25
8.1.190.0060.00317.54
8.1.180.0060.00318.10
8.1.170.0080.00018.63
8.1.160.0040.00418.96
8.1.150.0090.00018.57
8.1.140.0040.00417.51
8.1.130.0040.00417.84
8.1.120.0000.00717.39
8.1.110.0000.00817.46
8.1.100.0070.00017.34
8.1.90.0000.00717.49
8.1.80.0040.00417.37
8.1.70.0040.00417.42
8.1.60.0050.00517.56
8.1.50.0050.00317.51
8.1.40.0030.00617.51
8.1.30.0060.00317.58
8.1.20.0000.00917.64
8.1.10.0080.00017.59
8.1.00.0030.00617.44
8.0.300.0040.00419.85
8.0.290.0030.00617.03
8.0.280.0020.00518.39
8.0.270.0040.00417.20
8.0.260.0070.00016.85
8.0.250.0000.00717.01
8.0.240.0090.00016.85
8.0.230.0040.00417.02
8.0.220.0070.00016.92
8.0.210.0080.00016.92
8.0.200.0040.00417.01
8.0.190.0000.00716.96
8.0.180.0000.00716.95
8.0.170.0080.00016.82
8.0.160.0070.00016.89
8.0.150.0070.00016.86
8.0.140.0070.00016.75
8.0.130.0030.00313.33
8.0.120.0000.00816.93
8.0.110.0080.00016.96
8.0.100.0040.00416.73
8.0.90.0030.00516.79
8.0.80.0120.00316.97
8.0.70.0030.00516.75
8.0.60.0040.00416.74
8.0.50.0060.00316.76
8.0.30.0120.01117.04
8.0.20.0120.01017.40
8.0.10.0080.00016.90
8.0.00.0100.00916.75
7.4.330.0030.00316.67
7.4.320.0040.00416.61
7.4.300.0070.00016.59
7.4.290.0070.00016.60
7.4.280.0030.00316.62
7.4.270.0070.00016.61
7.4.260.0000.00613.27
7.4.250.0000.00816.37
7.4.240.0040.00416.57
7.4.230.0070.00016.61
7.4.220.0150.01216.57
7.4.210.0100.00716.56
7.4.200.0000.00716.68
7.4.190.0040.00416.48
7.4.160.0150.00916.72
7.4.150.0090.01017.40
7.4.140.0140.00517.86
7.4.130.0100.00816.53
7.4.120.0100.00716.71
7.4.110.0140.00416.58
7.4.100.0160.00316.43
7.4.90.0140.00416.60
7.4.80.0110.00619.39
7.4.70.0100.01016.41
7.4.60.0160.00316.68
7.4.50.0060.00316.17
7.4.40.0060.00922.77
7.4.30.0130.00616.40
7.4.00.0070.01115.02
7.3.330.0000.00613.32
7.3.320.0060.00013.18
7.3.310.0000.00716.18
7.3.300.0030.00316.32
7.3.290.0000.01416.30
7.3.280.0100.01016.33
7.3.270.0180.00017.40
7.3.260.0090.01018.24
7.3.250.0110.00916.30
7.3.240.0070.01016.28
7.3.230.0070.01016.61
7.3.210.0070.01116.54
7.3.200.0120.00619.39
7.3.190.0150.00916.62
7.3.180.0060.00916.39
7.3.170.0090.00916.65
7.3.160.0110.01116.49
7.3.120.0110.00714.98
7.3.110.0090.00914.75
7.3.100.0030.00914.97
7.3.90.0040.01414.82
7.3.80.0110.00614.77
7.3.70.0000.01314.78
7.3.60.0110.00414.74
7.3.50.0030.01014.60
7.3.40.0090.00614.70
7.3.30.0100.00314.85
7.3.20.0040.00416.63
7.3.10.0030.00916.50
7.3.00.0000.01516.32
7.2.330.0140.00716.72
7.2.320.0050.01416.66
7.2.310.0070.01116.79
7.2.300.0120.00616.61
7.2.290.0060.01116.38
7.2.240.0070.01014.90
7.2.230.0040.01414.79
7.2.220.0030.01314.94
7.2.210.0000.01715.09
7.2.200.0070.01015.04
7.2.190.0040.01415.11
7.2.180.0100.00714.76
7.2.170.0060.00914.84
7.2.160.0090.00614.69
7.2.150.0050.00516.69
7.2.140.0030.01016.89
7.2.130.0100.00616.53
7.2.120.0090.00016.76
7.2.110.0060.00616.84
7.2.100.0060.00916.71
7.2.90.0000.01116.65
7.2.80.0040.01116.51
7.2.70.0040.00816.57
7.2.60.0050.00816.73
7.2.50.0030.00916.83
7.2.40.0080.00416.95
7.2.30.0000.01416.81
7.2.20.0000.01416.76
7.2.10.0130.00316.82
7.2.00.0080.00417.86
7.1.330.0040.01115.61
7.1.320.0110.00715.79
7.1.310.0000.01015.66
7.1.300.0040.01215.48
7.1.290.0060.00615.59
7.1.280.0090.00615.51
7.1.270.0060.01015.36
7.1.260.0050.00515.67
7.1.250.0030.00915.66
7.1.200.0030.00715.59
7.1.100.0030.00917.84
7.1.70.0030.01016.92
7.1.60.0000.01119.17
7.1.50.0100.01616.61
7.1.00.0070.06722.51
7.0.200.0060.00316.47
7.0.140.0000.07721.96
7.0.100.0000.07720.06
7.0.90.0000.04720.11
7.0.80.0100.07719.96
7.0.70.0030.08020.08
7.0.60.0030.08320.05
7.0.50.0100.07320.50
7.0.40.0100.08719.95
7.0.30.0170.07320.09
7.0.20.0100.08320.14
7.0.10.0000.05720.16
7.0.00.0100.07720.09
5.6.280.0100.07021.16
5.6.250.0070.08720.71
5.6.240.0130.05320.65
5.6.230.0070.05020.64
5.6.220.0170.08020.71
5.6.210.0030.08320.63
5.6.200.0100.03721.13
5.6.190.0030.07721.09
5.6.180.0100.07321.07
5.6.170.0070.06021.15
5.6.160.0100.08321.18
5.6.150.0200.06721.02
5.6.140.0000.09021.00
5.6.130.0030.07721.17
5.6.120.0030.04321.14
5.6.110.0000.04721.07
5.6.100.0000.04721.01
5.6.90.0030.04721.11
5.6.80.0000.04320.39
5.6.70.0130.03020.47
5.6.60.0000.04320.50
5.6.50.0000.04020.50
5.6.40.0030.07720.49
5.6.30.0030.05320.36
5.6.20.0030.04720.49
5.6.10.0070.07720.57
5.6.00.0070.04720.41
5.5.380.0200.06720.39
5.5.370.0130.04720.45
5.5.360.0200.06320.47
5.5.350.0070.04020.45
5.5.340.0030.06020.93
5.5.330.0030.04320.87
5.5.320.0030.08320.86
5.5.310.0000.05320.87
5.5.300.0070.08320.87
5.5.290.0100.07720.89
5.5.280.0000.04720.99
5.5.270.0030.04320.92
5.5.260.0000.04720.69
5.5.250.0070.04020.78
5.5.240.0030.04320.35
5.5.230.0100.03320.08
5.5.220.0030.04020.33
5.5.210.0030.05320.19
5.5.200.0000.04320.35
5.5.190.0130.03020.30
5.5.180.0100.07720.24
5.5.160.0000.04320.24
5.5.150.0000.04720.22
5.5.140.0070.03720.31
5.5.130.0030.04020.22
5.5.120.0030.04320.30
5.5.110.0030.03320.29
5.5.100.0070.03720.22
5.5.90.0030.03320.23
5.5.80.0070.03720.13
5.5.70.0000.04320.13
5.5.60.0070.03020.12
5.5.50.0070.03720.02
5.5.40.0030.03320.13
5.5.30.0070.03720.11
5.5.20.0070.03020.11
5.5.10.0000.04020.19
5.5.00.0130.03720.07
5.4.450.0030.05319.26
5.4.440.0000.04719.29
5.4.430.0070.03719.51
5.4.420.0100.04019.36
5.4.410.0000.04019.09
5.4.400.0030.03719.14
5.4.390.0000.04019.23
5.4.380.0030.03719.05
5.4.370.0030.03718.87
5.4.360.0030.04019.13
5.4.350.0030.05318.86
5.4.340.0030.05319.20
5.4.320.0030.03718.88
5.4.310.0070.04019.16
5.4.300.0030.03719.06
5.4.290.0000.04318.91
5.4.280.0070.03719.05
5.4.270.0070.03318.89
5.4.260.0000.04318.96
5.4.250.0100.03018.91
5.4.240.0000.05019.02
5.4.230.0030.03719.22
5.4.220.0070.02718.95
5.4.210.0000.04018.91
5.4.200.0070.03719.22
5.4.190.0200.02318.95
5.4.180.0100.03019.04
5.4.170.0030.03719.03
5.4.160.0000.04019.14
5.4.150.0070.03718.84
5.4.140.0030.03716.31
5.4.130.0030.03316.43
5.4.120.0000.04016.31
5.4.110.0030.03716.56
5.4.100.0030.03316.42
5.4.90.0030.05016.39
5.4.80.0100.03016.52
5.4.70.0070.03316.32
5.4.60.0030.07016.32
5.4.50.0070.07016.51
5.4.40.0100.06716.39
5.4.30.0070.03016.39
5.4.20.0070.03016.47
5.4.10.0100.07316.29
5.4.00.0070.03315.85
5.3.290.0130.02714.76
5.3.280.0100.03314.80
5.3.270.0030.04014.78
5.3.260.0070.03314.81
5.3.250.0100.03014.83
5.3.240.0030.03714.79
5.3.230.0070.03014.82
5.3.220.0030.03014.82
5.3.210.0070.03314.82
5.3.200.0000.04714.66
5.3.190.0000.03314.82
5.3.180.0000.04014.63
5.3.170.0100.03014.67
5.3.160.0030.03314.67
5.3.150.0100.05314.66
5.3.140.0130.07014.65
5.3.130.0030.07714.79
5.3.120.0000.08014.65
5.3.110.0100.07314.72
5.3.100.0070.07714.13
5.3.90.0100.05714.19
5.3.80.0070.03314.16
5.3.70.0000.07714.22
5.3.60.0100.07714.13
5.3.50.0070.07314.13
5.3.40.0070.06014.04
5.3.30.0070.04014.09
5.3.20.0070.07313.92
5.3.10.0070.07313.80
5.3.00.0030.05713.74

preferences:
43.06 ms | 401 KiB | 5 Q