public function testImBorder() { $tests = array("100x200 -> 1 = 102x202", "100x200 -> 2 = 104x204"); foreach ($tests as $test) { preg_match("~(?P<sw>\\d+)x(?P<sh>\\d+) -> (?P<border>\\S+) = (?P<dw>\\d+)x(?P<dh>\\d+)~", $test, $m); $im = imagecreate($m['sw'], $m['sh']); $backgroundColor = imagecolorallocate($im, 0, 0, 0); $tmp = rtrim(`mktemp`); imagepng($im, $tmp); $target = imBorder($tmp, ['border' => $m['border'], 'dir' => sys_get_temp_dir(), 'log' => true, 'overwrite' => true]); $this->assertTrue(is_file($target)); list($w, $h) = getimagesize($target); $this->assertEquals($w, $m['dw']); $this->assertEquals($h, $m['dh']); } }
function imCaptcha($settings = array()) { $width = isset($settings['width']) ? $settings['width'] : null; $height = isset($settings['height']) ? $settings['height'] : 50; $length = isset($settings['length']) ? $settings['length'] : 6; $padding = isset($settings['padding']) ? $settings['padding'] : 4; $padding = intval($padding) > 0 ? intval($padding) : 4; $length = intval($length) > 0 ? intval($length) : 6; $height = intval($height) > 0 ? intval($height) : 50; $paddingLR = $padding; $paddingTB = $padding; $size = $height - 2 * $padding; $fonts = glob(__DIR__ . "/fonts/*.ttf"); if (!$fonts) { return array(); } $font = $fonts[mt_rand(0, count($fonts) - 1)]; $box = imagettfbbox($size, 0, $font, "H"); $_width = abs($box[2] - $box[0]); $_shift = abs($box[6] - $box[0]); $_height = abs($box[7] - $box[1]); // if (!$width) { $width = $length * $_width + ($length + 1) * $padding + $_shift; } // $image = imagecreatetruecolor($width, $height); if (!$image) { return array(); } $letters = 'ABCDEFGHJKMNPRSTUVWXYZ'; // no "I", "L", "O", "Q" $backgroundColor = imagecolorallocate($image, 255, 255, 255); $lineColor = imagecolorallocate($image, 64, 64, 64); $pixelColor = imagecolorallocate($image, 0, 0, 255); $textColor = imagecolorallocate($image, 0, 0, 0); imagefilledrectangle($image, 0, 0, $width, $height, $backgroundColor); // 3 lines imageline($image, 0, mt_rand() % $height, $width, mt_rand() % $height, $lineColor); imageline($image, 0, mt_rand() % $height, $width, mt_rand() % $height, $lineColor); imageline($image, 0, mt_rand() % $height, $width, mt_rand() % $height, $lineColor); // add noise for ($i = 0; $i < 500; $i++) { imagesetpixel($image, mt_rand() % $width, mt_rand() % $height, $pixelColor); } $len = strlen($letters); $word = ""; for ($i = 0; $i < $length; $i++) { $letter = $letters[mt_rand(0, $len - 1)]; $angle = -5 + mt_rand(0, 10); imagettftext($image, $size, $angle, $i * ($padding + $_width) + $padding, $padding + $_height, $textColor, $font, $letter); $word .= $letter; } $file = rtrim(`mktemp --suffix=.png`); imagepng($image, $file); imagedestroy($image); $imBorder = array(); if (isset($settings['borderColor'])) { $imBorder['borderColor'] = $settings['borderColor']; } if (isset($settings['border'])) { $imBorder['border'] = $settings['border']; } $file = imBorder($file, $imBorder); if (!$file) { return array(); } return array('file' => $file, 'word' => $word); }