public static function generate($id, $width = 100, $height = 40) { $code = session::get("capcha_{$id}"); // If not set then font size will be 75% size of height or width if (!self::$_font_size) { if ($width > $height) { self::$_font_size = $height * 0.75; } else { self::$_font_size = $width * 0.75; } } // Create image $image = imagecreate($width, $height) or die('Cannot initialize new GD image stream'); // set the colors $background_color = imagecolorallocate($image, self::$_background_color[0], self::$_background_color[1], self::$_background_color[2]); $text_color = imagecolorallocate($image, self::$_font_color[0], self::$_font_color[1], self::$_font_color[2]); $noise_color = imagecolorallocate($image, self::$_noise_color[0], self::$_noise_color[1], self::$_noise_color[2]); // Generate random dots in background for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } // Generate random lines in background for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } // create textbox and add text $textbox = imagettfbbox(self::$_font_size, 0, self::$_font_file, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, self::$_font_size, 0, $x, $y, $text_color, self::$_font_file, $code) or die('Error in imagettftext function'); // Output captcha image to browser header('Content-Type:image/jpeg'); imagejpeg($image); imagedestroy($image); }
function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 10, $sses_name = "verify") { // 创建画布 $width = 80; $height = 28; $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); $balck = imagecolorallocate($image, 0, 0, 0); // 用填充矩形填充画布 imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); $chars = buildRandomString($type, $length); $_SESSION[$sses_name] = $chars; for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); $angle = mt_rand(-15, 15); $x = 3 + $i * $size; $y = mt_rand(18, 22); $fontfile = "../fonts/" . "STFANGSO.TTF"; $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); $text = substr($chars, $i, 1); imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } // 加点 for ($i = 0; $i < $pixel; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $balck); } // 加直线 for ($i = 0; $i < $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } header("content-type:image/jpg"); imagegif($image); imagedestroy($image); }
function generateImage($certiImage, $posXString, $posYString, $posX2String, $posY2String, $valueString) { $certiPath = dirname(__FILE__); $certiImage = imagecreatefromjpeg($certiPath . '/certi_images/' . $certiImage); $color = imagecolorallocate($certiImage, 0, 0, 0); //black // $whiteBackground = imagecolorallocate($background, 255, 255, 255); // $imagefill($certiImage,0,0,$whiteBackground); $rotatedImage = imagerotate($certiImage, 90, $color); //rotate certificate $font = $certiPath . '/fonts/odstemplik.otf'; $posXArray = explode("::", $posXString); $posYArray = explode("::", $posYString); $posX2Array = explode("::", $posX2String); $posY2Array = explode("::", $posY2String); $valuesArray = explode("::", $valueString); // error_log(print_r($valuesArray)); for ($i = 0; $i < sizeof($valuesArray); $i++) { $lineWidth = $posYArray[$i] - $posY2Array[$i]; $font_size = 90; do { $p = imagettfbbox($font_size, 0, $font, $valuesArray[$i]); $textWidth = $p[2] - $p[0]; $font_size--; // error_log($textWidth); } while ($textWidth >= $lineWidth); $y = ($lineWidth - $textWidth) / 2; imagettftext($rotatedImage, $font_size, 90, $posXArray[$i], $posYArray[$i] - $y, $color, $font, $valuesArray[$i]); } ob_start(); imagejpeg($rotatedImage); $actual_image = base64_encode(ob_get_contents()); ob_end_clean(); return "data:image/png;base64," . $actual_image; }
public function show_captcha() { if (session_id() == "") { session_name("CAKEPHP"); session_start(); } $vendor_path = App::path('Vendor'); $path = $vendor_path[0] . 'captcha'; $imgname = 'bg.jpg'; $imgpath = $path . DS . 'images' . DS . $imgname; $captchatext = md5(time()); $captchatext = substr($captchatext, 0, 5); $_SESSION['captcha'] = $captchatext; if (file_exists($imgpath)) { $im = imagecreatefromjpeg($imgpath); $grey = imagecolorallocate($im, rand(1, 128), 128, 128); $font = $path . DS . 'fonts' . DS . '3D_Noise.ttf'; imagettftext($im, 36, 0, 0, 55, $grey, $font, $captchatext); header('Content-Type: image/jpeg'); header("Cache-control: private, no-cache"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Pragma: no-cache"); imagejpeg($im); imagedestroy($im); ob_flush(); flush(); } else { echo 'captcha error'; exit; } }
public function calcFontSizeAddText($text, $parts, $whole, $angle = 0) { $imgWidth = imagesx($this->image); $imgHeight = imagesy($this->image); if (strlen($text) > 0) { $fontsize = $imgWidth / strlen($text); $textBox = imagettfbbox($fontsize, 0, $this->font, $text); /* [0] lower left X coordinate [1] lower left Y coordinate [2] lower right X coordinate [3] lower right Y coordinate [4] upper right X coordinate [5] upper right Y coordinate [6] upper left X coordinate [7] upper left Y coordinate */ $textBoxLowerLeftX = $textBox[0]; $textBoxLowerRightX = $textBox[2]; $textBoxLowerLeftY = $textBox[1]; $textBoxUpperLeftY = $textBox[7]; $textHeight = $textBoxUpperLeftY - $textBoxLowerLeftY; $x = ceil(($imgWidth - $textBoxLowerRightX - $textBoxLowerLeftX) / 2); $y = ($imgHeight / $whole - $textHeight / $whole) * $parts; imagettftext($this->image, $fontsize, $angle, $x, $y, $this->color, $this->font, $text); } }
function smart_spam($code) { @putenv('GDFONTPATH=' . realpath('.')); $font = 'FONT.TTF'; // antispam image height $height = 40; // antispam image width $width = 110; $font_size = $height * 0.6; $image = @imagecreate($width, $height); $background_color = @imagecolorallocate($image, 255, 255, 255); $noise_color = @imagecolorallocate($image, 20, 40, 100); /* add image noise */ for ($i = 0; $i < $width * $height / 4; $i++) { @imageellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } /* render text */ $text_color = @imagecolorallocate($image, 20, 40, 100); @imagettftext($image, $font_size, 0, 7, 29, $text_color, $font, $code) or die('Cannot render TTF text.'); //output image to the browser *//* header('Content-Type: image/png'); @imagepng($image) or die('imagepng error!'); @imagedestroy($image); exit; }
function createCode($len = 4) { $width = 100; $height = 50; $size = 22; //字体大小 $font = ROOT_PATH . '/static/font/arial.ttf'; //字体 $img = imagecreatetruecolor($width, $height); //创建画布 $bgimg = imagecreatefromjpeg(ROOT_PATH . '/static/background/' . rand(1, 5) . '.jpg'); //生成背景图片 $bg_x = rand(0, 100); //随机招贴画布起始X轴坐标 $bg_y = rand(0, 50); //随机招贴画布起始Y轴坐标 imagecopy($img, $bgimg, 0, 0, $bg_x, $bg_y, $bg_x + $width, $bg_y + $height); //把背景图片$bging粘贴的画布上 $str = $this->creaStr($len); //字符串 for ($i = 0, $j = 5; $i < 4; $i++) { $array = array(-1, 1); $p = array_rand($array); $an = $array[$p] * mt_rand(1, 10); //扭曲角度 imagettftext($img, $size, $an, $j + 5, 34, imagecolorallocate($img, rand(0, 100), rand(0, 100), rand(0, 100)), $font, $str[$i]); //生成验证字符窜 $j += 20; } cookie('captchacode', strtolower($str)); header('Content-type:image/png'); imagepng($img); imagedestroy($img); }
/** * Create image from text (for captcha) * @param int widht * @param int height * @param string text * @param string Font (Arial) * @param int fontsize * @param string if not empty then save to file * @param int randomline */ static function createImageTextKod($width, $height, $text, $TTFFile, $fontsize = 16, $file = '', $randomline = 2) { $img = imagecreate($width, $height); $col = imagecolorallocate($img, 255, 255, 255); $line = imagecolorallocate($img, 220, 220, 220); for ($i = 5; $i < $width; $i = $i + $fontsize) { imageline($img, $i, 0, $i, $height, $line); } for ($i = 5; $i < $height; $i = $i + $fontsize) { imageline($img, 0, $i, $width, $i, $line); } $c[0] = imagecolorallocate($img, 20, 50, 235); $c[1] = imagecolorallocate($img, 20, 220, 23); $c[2] = imagecolorallocate($img, 220, 20, 23); $c[3] = imagecolorallocate($img, 255, 120, 10); $c[4] = imagecolorallocate($img, 120, 120, 140); $c[5] = imagecolorallocate($img, 20, 130, 140); $c[6] = imagecolorallocate($img, 120, 130, 40); srand(time()); for ($i = 0; $i < $randomline; $i++) { $color = $c[rand(0, 6)]; $y1 = rand(0, $height); $y2 = rand(0, $height); imageline($img, 0, $y1, $width, $y2, $color); } for ($i = 0; $i < strlen($text); $i++) { $symb = substr($text, $i, 1); $color = $c[rand(0, 6)]; $angle = rand(-15, 15); $y = $fontsize + intval($fontsize / 2) + intval($fontsize / 5) + rand(-intval($fontsize / 2), intval($fontsize / 2)) + 1; $x = $fontsize * $i + intval($fontsize / 2) + 1; imagettftext($img, $fontsize, $angle, $x, $y, $color, $TTFFile, $symb); } imagejpeg($img, $file, 90); }
function create_image() { // hash: là mật mã $md5_hash = md5(rand(0, 999)); // rand phat sinh 1 số từ 0 - 999 // md5 sẽ mã hóa 1 số thành 1 số khác đến 32 ký tự $security_code = substr($md5_hash, 15, 4); // substr lấy 1 chuỗi con trong md5_hash, lấy từ ký tự thứ 15 và lấy 5 ký tự $_SESSION['security_code'] = $security_code; $width = 100; //Khai báo kích thước captcha $height = 30; $image = imagecreate($width, $height); $while = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); $red = imagecolorallocate($image, 255, 255, 0); imagefill($image, 0, 0, $black); imagestring($image, 5, 30, 6, $security_code, $while); // 5 font size // 30 khoảng cách bên trái //khoảng cách từ trên xuống $captcha = imagecreatefrompng('captcha1.png'); $font = 'arial.ttf'; imagettftext($captcha, 23, 6, 20, 38, $black, $font, $security_code); header("Content-Type:image/jpeg"); // chuyển trang web thành dạng hình jpg imagejpeg($captcha); //Tạo hình imagedestroy($captcha); // Hủy hình gốc vì đã tạo thành trang }
/** * 生成验证码 * @param int $width 验证码图片宽度.默认130 * @param int $height 验证码图片高度.默认40 * @param int $fontSize 验证码字体大小.默认20 * @param int $length 验证码字符个数.默认4 * @return string 验证码中的字符串 */ public static function getCaptcha($width = '130', $height = '40', $fontSize = '20', $length = '4') { $chars = '0123456789abcdefghijklmnopqrstuvwxyz'; $randStr = substr(str_shuffle($chars), 0, $length); $image = imagecreatetruecolor($width, $height); // 定义背景色 $bgColor = imagecolorallocate($image, 0xff, 0xff, 0xff); // 定义文字及边框颜色 $blackColor = imagecolorallocate($image, 0x0, 0x0, 0x0); //生成矩形边框 imagefilledrectangle($image, 0, 0, $width, $height, $bgColor); // 循环生成雪花点 for ($i = 0; $i < 200; $i++) { $grayColor = imagecolorallocate($image, 128 + rand(0, 128), 128 + rand(0, 128), 128 + rand(0, 128)); imagesetpixel($image, rand(1, $width - 2), rand(4, $height - 2), $grayColor); } $font = ROOT_PATH . 'resources/fonts/acidic.ttf'; // 把随机字符串输入图片 $i = -1; while (isset($randStr[++$i])) { $fontColor = imagecolorallocate($image, rand(0, 100), rand(0, 100), rand(0, 100)); if (!function_exists('imagettftext')) { imagechar($image, $fontSize, 15 + $i * 30, rand(5, 20), $randStr[$i], $fontColor); } else { imagettftext($image, $fontSize, 0, 10 + $i * 30, rand(25, 35), $fontColor, $font, $randStr[$i]); } } imagepng($image); $image = $bgColor = $blackColor = $grayColor = $fontColor = null; return $randStr; }
function create_captcha($time) { @session_start(); $image; global $image; $word_1 = ''; $word_2 = ""; for ($i = 0; $i < 4; $i++) { $word_1 .= chr(rand(97, 122)); } for ($i = 0; $i < 4; $i++) { $word_2 .= chr(rand(97, 122)); } $_SESSION['random_number'] = $word_1 . ' ' . $word_2; $dir = './fonts/'; $image = imagecreatetruecolor(165, 50); $font = "recaptchaFont.ttf"; // font style $color = imagecolorallocate($image, 0, 0, 0); // color $white = imagecolorallocate($image, 255, 255, 255); // background color white imagefilledrectangle($image, 0, 0, 709, 99, $white); imagettftext($image, 22, 0, 5, 30, $color, $dir . $font, $_SESSION['random_number']); $_SESSION['count'] = $_SESSION['random_number']; $_SESSION['captcha_string'] = $_SESSION['random_number']; $images = glob("./style/captcha/*.png"); foreach ($images as $image_to_delete) { @unlink($image_to_delete); } imagepng($image, "./style/captcha/" . $time . ".png"); //return array('image'=>$image,'count'=>$_SESSION['count']); }
/** * 添加文字水印 * @$word 水印文字 */ public function text($word) { $textcolor = imagecolorallocate($this->info['image'], 255, 255, 255); $font = 'msyh.ttf'; //imagestring($this->info['image'],2,40,40,$word,$textcolor); imagettftext($this->info['image'], 14, 0, 20, 20, $textcolor, $font, $word); }
public function createCaptcha() { $captcha = ''; $symbol = '0'; $width = 420; $height = 70; $font = 'fonts/bellb.ttf'; $fontsize = 20; $captchaLength = rand(1, 1); $im = imagecreatetruecolor($width, $height); $bg = imagecolorallocatealpha($im, 0, 0, 0, 127); imagefill($im, 0, 0, $bg); for ($i = 0; $i < $captchaLength; $i++) { $captcha .= $symbol[rand(0, strlen($symbol) - 1)]; $x = ($width - 20) / $captchaLength * $i + 10; $x = rand($x, $x + 4); $y = $height - ($height - $fontsize) / 2; $curcolor = imagecolorallocate($im, rand(0, 100), rand(0, 100), rand(0, 100)); $angle = rand(-25, 25); imagettftext($im, $fontsize, $angle, $x, $y, $curcolor, $font, $captcha[$i]); } session_start(); $_SESSION['captcha'] = $captcha; header('Content-type: image/png'); imagepng($im); imagedestroy($im); }
/** * [GetImage 生成验证码] */ public function GetImage() { $this->font_name = __DIR__ . DIRECTORY_SEPARATOR . $this->font_name; $this->type = strtolower($this->type); //转小写 $img = imagecreate($this->width, $this->height) or die("can't found GD Moudle!"); $bg = imagecolorallocate($img, rand(0, 100), rand(0, 100), rand(0, 100)); $font_color = imagecolorallocate($img, 255, 255, 255); $str = $this->GetString(); $strinterval = floor($this->width / $this->char_num); $pos_y = abs(floor($this->height / 2) + floor($this->font_size / 2)); if ($this->chinese) { $str_arr = str_split($str, 3); } else { $str_arr = str_split($str); } $length = mb_strlen($str, "utf-8"); //输出文字 for ($i = 0; $i < $length; $i++) { $font_color_rand = imagecolorallocate($img, rand(155, 255), rand(155, 255), rand(155, 255)); imagettftext($img, $this->font_size, rand(-25, 25), $strinterval * $i + 3, $pos_y, $font_color_rand, $this->font_name, $str_arr[$i]); } $this->CreateLine($img); $this->CreatePixel($img); $this->img = $img; }
function thumbnailThis($ruta, $img, $c_nombr, $c_vig, $c_folio) { // Set the enviroment variable for GD putenv('GDFONTPATH=' . realpath('.')); $cardId = uniqid(); // Genera un ID único para el nombre de archivo while (file_exists($ruta . '/cards/' . $cardId . ".jpg")) { // Si el nombre existe, genera otro $cardId = uniqid(); // unlink( $ruta.$img."_usuario.jpg" ); } // Name the font to be used (note the lack of the .ttf extension) $font = 'varela'; /*Dimensiones*/ $c_width = 1004; $c_height = 1299; $laImagen = $img . ".jpg"; $createdImg = imagecreatefromjpeg($ruta . $laImagen); /*imagejpeg($createdImg,$ruta."hola.jpg",50);*/ $width = imagesx($createdImg); $height = imagesy($createdImg); $previous = imagecreatetruecolor($c_width, $c_height); imagecopyresampled($previous, $createdImg, 0, 0, 0, 0, $c_width, $c_height, $width, $height); /*Color en RGB*/ $text_color = imagecolorallocate($previous, 5, 5, 5); /*imagestring($previous, 5, 5, 5, 'A Simple Text String', $text_color);*/ /*Texto para la imagen Imagen, tamaño, ángulo, posX, posY, color, fuente, texto*/ imagettftext($previous, 20, 0, 130, 767, $text_color, $font, $c_nombr); imagettftext($previous, 20, 0, 750, 767, $text_color, $font, $c_vig); imagettftext($previous, 20, 0, 750, 875, $text_color, $font, $c_folio); imagejpeg($previous, $ruta . '/cards/' . $cardId . ".jpg", 100); return $ruta . '/cards/' . $cardId . ".jpg"; }
function getAuthImage($text) { $this->setpin($text); $im_x = 160; $im_y = 40; $im = imagecreatetruecolor($im_x, $im_y); $text_c = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); $tmpC0 = mt_rand(100, 255); $tmpC1 = mt_rand(100, 255); $tmpC2 = mt_rand(100, 255); $buttum_c = ImageColorAllocate($im, $tmpC0, $tmpC1, $tmpC2); imagefill($im, 16, 13, $buttum_c); $font = PATH_SYS_PUBLIC . 'font-awesome/fonts/verdana.ttf'; for ($i = 0; $i < strlen($text); $i++) { $tmp = substr($text, $i, 1); $array = array(-1, 1); $p = array_rand($array); $an = $array[$p] * mt_rand(1, 10); //角度 $size = 28; imagettftext($im, $size, $an, 15 + $i * $size, 35, $text_c, $font, $tmp); } $distortion_im = imagecreatetruecolor($im_x, $im_y); imagefill($distortion_im, 16, 13, $buttum_c); for ($i = 0; $i < $im_x; $i++) { for ($j = 0; $j < $im_y; $j++) { $rgb = imagecolorat($im, $i, $j); if ((int) ($i + 20 + sin($j / $im_y * 2 * M_PI) * 10) <= imagesx($distortion_im) && (int) ($i + 20 + sin($j / $im_y * 2 * M_PI) * 10) >= 0) { imagesetpixel($distortion_im, (int) ($i + 10 + sin($j / $im_y * 2 * M_PI - M_PI * 0.1) * 4), $j, $rgb); } } } //加入干扰象素; $count = 160; //干扰像素的数量 for ($i = 0; $i < $count; $i++) { $randcolor = ImageColorallocate($distortion_im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagesetpixel($distortion_im, mt_rand() % $im_x, mt_rand() % $im_y, $randcolor); } $rand = mt_rand(5, 30); $rand1 = mt_rand(15, 25); $rand2 = mt_rand(5, 10); for ($yy = $rand; $yy <= +$rand + 2; $yy++) { for ($px = -80; $px <= 80; $px = $px + 0.1) { $x = $px / $rand1; if ($x != 0) { $y = sin($x); } $py = $y * $rand2; imagesetpixel($distortion_im, $px + 80, $py + $yy, $text_c); } } //设置文件头; Header("Content-type: image/JPEG"); //以PNG格式将图像输出到浏览器或文件; ImagePNG($distortion_im); //销毁一图像,释放与image关联的内存; ImageDestroy($distortion_im); ImageDestroy($im); }
function captcha($width, $height, $code) { $font = "./font/ChalkboardBold.ttf"; $font_size = 17; $image = imagecreate($width, $height); $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 20, 40, 100); $noise_color = imagecolorallocate($image, 100, 120, 180); for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } $x = 3; $y = 20; imagettftext($image, $font_size, rand(-45, 45), $x, $y + rand() % 16, $text_color, $font, $code[0]); imagettftext($image, $font_size, rand(-45, 45), $x + 23, $y + rand() % 16, $text_color, $font, $code[1]); imagettftext($image, $font_size, rand(-45, 45), $x + 46, $y + rand() % 16, $text_color, $font, $code[2]); imagettftext($image, $font_size, rand(-45, 45), $x + 69, $y + rand() % 16, $text_color, $font, $code[3]); imagettftext($image, $font_size, rand(-45, 45), $x + 92, $y + rand() % 16, $text_color, $font, $code[4]); imagettftext($image, $font_size, rand(-45, 45), $x + 115, $y + rand() % 16, $text_color, $font, $code[5]); header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); }
public function show() { $captchaText = strtoupper(substr(md5(microtime()), 0, 7)); $this->Session->write('captcha', $captchaText); $image = imagecreate(190, 70); $background = imagecolorallocate($image, 255, 255, 255); $textColor = imagecolorallocatealpha($image, 0, 0, 0, 1); $x = 5; $y = 50; for ($i = 0; $i < 3000; $i++) { $fontSize = mt_rand(15, 28); $text = substr($captchaText, $i, 1); imagettftext($image, $fontSize, 0, $x, $y, $textColor, './impact.ttf', $text); $x = $x + 17 + mt_rand(0, 10); $y = mt_rand(40, 65); imagesetpixel($image, rand() % 235, rand() % 85, 3000000); } $line_color = imagecolorallocate($image, 4, rand() % 110, rand() % 220); for ($i = 0; $i < 10; $i++) { imageline($image, 0, rand() % 50, 200, rand() % 120, $line_color); } header("Content-type: application/jpeg"); return imagejpeg($image); imagedestroy($image); }
/** * Return captcha image * @param number $maxChar */ public function generate($maxChar = 4) { // $characters = '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'; $characters = 'ABCDEFGHKMNPQRST'; $captchaText = ''; for ($i = 0; $i < $maxChar; $i++) { $captchaText .= $characters[rand(0, strlen($characters) - 1)]; } strtoupper(substr(md5(microtime()), 0, 7)); \Session::put('captchaHash', \Hash::make($captchaText)); $image = imagecreate(30 * $maxChar, 35); $background = imagecolorallocatealpha($image, 255, 255, 255, 1); $textColor = imagecolorallocatealpha($image, 206, 33, 39, 1); $x = 5; $y = 20; $angle = 0; for ($i = 0; $i < 7; $i++) { $fontSize = 16; $text = substr($captchaText, $i, 1); imagettftext($image, $fontSize, $angle, $x, $y, $textColor, public_path('/fonts/LibreBaskerville/librebaskerville-regular.ttf'), $text); $x = $x + 17 + mt_rand(1, 10); $y = mt_rand(18, 25); $angle = mt_rand(0, 20); } header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate'); header('Pragma: no-cache'); header('Content-type: image/jpeg'); imagejpeg($image, null, 100); imagedestroy($image); }
public function __construct($width = '120', $height = '40', $characters = '6') { $code = $this->generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.75; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 20, 40, 100); $noise_color = imagecolorallocate($image, 100, 120, 180); /* generate random dots in background */ for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } /* generate random lines in background */ for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font, $code) or die('Error in imagettftext function'); /* output captcha image to browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); /* @var $CI My_Controller */ $CI = get_instance(); $CI->session->set_userdata('security_code', $code); }
function CaptchaSecurityImages($width = '120', $height = '40', $characters = '6') { $code = $this->generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.5; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 20, 20, 20); $text_color = imagecolorallocate($image, 230, 197, 89); $noise_color = imagecolorallocate($image, 0, 0, 0); /* generate random dots in background */ for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } /* generate random lines in background */ /*for( $i=0; $i<($width*$height)/150; $i++ ) { imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color); }*/ /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font, $code) or die('Error in imagettftext function'); /* output captcha image to browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); $_SESSION['security_code'] = $code; }
/** * * @param string $playername Minecraft player name * @param resource $avatar the rendered avatar (for example player head) * * @param string $background Image Path or Standard Value * @return resource the generated banner */ public static function player($playername, $avatar = NULL, $background = NULL) { $canvas = MinecraftBanner::getBackgroundCanvas(self::PLAYER_WIDTH, self::PLAYER_HEIGHT, $background); $head_height = self::AVATAR_SIZE; $head_width = self::AVATAR_SIZE; $avater_x = self::PLAYER_PADDING; $avater_y = self::PLAYER_HEIGHT / 2 - self::AVATAR_SIZE / 2; if ($avatar == NULL) { $avatar = imagecreatefrompng(__DIR__ . "/img/head.png"); imagesavealpha($avatar, true); imagecopy($canvas, $avatar, $avater_x, $avater_y, 0, 0, $head_width, $head_height); } else { $head_width = imagesx($avatar); $head_height = imagesy($avatar); if ($head_width > self::AVATAR_SIZE) { $head_width = self::AVATAR_SIZE; } if ($head_height > self::AVATAR_SIZE) { $head_height = self::AVATAR_SIZE; } $center_x = $avater_x + self::AVATAR_SIZE / 2 - $head_width / 2; $center_y = $avater_y + self::AVATAR_SIZE / 2 - $head_height / 2; imagecopy($canvas, $avatar, $center_x, $center_y, 0, 0, $head_width, $head_height); } $box = imagettfbbox(self::TEXT_SIZE, 0, MinecraftBanner::FONT_FILE, $playername); $text_width = abs($box[4] - $box[0]); $text_color = imagecolorallocate($canvas, 255, 255, 255); $remaining = self::PLAYER_WIDTH - self::AVATAR_SIZE - $avater_x - self::PLAYER_PADDING; $text_posX = $avater_x + self::AVATAR_SIZE + $remaining / 2 - $text_width / 2; $text_posY = $avater_y + self::AVATAR_SIZE / 2 + self::TEXT_SIZE / 2; imagettftext($canvas, self::TEXT_SIZE, 0, $text_posX, $text_posY, $text_color, MinecraftBanner::FONT_FILE, $playername); return $canvas; }
function createImageData() { $rand_char = PhocaguestbookHelperCaptchaTTF::generateRandomChar(6); $image_name = PhocaguestbookHelperCaptchaTTF::getRandomImage(); $image = @imagecreatefromjpeg($image_name); $ttf[0] = JPATH_COMPONENT . DS . 'assets' . DS . 'captcha' . DS . 'fonts' . DS . 'essai.ttf'; // $ttf[1] = JPATH_COMPONENT . DS . 'assets'. DS . 'captcha'.DS. 'fonts'.DS. 'vera.ttf'; $ttf[1] = JPATH_COMPONENT . DS . 'assets' . DS . 'captcha' . DS . 'fonts' . DS . 'justus.ttf'; $i = 15; $char_string = ''; foreach ($rand_char as $key => $value) { $font_color = PhocaguestbookHelperCaptchaTTF::getRandomFontColor(); $position_x = PhocaguestbookHelperCaptchaTTF::getRandomPositionX($i); $position_y = mt_rand(55, 80); $font_size = mt_rand(20, 40); $angle = mt_rand(-30, 30); $rand_ttf = mt_rand(0, 1); imagettftext($image, $font_size, $angle, $position_x, $position_y, ImageColorAllocate($image, $font_color[0], $font_color[1], $font_color[2]), $ttf[$rand_ttf], $value); $i = $i + 37; $char_string .= $value; } $image_data['outcome'] = $char_string; //$rand_char; $image_data['image'] = $image; return $image_data; }
public static function create() { $image = imagecreatetruecolor(100, 40) or die('cannot create canvas.<br>'); $red = imagecolorallocate($image, 255, 0, 0); $green = imagecolorallocate($image, 0, 255, 0); $blue = imagecolorallocate($image, 0, 0, 255); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); imagefill($image, 0, 0, $white); imagerectangle($image, 1, 1, 99, 39, $black); $color = [$red, $green, $blue]; for ($i = 1; $i <= 100; $i++) { imagesetpixel($image, mt_rand(2, 98), mt_rand(2, 38), $color[mt_rand(0, 2)]); } $source = "abcdefghigklmnopqrstuvwxyz0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ"; $first = $source[mt_rand(0, 61)]; $second = $source[mt_rand(0, 61)]; $third = $source[mt_rand(0, 61)]; $fourth = $source[mt_rand(0, 61)]; $_SESSION['captcha'] = $first . $second . $third . $fourth; $font = dirname(dirname(__DIR__)) . '/../public/assets/fonts/SpicyRice.ttf'; imagettftext($image, 20, mt_rand(-20, 20), 10, 30, $blue, $font, $first); imagettftext($image, 20, mt_rand(-20, 20), 30, 30, $blue, $font, $second); imagettftext($image, 20, mt_rand(-20, 20), 50, 30, $blue, $font, $third); imagettftext($image, 20, mt_rand(-20, 20), 70, 30, $blue, $font, $fourth); return imagejpeg($image); }
function guvenlik_resmi() { $sifre = substr(md5(rand(0, 999999999999)), -5); if ($sifre) { $this->session->set_userdata('koruma', $sifre); $yukseklik = 62; $genislik = 200; $resim = ImageCreate($genislik, $yukseklik); $siyah = ImageColorAllocate($resim, 0, 0, 0); $kirmizi = ImageColorAllocate($resim, 182, 16, 99); $beyaz = ImageColorAllocate($resim, 255, 255, 255); ImageFill($resim, 0, 0, $beyaz); $font = 'css/comic.ttf'; $font_boyut = 24; $sM = 30; $uM = 45; //kullanımı //resim adı, font boyutu, yazının açısı, yazının soldan margini, üstten margin, renk, font adı, şifrenin hangi digitinin yazılacağı bellirtiliyor imagettftext($resim, $font_boyut, rand(-45, 45), $sMa = $sM, $uM, rand(0, 255), $font, $sifre[0]); imagettftext($resim, $font_boyut, rand(-45, 45), $sM = $sMa + $sM, $uM, rand(0, 255), $font, $sifre[1]); imagettftext($resim, $font_boyut, rand(-45, 45), $sM = $sMa + $sM, $uM, rand(0, 255), $font, $sifre[2]); imagettftext($resim, $font_boyut, rand(-45, 45), $sM = $sMa + $sM, $uM, rand(0, 255), $font, $sifre[3]); imagettftext($resim, $font_boyut, rand(-45, 45), $sM = $sMa + $sM, $uM, rand(0, 255), $font, $sifre[4]); //ImageRectangle($resim, 0, 0, $genislik-1, $yukseklik-1, $kirmizi); imageline($resim, 0, $yukseklik / 2, $genislik, $yukseklik / 2, $kirmizi); imageline($resim, $genislik / 2, 0, $genislik / 2, $yukseklik, $kirmizi); header("Content-Type: image/png"); ImagePng($resim); ImageDestroy($resim); } exit; }
/** * 2015-12-08 * Точка отсчёта системы координат [0, 0] — это самая левая верхняя точка холста. * Далее кординаты увеличиваются вниз и вправо. * @override * @see \Df\GoogleFont\Fonts\Png::draw() * @used-by \Df\GoogleFont\Fonts\Png::image() * @param resource $image * @return void */ protected function draw($image) { $r = imagefill($image, 0, 0, $this->colorAllocateAlpha($image, $this->bgColor())); df_assert($r); $r = imagettftext($image, $this->fontSize(), 0, $this->marginLeft(), $this->height() - abs($this->contentBottomY()), $this->colorAllocateAlpha($image, $this->fontColor()), $this->ttfPath(), $this->text()); df_assert($r); }
public function index() { $code = substr(sha1(mt_rand()), 17, 6); $this->session->set_userdata('captcha_code', $code); $width = '120'; $height = '40'; $font = APPPATH . 'modules/contact/assets/fonts/monofont.ttf'; $font_size = $height * 0.75; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 20, 40, 100); $noise_color = imagecolorallocate($image, 100, 120, 180); /* generate random dots in background */ for ($i = 0; $i < $width * $height / 3; $i++) { imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color); } /* generate random lines in background */ for ($i = 0; $i < $width * $height / 150; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color); } /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $font, $code) or die('Error in imagettftext function'); /* output captcha image to browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); }
public function generate() { imagesavealpha($this->_owner->image, true); imagealphablending($this->_owner->image, true); $width = $this->_owner->getImageWidth(); $height = $this->_owner->getImageHeight(); $white = $this->_owner->imagecolorallocate($this->_text_color); $data = array(); $total_width = 0; for ($x = 0; $x < strlen($this->_text); $x++) { $data[$x]['text'] = $this->_text[$x]; $data[$x]['font'] = $this->_arr_ttf_font[rand(0, count($this->_arr_ttf_font) - 1)]; $data[$x]['size'] = rand($this->_text_size, $this->_text_size + $this->_text_size_random); $data[$x]['angle'] = $this->_text_angle_random / 2 - rand(0, $this->_text_angle_random); $captcha_dimensions = imagettfbbox($data[$x]['size'], $data[$x]['angle'], $data[$x]['font'], $data[$x]['text']); $data[$x]['width'] = abs($captcha_dimensions[2]) + $this->_text_spacing; $data[$x]['height'] = abs($captcha_dimensions[5]); $total_width += $data[$x]['width']; } $x_offset = ($width - $total_width) / 2; $x_pos = 0; $y_pos = 0; foreach ($data as $ld) { $y_pos = ($height + $ld['height']) / 2; imagettftext($this->_owner->image, $ld['size'], $ld['angle'], $x_offset + $x_pos, $y_pos, $white, $ld['font'], $ld['text']); $x_pos += $ld['width']; } return true; }
/** * @param $text * @return resource */ private function createImage($text) { if (!is_string($text) || trim($text) == '') { $text = 'ERROR'; } // Create an image from captchaBackground.png $image = imagecreatefrompng(realpath('bundles/comun/images/captchaBackground.png')); // Set the font colour $colour = imagecolorallocate($image, 183, 178, 152); // Set the font //$font = '../fonts/Anorexia.ttf'; $font = realpath('bundles/comun/fonts/Anorexia.ttf'); // Set a random integer for the rotation between -15 and 15 degrees $rotate = rand(-15, 15); // Create an image using our original image and adding the detail imagettftext($image, 14, $rotate, 18, 30, $colour, $font, $text); // Output the image as a png //imagepng($image); /*return new Response( $captchaText, 200, array( 'Content-Type' => 'image/png', 'Cache-Control' => 'no-cache' ) );*/ return $image; }
function _qmd_img_list($pic_path = '', $user_uid = 0, $user_face = '', $topic_content = '', $topic_dateline) { header("Content-type: image/png"); $bg = imagecreatefromjpeg($pic_path); $white = imagecolorallocate($bg, 00, 00, 00); $content = str_split($topic_content, 40); $content = array_iconv($this->Config['charset'], 'utf-8', $content); $topic_url = $this->Config['site_url']; $topic_date = array_iconv($this->Config['charset'], 'utf-8', $topic_dateline . ' | ' . '记事狗微博'); imagettftext($bg, 9, 0, 130, 25, $white, "images/simsun.ttc", $content[0]); imagettftext($bg, 9, 0, 130, 45, $white, "images/simsun.ttc", $content[1]); imagettftext($bg, 9, 0, 130, 70, $white, "images/simsun.ttc", $topic_date); imagettftext($bg, 9, 0, 218, 90, $white, "images/simsun.ttc", $topic_url); $dst_im = imagecreatefromjpeg($bg); $dst_info = getimagesize($bg); $src = $user_face; $src_im = imagecreatefromjpeg($src); $src_info = getimagesize($src); $dst_x = 20; $dst_y = 12; $src_x = 0; $src_y = 0; $src_w = $src_info[0]; $src_h = $src_info[1]; $alpha = 100; imagecopymerge($bg, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $alpha); $image_path = RELATIVE_ROOT_PATH . 'images/qmd/' . face_path(MEMBER_ID); if (!is_dir($image_path)) { jio()->MakeDir($image_path); } $image_file = $image_path . MEMBER_ID . '_o.png'; imagepng($bg, $image_file); imagedestroy($bg); return $image_file; }