/** * Saves avatar to DB and outputs that as PNG * @param int $id */ public function generate($id) { $uid = (int) $id; $user = User::get($uid); $initial = strtoupper($user->username[0]); $bI = rand(0, 41); $color = $this->BACKGROUND_COLORS[$bI]; $rgb = $this->hex2rgb($color); // Create a 300x100 image $im = imagecreatetruecolor(128, 128); $red = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]); $black = imagecolorallocate($im, 0xff, 0xff, 0xff); // Make the background red imagefilledrectangle($im, 0, 0, 128, 128, $red); // Path to our ttf font file $font_file = ASSET_DIR . 'img/general/SourceCodePro-Light.ttf'; // Draw the text 'PHP Manual' using font size 13 imagefttext($im, 80, 0, 32, 100, $black, $font_file, $initial); // Output image to the browser header('Content-Type: image/png'); $name = $initial . '_' . str_replace('#', '', $color) . '.png'; imagepng($im, AVATAR_PATH . $name); $resizer = new \Ext\ImageResize(); $iconW = 36; $iconH = 36; $iconPath = AVATAR_PATH . 'icons/' . $name; $success = $resizer->smart_resize_image(AVATAR_PATH . $name, NULL, $iconW, $iconH, true, $iconPath, FALSE, FALSE, 75); if (!$success) { @copy(AVATAR_PATH . $name, $iconPath); } $user->set(array('avatar' => $name)); imagepng($im); imagedestroy($im); }
/** * Outputs the Captcha image. * * @param boolean html output * @return mixed */ public function render($html) { // Creates a black image to start from $this->image_create(Captcha::$config['background']); // Add random white/gray arcs, amount depends on complexity setting $count = (Captcha::$config['width'] + Captcha::$config['height']) / 2; $count = $count / 5 * min(10, Captcha::$config['complexity']); for ($i = 0; $i < $count; $i++) { imagesetthickness($this->image, mt_rand(1, 2)); $color = imagecolorallocatealpha($this->image, 255, 255, 255, mt_rand(0, 120)); imagearc($this->image, mt_rand(-Captcha::$config['width'], Captcha::$config['width']), mt_rand(-Captcha::$config['height'], Captcha::$config['height']), mt_rand(-Captcha::$config['width'], Captcha::$config['width']), mt_rand(-Captcha::$config['height'], Captcha::$config['height']), mt_rand(0, 360), mt_rand(0, 360), $color); } // Use different fonts if available $font = Captcha::$config['fontpath'] . Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])]; // Draw the character's white shadows $size = (int) min(Captcha::$config['height'] / 2, Captcha::$config['width'] * 0.8 / strlen($this->response)); $angle = mt_rand(-15 + strlen($this->response), 15 - strlen($this->response)); $x = mt_rand(1, Captcha::$config['width'] * 0.9 - $size * strlen($this->response)); $y = (Captcha::$config['height'] - $size) / 2 + $size; $color = imagecolorallocate($this->image, 255, 255, 255); imagefttext($this->image, $size, $angle, $x + 1, $y + 1, $color, $font, $this->response); // Add more shadows for lower complexities Captcha::$config['complexity'] < 10 and imagefttext($this->image, $size, $angle, $x - 1, $y - 1, $color, $font, $this->response); Captcha::$config['complexity'] < 8 and imagefttext($this->image, $size, $angle, $x - 2, $y + 2, $color, $font, $this->response); Captcha::$config['complexity'] < 6 and imagefttext($this->image, $size, $angle, $x + 2, $y - 2, $color, $font, $this->response); Captcha::$config['complexity'] < 4 and imagefttext($this->image, $size, $angle, $x + 3, $y + 3, $color, $font, $this->response); Captcha::$config['complexity'] < 2 and imagefttext($this->image, $size, $angle, $x - 3, $y - 3, $color, $font, $this->response); // Finally draw the foreground characters $color = imagecolorallocate($this->image, 0, 0, 0); imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response); // Output return $this->image_render($html); }
function generate_image($canvas) { global $captcha_font_path, $captcha_img_path; $hit = "Считай детерминант!"; $a = rand(1, 9); $b = rand(1, 9); $c = rand(1, 9); $d = rand(1, 9); $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); imageline($canvas, 20, 10, 20, 60, $color); imageline($canvas, 160, 10, 160, 60, $color); $rand = " =?"; for ($i = 0; $i < strlen($rand); $i++) { $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); imagefttext($canvas, 20, rand(-45, 45), 144 + 20 * $i, 50, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $rand[$i]); } $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); imagefttext($canvas, 20, rand(-5, 5), 40, 20, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $a); imagefttext($canvas, 20, rand(-5, 5), 40, 50, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $b); imagefttext($canvas, 20, rand(-5, 5), 120, 20, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $c); imagefttext($canvas, 20, rand(-5, 5), 120, 50, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $d); imagefttext($canvas, 11, rand(-5, 5), 15, 75, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $hit); $nme = $this->ucaptcha->get_filename(); //imagepng($canvas,$captcha_img_path."/".$nme.".png"); imagepng($canvas); $ans = $a * $d - $b * $c; $captcha[0] = $nme; $captcha[1] = $ans; return $captcha; }
function msg($msg) { $container = imagecreate(250, 170); //SET SIZE OF IMAGE $black = imagecolorallocate($container, 0, 0, 0); //set RGB for black $white = imagecolorallocate($container, 255, 255, 255); //set RGB for white $font = '/Library/Fonts/Arial.ttf'; //grab the font from my harddrive imagefilledrectangle($container, 0, 0, 250, 170, $black); // crate rectangle graphic $px = imagesx($container) / (strlen($msg) / 1.15); // x axis position $py = imagesy($container) / 3.5; // ;y axis position // Write text to the image using fonts using FreeType 2 imagefttext($container, 28, -27, $px, $py, $white, $font, $msg); header("Content-type: image/png"); // Change header type imagepng($container, null); // Output a png image to either the browser or a file imagedestroy($container); // Frees any memory associated with the image }
function vCode($num = 4, $size = 20, $width = 0, $height = 0) { !$width && ($width = $num * $size * 4 / 5 + 5); !$height && ($height = $size + 10); $str = "0123456789"; //验证码字符全集 $code = ''; for ($i = 0; $i < $num; $i++) { $code .= $str[mt_rand(0, strlen($str) - 1)]; } // 画图像 $im = imagecreatetruecolor($width, $height); // 定义要用到的颜色 $back_color = imagecolorallocate($im, 255, 255, 255); // $boer_color = imagecolorallocate($im, 100, 100, 100); $text_color = imagecolorallocate($im, 50, 50, 50); // 画背景 imagefilledrectangle($im, 0, 0, $width, $height, $back_color); // 画边框 // imagerectangle($im, 0, 0, $width-1, $height-1, $boer_color); // 画干扰线 for ($i = 0; $i < 30; $i++) { $font_color = imagecolorallocate($im, 0, 0, 0); imagearc($im, mt_rand(-$width, $width), mt_rand(-$height, $height), mt_rand(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color); } // 画验证码 @imagefttext($im, $size, 0, 5, $size + 3, $text_color, 'images/index/Callie-Mae.ttf', $code); $_SESSION["VerifyCode"] = $code; header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate"); header("Content-type: image/png;charset=gb2312"); imagepng($im); imagedestroy($im); }
public function genImage() { // text to be printed if (!$this->value) { $this->value = $this->genValue(); } $chars = mb_strlen($this->value, "utf8"); // create canvas $image = imagecreatetruecolor($this->config["width"], $this->config["height"]); $bg = imagecolorallocate($image, $this->config["background"][0], $this->config["background"][1], $this->config["background"][2]); $fg = imagecolorallocate($image, $this->config["color"][0], $this->config["color"][1], $this->config["color"][2]); imagefill($image, $this->config["width"] / 2, $this->config["height"] / 2, $bg); // font size if ($this->config["minfont"] == "auto") { $fsmin = min($this->config["width"] / ($chars + 1), $this->config["height"]); } else { $fsmin = $this->config["minfont"]; } if ($this->config["maxfont"] == "auto") { $fsmax = min($this->config["width"] / ($chars + 1), $this->config["height"]); } else { $fsmax = $this->config["maxfont"]; } // Padding - offset of the text $xStart = -$fsmax / 2; $xEnd = $this->config["width"]; $yStart = $this->config["height"] * 0.6; $yEnd = $this->config["height"] * 0.8; // angle $angle = mt_rand(-15, 15); // print chars for ($i = 0; $i < $chars; $i++) { $angle += mt_rand(-3, 3); $xStart += $fsmax; imagefttext($image, mt_rand($fsmin, $fsmax), $angle, $xStart, mt_rand($yStart, $yEnd), $fg, $this->config["font"], mb_substr($this->value, $i, 1, "utf8")); } // Add some noise to the image. if ($this->config["noise"]) { for ($i = 0; $i < $this->config["noise"]; $i++) { $color = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); for ($j = 0; $j < $this->config["width"] * $this->config["height"] / 100; $j++) { imagesetpixel($image, mt_rand(0, $this->config["width"]), mt_rand(0, $this->config["height"]), $color); } } } // blur image if ($this->config["blur"]) { imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR); } // send image if ($this->config["format"] == "png") { header("Content-type: image/png"); imagepng($image); } else { header("Content-type: image/jpeg"); imagejpeg($image, null, 80); } imagedestroy($image); //return $image; }
function generateImage(&$captcha) { $width = $this->getWidth(); $height = $this->getHeight(); $length = String::strlen($captcha->getValue()); $value = $captcha->getValue(); $image = imagecreatetruecolor($width, $height); $fg = imagecolorallocate($image, rand(128, 255), rand(128, 255), rand(128, 255)); $bg = imagecolorallocate($image, rand(0, 64), rand(0, 64), rand(0, 64)); imagefill($image, $width / 2, $height / 2, $bg); $xStart = rand($width / 12, $width / 3); $xEnd = rand($width * 2 / 3, $width * 11 / 12); for ($i = 0; $i < $length; $i++) { imagefttext($image, rand(20, 34), rand(-15, 15), $xStart + ($xEnd - $xStart) * $i / $length + rand(-5, 5), rand(40, 60), $fg, Config::getVar('captcha', 'font_location'), String::substr($value, $i, 1)); } // Add some noise to the image. for ($i = 0; $i < 20; $i++) { $color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); for ($j = 0; $j < 20; $j++) { imagesetpixel($image, rand(0, $this->getWidth()), rand(0, $this->getHeight()), $color); } } header('Content-type: ' . $this->getMimeType()); imagepng($image); imagedestroy($image); }
function generate_image($canvas) { /* Algorithm - Vieta's theorem: b = -(x1 + x2), c = x1 * x2 */ global $captcha_font_path, $captcha_img_path; /* Roots */ $x1 = $this->rand_except_zero(-20, 20); $x2 = $this->rand_except_zero(-20, 20); /* Coefficients */ $a = $this->rand_except_zero(-5, 5); $b = -($x1 + $x2) * $a; $c = $x1 * $x2 * $a; switch (rand(0, 1)) { case 0: $hit = "max(x1, x2)=?"; $ans = max($x1, $x2); break; case 1: $hit = "min(x1, x2)=?"; $ans = min($x1, $x2); break; } /* Format output */ $eq = $this->format($a, $b, $c); for ($i = 0; $i < strlen($eq); $i++) { $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); imagefttext($canvas, 12, rand(-5, 5), 5 + 12 * $i, 50, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $eq[$i]); } imagefttext($canvas, 10, rand(-5, 5), 20, 75, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $hit); $nme = $this->ucaptcha->get_filename(); imagepng($canvas); $captcha[0] = $nme; $captcha[1] = (string) $ans; return $captcha; }
/** * @param Thumbnail $thumbnail * @return void * @throws Exception\NoThumbnailAvailableException */ public function refresh(Thumbnail $thumbnail) { $temporaryPathAndFilename = null; try { $filename = pathinfo($thumbnail->getOriginalAsset()->getResource()->getFilename(), PATHINFO_FILENAME); $temporaryLocalCopyFilename = $thumbnail->getOriginalAsset()->getResource()->createTemporaryLocalCopy(); $temporaryPathAndFilename = $this->environment->getPathToTemporaryDirectory() . uniqid('ProcessedFontThumbnail-') . '.' . $filename . '.jpg'; $width = 1000; $height = 1000; $im = imagecreate($width, $height); $red = imagecolorallocate($im, 0xff, 0xff, 0xff); $black = imagecolorallocate($im, 0x0, 0x0, 0x0); imagefilledrectangle($im, 0, 0, 1000, 1000, $red); imagefttext($im, 48, 0, 80, 150, $black, $temporaryLocalCopyFilename, 'Neos Font Preview'); imagefttext($im, 32, 0, 80, 280, $black, $temporaryLocalCopyFilename, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'); imagefttext($im, 32, 0, 80, 360, $black, $temporaryLocalCopyFilename, 'abcdefghijklmopqrstuvwxyz'); imagefttext($im, 32, 0, 80, 440, $black, $temporaryLocalCopyFilename, '1234567890'); imagefttext($im, 32, 0, 80, 560, $black, $temporaryLocalCopyFilename, '+ " * ç % & / ( ) = ? @ €'); imagejpeg($im, $temporaryPathAndFilename); $resource = $this->resourceManager->importResource($temporaryPathAndFilename); $processedImageInfo = $this->resize($thumbnail, $resource); $thumbnail->setResource($processedImageInfo['resource']); $thumbnail->setWidth($processedImageInfo['width']); $thumbnail->setHeight($processedImageInfo['height']); Files::unlink($temporaryPathAndFilename); } catch (\Exception $exception) { Files::unlink($temporaryPathAndFilename); $filename = $thumbnail->getOriginalAsset()->getResource()->getFilename(); $sha1 = $thumbnail->getOriginalAsset()->getResource()->getSha1(); $message = sprintf('Unable to generate thumbnail for the given font (filename: %s, SHA1: %s)', $filename, $sha1); throw new Exception\NoThumbnailAvailableException($message, 1433109653, $exception); } }
function generate_image($canvas) { global $captcha_font_path, $captcha_img_path; $txt = $this->get_random_question(); $rand = $txt[0]; $lines = explode("\n", $rand); for ($i = 0; $i < count($lines); $i++) { $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); imagefttext($canvas, 7 + rand(0, 3), rand(-5, 5), 5, 40 + $i * 10, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $lines[$i]); } $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); imagefttext($canvas, 8, rand(-5, 5), 5, 10 + $i * 10, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $txt[1]); $nme = $this->ucaptcha->get_filename(); imagepng($canvas); //imagepng($canvas,$captcha_img_path."/".$nme.".png"); $captcha[0] = $nme; switch ($l) { case 0: $captcha[1] = 'Вюрца'; break; case 1: $captcha[1] = 'Вюрца-Фиттига'; break; case 2: $captcha[1] = 'Зинина'; break; case 3: $captcha[1] = 'Кучерова'; break; case 4: $captcha[1] = 'Коновалова'; break; } return $captcha; }
function generate_image($canvas) { global $captcha_font_path, $captcha_img_path; imagesetthickness($canvas, 1); //$rand = preg_replace("/([0-9])/e","chr((\\1+112))",rand(100000,999999)); $var = rand(0, 1); $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); $liney = rand(3, 6); $linex = rand(6, 12); for ($i = $liney; $i > 0; $i--) { $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); // if ($i==0) // { // $this->arrow($canvas,10,$i*60/$liney,200,$i*60/$liney, 5, 5, $color ); // } // else // { imageline($canvas, 10, $i * 60 / $liney, 200, $i * 60 / $liney, $color); // } imagefttext($canvas, 8, rand(-45, 45), 3, $i * 60 / $liney, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $liney - $i); $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); } for ($i = 0; $i < $linex; $i++) { $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); imageline($canvas, 30 + $i * 160 / $linex, 0, 30 + $i * 160 / $linex, 70, $color); $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); imagefttext($canvas, 8, rand(-45, 45), 30 + $i * 160 / $linex, 80, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $i); $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); } $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); imagesetthickness($canvas, 1); $x = rand(0, $linex - 1); $y = rand(0, $liney - 1); for ($i = -5; $i < 5; $i++) { $xr = rand(0, $linex); $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); $yr = rand(0, $liney); imageline($canvas, 30 + $x * 160 / $linex, ($liney - $y) * 60 / $liney, 30 + $xr * 160 / $linex, ($liney - $yr) * 60 / $liney, $color); } $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); //echo "!".$x."===".$y."!<br>"; if ($var) { imagefttext($canvas, 20, rand(-45, 45), 40 + 20, 50, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", "y=?"); } else { imagefttext($canvas, 12, rand(-45, 45), 100, 70, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", "X=?"); } $nme = $this->ucaptcha->get_filename(); imagepng($canvas); //imagepng($canvas,$captcha_img_path."/".$nme.".png"); $captcha[0] = $nme; if ($var) { $captcha[1] = $y; $captcha[2] = "Введите координату Y точки, откуда берут начало прямые"; } else { $captcha[1] = $x; $captcha[2] = "Введите координату X точки, откуда берут начало прямые"; } return $captcha; }
private function nameToImage() { $fontcolor = imagecolorallocate($this->image, $this->fontcolor_red, $this->fontcolor_green, $this->fontcolor_blue); if (preg_match("/^([0-9a-zA-Z_]|[[:punct:]])\$/", $this->name_arr, $this->matches)) { imagefttext($this->image, $this->img_width * 0.52, 0, $this->img_width * 0.33, $this->img_width * 0.7, $fontcolor, $this->font_file, $this->matches[0]); } else { imagefttext($this->image, $this->img_width * 0.52, 0, $this->img_width * 0.17, $this->img_width * 0.73, $fontcolor, $this->font_file, $this->name_arr); } }
private function makeImage() { $achievement = imagecreatefrompng($this->background); imagesavealpha($achievement, true); imagecopyresampled($achievement, $this->iconImage, 16, 16, 0, 0, 32, 32, 32, 32); imagefttext($achievement, 12, 0, 60, 28, imagecolorallocate($achievement, 255, 255, 0), $this->font, $this->topText); imagefttext($achievement, 12, 0, 60, 50, imagecolorallocate($achievement, 255, 255, 255), $this->font, $this->bottomText); //imagefttext($achievement, 6, 0, 10, 57, imagecolorallocate($achievement, 255, 255, 0), $this->font, "achievecraft.net"); return $achievement; }
public function mkCode($num = 4, $size = 20, $width = 0, $height = 0) { // $num => 验证码字符数目 // $size => 字符大小(注意单位是pt不是px) // $width这只是大概的图像宽度,因为不同字体的同样font-size所占width也是有微小差距的 !$width && ($width = $num * $size); //根据要显示验证码字符长度设置验证码图像长度 !$height && ($height = $size + 10); //设置图像的高度 $font_url = dirname(dirname(__FILE__)) . "/font/mywanderingheart.ttf"; //如果字体路径错误:会无法显示验证码,但是有图像(后面这一点得看情况) // 我们要显示的验证码字符集合 $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW"; //要显示的验证码 $code = ''; for ($i = 0; $i < $num; $i++) { $code .= $str[mt_rand(0, strlen($str) - 1)]; } // 画图像,新建一个真彩色图像,并返回一个图像标识符赋值给 $im $im = imagecreatetruecolor($width, $height); // 定义要用到的颜色 $background_color = imagecolorallocate($im, 255, 255, 255); //这里默认白色背景 $border_color = imagecolorallocate($im, 255, 255, 255); //这里默认白色边框 $text_color = imagecolorallocate($im, 50, 50, 50); //这里默认淡黑色字体颜色 // 画背景 imagefilledrectangle($im, 0, 0, $width, $height, $background_color); // 画边框(该函数事实上是画一个矩形) imagerectangle($im, 0, 0, $width - 1, $height - 1, $border_color); // 画干扰点 for ($i = 0; $i < 10 * $num; $i++) { $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $font_color); } // 默认画4条干扰线 //这地方可以自己研究一下 for ($i = 0; $i < 4; $i++) { $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagearc($im, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, 360), mt_rand(0, 360), $font_color); // imagearc($im, 0, 0, $width*2, $height, 0, 90, $font_color); } //好吧,是时候把验证码字符画上去了 @imagefttext($im, $size, 0, 10, $size + 5, $text_color, $font_url, $code); // 喂,浏览器!我给你发的这个东西不要从你的缓存中拿啊。还有啊,这是一个png图像,不要解析错了。 header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate"); header("Content-type: image/png;charset=utf-8"); // imagepng 以 PNG 格式将图像输出到浏览器,如果指定了第二个参数 filename 将保存图像到文件 // 例如imagepng( $im, '1.png' ); 将图像保存到同级目录下的1.png文件(如果该文件已存在则覆盖) imagepng($im); imagedestroy($im); return $code; //把验证码字符返回 }
public function msg($msg) { $container = imagecreatefromjpeg('images/captcha_bg.jpg'); $black = imagecolorallocate($container, 0, 0, 0); //$white = imagecolorallocate($container,255,255,255); $font = 'fonts/acce.ttf'; imagerectangle($container, 0, 0, 200, 100, $black); imagefttext($container, 24, 0, 22, 62, $black, $font, $msg); //can make a randomizer imagepng($container, 'images/newCaptcha.jpg'); imagedestroy($container); }
public static function make($is_text = 'no') { global $uri; // $db1=array('so','feel','catch','great','well','type','run','fast','call','like','hot','cold','cool','can','write','read','speed'); // $db2=array('me','what','baby','care','rank','park','summer','katy'); // shuffle($db1); // shuffle($db2); $main_img = imagecreatetruecolor(250, 78); $bg_color = imagecolorallocate($main_img, 255, 255, 255); imagefill($main_img, 0, 0, $bg_color); $captcha_str = String::randNumber(6); // $captcha_str = $db1[0] . ' ' . $db2[0]; $dbColor = array(0 => array(100, 161, 39), 1 => array(64, 64, 64), 2 => array(65, 192, 209), 3 => array(237, 89, 26), 4 => array(237, 26, 72), 5 => array(163, 39, 161), 6 => array(78, 207, 96), 7 => array(219, 24, 157)); $dbRotator = array(45, 10, 30, -5, -10, -30, -50); $dbUpDown = array(55, 40, 50, 45, 35, 30, 65); $font_file = ROOT_PATH . 'uploads/Eirik Raude.ttf'; $strLen = strlen($captcha_str); $space = 20; for ($i = 0; $i < $strLen; $i++) { $colorRand = rand(0, 7); $rotatorRand = rand(0, 5); $updownRand = rand(0, 3); $black = imagecolorallocate($main_img, $dbColor[$colorRand][0], $dbColor[$colorRand][1], $dbColor[$colorRand][2]); imagefttext($main_img, 30, $dbRotator[$rotatorRand], $space, $dbUpDown[$updownRand], $black, $font_file, $captcha_str[$i]); $space += 20; } // $colorRand = rand(0, 7); // // $black = imagecolorallocate($main_img, $dbColor[$colorRand][0], $dbColor[$colorRand][1], $dbColor[$colorRand][2]); // // imagefttext($main_img, 30, 0, 15, 55, $black, $font_file, $captcha_str); // Create session $hash = md5($captcha_str); // self::$thisHash=$hash; if (!isset($_SESSION['captcha'])) { $_SESSION['captcha'] = array(); } $_SESSION['captcha'][$hash] = 'OK'; // End create session if ($is_text == 'no') { header("Content-type: image/png"); imagepng($main_img); } else { $savePath = ROOT_PATH . 'uploads/' . String::randAlpha(12) . '.png'; imagepng($main_img, $savePath); // $dataImg = ob_get_contents(); // ob_end_clean(); $dataImg = file_get_contents($savePath); unlink($savePath); return 'data:image/png;base64,' . base64_encode($dataImg); } }
/** * @param resource &$objResource * * @return bool */ public function render(&$objResource) { $strFontPath = class_resourceloader::getInstance()->getPathForFile("/system/fonts/" . $this->strFont); if ($strFontPath !== false && is_file(_realpath_ . $strFontPath)) { $intColor = $this->allocateColor($objResource, $this->arrColor); $strText = html_entity_decode($this->strText, ENT_COMPAT, "UTF-8"); imagealphablending($objResource, true); imagefttext($objResource, $this->floatSize, $this->floatAngle, $this->intX, $this->intY, $intColor, _realpath_ . $strFontPath, $strText); imagealphablending($objResource, false); return true; } return false; }
function text2image($height, $text) { header("Content-type: image/png"); $width = $height * strlen($text)/ 5 * 2; $font = "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf"; $image = imageCreate($width, $height); $backgroundColor = imageColorAllocate($image, 255, 255, 255); $textColor = imageColorAllocate($image, 0, 0, 0); imagefttext($image, $height/2, 0, 0, $height/10*9, $textColor, $font, $text); imageInterlace($image, 1); imageColorTransparent($image, $backgroundColor); imagePNG($image); }
private function _image($string, array $options) { ob_clean(); $height = !empty($options['height']) ? intval($options['height']) : $this->height; $width = ceil($height * 13 / 24); $text_offset = floor($height * 2 / 13); $text_y = floor($height * 18 / 24); $font_size_min = floor($width / 18 * 24); $font_size_max = floor($width / 16 * 24); $imageY = $height; //the image height $imageX = strlen($string) * $width; //the image width $im = imagecreatetruecolor($imageX, $imageY); //背景 $bgcolor = mt_rand(0, 3); imagefill($im, 0, 0, imagecolorallocate($im, $this->bgcolors[$bgcolor][0], $this->bgcolors[$bgcolor][1], $this->bgcolors[$bgcolor][2])); //角度旋转写入 $color = mt_rand(0, 2); $fontColor = imagecolorallocate($im, $this->colors[$color][0], $this->colors[$color][1], $this->colors[$color][2]); $font = mt_rand(0, 1); for ($i = 0; $i < strlen($string); $i++) { $angle = mt_rand(-$this->angle, $this->angle); //角度随机 $fontsize = mt_rand($font_size_min, $font_size_max); //字体大小随机 imagefttext($im, $fontsize, $angle, $text_offset + $i * ($width - $text_offset), $text_y, $fontColor, $this->fonts[$font], $string[$i]); } //扭曲 $dstim = imagecreatetruecolor($imageX, $imageY); imagefill($dstim, 0, 0, imagecolorallocate($dstim, 255, 255, 255)); $this->contort = mt_rand(1, $this->contort); $funcs = array('sin', 'cos'); $func = $funcs[mt_rand(0, 1)]; for ($j = 0; $j < $imageY; $j++) { $amend = round($func($j / $imageY * 2 * M_PI - M_PI * 0.5) * $this->contort); for ($i = 0; $i < $imageX; $i++) { $rgb = imagecolorat($im, $i, $j); imagesetpixel($dstim, $i + $amend, $j, $rgb); } } //边框 if (empty($options['no_border'])) { $border = imagecolorallocate($dstim, 222, 222, 222); imagerectangle($dstim, 0, 0, $imageX - 1, $imageY - 1, $border); } header("content-type:image/png\r\n"); imagepng($dstim); imagedestroy($im); imagedestroy($dstim); }
/** * {@inheritdoc} */ public function draw(CanvasInterface $canvas, Text $text) { $x = $text->getCoordinate()->getX(); $y = $text->getCoordinate()->getY(); $width = $this->getWidth(); $outlineColor = $this->getColor()->getValue(); $result = true; for ($xc = $x - abs($width); $xc <= $x + abs($width); $xc++) { for ($yc = $y - abs($width); $yc <= $y + abs($width); $yc++) { $result = @imagefttext($canvas->getHandler(), $text->getFontSize(), $text->getAngle(), $xc, $yc + $text->getFontSize(), $outlineColor, $text->getFont(), $text->getString(), array('linespacing' => $text->getLineSpacing())); } } return (bool) ($result && @imagefttext($canvas->getHandler(), $text->getFontSize(), $text->getAngle(), $text->getCoordinate()->getX(), $text->getCoordinate()->getY() + $text->getFontSize(), $text->getColor()->getValue(), $text->getFont(), $text->getString(), array('linespacing' => $text->getLineSpacing()))); }
public function apply($resource) { // Find original dimensions: $imageWidth = imagesx($resource->image); $imageHeight = imagesy($resource->image); $fontbox = imageftbbox($this->settings->size, 0, realpath($this->settings->font), $this->settings->text); $width = $fontbox[4] - $fontbox[6]; $width += $this->settings->padding * 2; $height = $fontbox[1] - $fontbox[7]; $height += $this->settings->padding * 2; $image = imagecreatetruecolor($width, $height); $bg = new Colour($this->settings->bg); $fg = new Colour($this->settings->fg); $bg = $bg->allocate($image); $fg = $fg->allocate($image); imagesavealpha($image, true); imagesavealpha($resource->image, true); imagefill($image, 0, 0, $bg); imagefttext($image, $this->settings->size, 0, $this->settings->padding, $this->settings->size + $this->settings->padding, $fg, realpath($this->settings->font), $this->settings->text); $margin = $this->settings->margin; // Calculate X alignment: if ($this->settings->xalign == 'left') { $offsetX = $margin; } else { if ($this->settings->xalign == 'right') { $offsetX = round(max($imageWidth, $width) - min($imageWidth, $width)) - $margin; } else { $offsetX = 0 - round((min($imageWidth, $width) - max($imageWidth, $width)) / 2); } } // Invert X: //if ($imageWidth < $width) { // $offsetX = 0 - $offsetX; //} // Calculate Y alignment: if ($this->settings->yalign == 'top') { $offsetY = $margin; } else { if ($this->settings->yalign == 'bottom') { $offsetY = round(max($imageHeight, $height) - min($imageHeight, $height)) - $margin; } else { $offsetY = 0 - round((min($imageHeight, $height) - max($imageHeight, $height)) / 2); } } // Invert Y: //if ($imageHeight < $height) { // $offsetY = 0 - $offsetY; //} imagecopyresampled($resource->image, $image, $offsetX, $offsetY, 0, 0, $width, $height, $width, $height); }
function generate_image($canvas) { global $captcha_font_path, $captcha_img_path; $hit = "R=? (Ohms)"; $a1 = rand(0, 9); $a2 = rand(0, 9); $a3 = rand(0, 9); $m = rand(0, 9); $lcolor[0] = imagecolorallocate($canvas, 0, 0, 0); // black $lcolor[1] = imagecolorallocate($canvas, 150, 75, 0); // brown $lcolor[2] = imagecolorallocate($canvas, 255, 0, 0); // red $lcolor[3] = imagecolorallocate($canvas, 255, 165, 0); // orange $lcolor[4] = imagecolorallocate($canvas, 255, 255, 0); // yellow $lcolor[5] = imagecolorallocate($canvas, 0, 255, 0); // green $lcolor[6] = imagecolorallocate($canvas, 0, 0, 255); // blue $lcolor[7] = imagecolorallocate($canvas, 139, 0, 255); // violet $lcolor[8] = imagecolorallocate($canvas, 127, 127, 127); // grey $lcolor[9] = imagecolorallocate($canvas, 255, 255, 255); // white // выводы резистора $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); imagefilledrectangle($canvas, 20, 33, 40, 37, $color); imagefilledrectangle($canvas, 140, 33, 160, 37, $color); // тело резистора $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); imagefilledrectangle($canvas, 40, 20, 140, 50, $color); // линии imagefilledrectangle($canvas, 55, 20, 65, 50, $lcolor[$a1]); imagefilledrectangle($canvas, 75, 20, 85, 50, $lcolor[$a2]); imagefilledrectangle($canvas, 95, 20, 105, 50, $lcolor[$a3]); imagefilledrectangle($canvas, 115, 20, 125, 50, $lcolor[$m]); imagefttext($canvas, 11, rand(-5, 5), 15, 75, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $hit); $nme = $this->ucaptcha->get_filename(); imagepng($canvas); //imagepng($canvas,$captcha_img_path."/".$nme.".png"); $ans = ($a1 * 100 + $a2 * 10 + $a3 * 1) * pow(10, $m); $captcha[0] = $nme; $captcha[1] = $ans; return $captcha; }
function generate_image($canvas) { global $captcha_font_path, $captcha_img_path; $rand = preg_replace("/([0-9])/e", "chr((\\1+112))", rand(100000, 999999)); for ($i = 0; $i < strlen($rand); $i++) { $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); imagefttext($canvas, 20, rand(-45, 45), 40 + 20 * $i, 50, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $rand[$i]); } $nme = $this->ucaptcha->get_filename(); //imagepng($canvas,$captcha_img_path."/".$nme.".png"); imagepng($canvas); $captcha[0] = $nme; $captcha[1] = $rand; return $captcha; }
function message($msg) { $canvas = imagecreate(270, 50); $black = imagecolorallocate($canvas, 0, 0, 0); $white = imagecolorallocate($canvas, 255, 255, 255); $font = 'fonts/captcha.ttf'; // imagefilledrectangle(image, x1, y1, x2, y2, color) imagefilledrectangle($canvas, 0, 0, 250, 150, $black); // imagefttext(image, size, angle, x, y, color, fontfile, text) imagefttext($canvas, 30, 0, 55, 38, $white, $font, $msg); // imagepng(image) imagepng($canvas, 'cap.png'); // Frees up the resources imagedestroy($canvas); }
/** * Outputs the Captcha image. * * @param boolean html output * @return mixed */ public function render() { // Init Challenge; $this->initChallenge(); // Creates $this->image $this->createImage(Captcha::$config['background']); // Add a random gradient if (empty(Captcha::$config['background'])) { $color1 = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); $color2 = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); $this->gradientImage($color1, $color2); } // Add a few random circles for ($i = 0, $count = mt_rand(10, Captcha::$config['complexity'] * 3); $i < $count; $i++) { $color = imagecolorallocatealpha($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255), mt_rand(80, 120)); $size = mt_rand(5, Captcha::$config['height'] / 3); imagefilledellipse($this->image, mt_rand(0, Captcha::$config['width']), mt_rand(0, Captcha::$config['height']), $size, $size, $color); } // Calculate character font-size and spacing $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / strlen($this->response); $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen($this->response)); // Background alphabetic character attributes $color_limit = mt_rand(96, 160); $chars = 'ABEFGJKLPQRTVY'; // Draw each Captcha character with varying attributes for ($i = 0, $strlen = strlen($this->response); $i < $strlen; $i++) { // Use different fonts if available $font = Captcha::$config['fontpath'] . Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])]; $angle = mt_rand(-40, 20); // Scale the character size on image height $size = $default_size / 10 * mt_rand(8, 12); $box = imageftbbox($size, $angle, $font, $this->response[$i]); // Calculate character starting coordinates $x = $spacing / 4 + $i * $spacing; $y = Captcha::$config['height'] / 2 + ($box[2] - $box[5]) / 4; // Draw captcha text character // Allocate random color, size and rotation attributes to text $color = imagecolorallocate($this->image, mt_rand(150, 255), mt_rand(200, 255), mt_rand(0, 255)); // Write text character to image imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response[$i]); // Draw "ghost" alphabetic character $text_color = imagecolorallocatealpha($this->image, mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand(70, 120)); $char = substr($chars, mt_rand(0, 14), 1); imagettftext($this->image, $size * 2, mt_rand(-45, 45), $x - mt_rand(5, 10), $y + mt_rand(5, 10), $text_color, $font, $char); } // Output return $this->renderImage(); }
/** * * @param string $text * @param number $x * @param number $y * @param number $fontSize * @param number $angle the angle in degrees * @param number $red value of red component * @param number $green value of green component * @param number $blue value of blue component * * @return Text2Image */ public function pushText2Image($text, $x, $y, $fontSize, $angle = 0, $red = 255, $green = 255, $blue = 255) { $img = $this->getImageHandle(); if (!empty($img)) { $color = imagecolorallocate($img, $red, $green, $blue); imagefttext($img, $fontSize, $angle, $x, $y, $color, $this->fontFile, $text); if ($this->imageType == IMAGETYPE_JPEG) { @imagejpeg($img, $this->fileName, 100); } else { if ($this->imageType == IMAGETYPE_PNG) { @imagepng($img, $this->fileName, 100); } } imagedestroy($img); } return $this; }
function generate_image($canvas) { global $captcha_font_path, $captcha_img_path; $txt = $this->get_random_question(); $rand = $txt[0]; $lines = explode("\n", $rand); for ($i = 0; $i < count($lines); $i++) { $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); imagefttext($canvas, 7 + rand(0, 3), rand(-5, 5), 5, 10 + $i * 10, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $lines[$i]); } imagefttext($canvas, 8, rand(-5, 5), 5, 10 + $i * 10, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $txt[1]); $nme = $this->ucaptcha->get_filename(); //imagepng($canvas,$captcha_img_path."/".$nme.".png"); imagepng($canvas); $captcha[0] = $nme; $captcha[1] = $txt[2]; return $captcha; }
/** * 生成验证码 * @param int $num:字符数 * @param int $size:大小 * @param int $width:宽度 * @param int $height:高度 */ function vCode($num = 4, $size = 20, $width = 0, $height = 0) { if (empty($width)) { $width = $num * $size * 4 / 5 + 5; } if (empty($height)) { $height = $size + 10; } //组装随机字符 // 去掉了 0 1 o O l 等 $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW"; $code = ''; for ($i = 0; $i < $num; $i++) { $code .= $str[mt_rand(0, strlen($str) - 1)]; } // 画图像 $im = imagecreatetruecolor($width, $height); // 定义要用到的颜色 $back_color = imagecolorallocate($im, 235, 236, 237); $boer_color = imagecolorallocate($im, 118, 151, 199); $text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120)); // 画背景 imagefilledrectangle($im, 0, 0, $width, $height, $back_color); // 画边框 imagerectangle($im, 0, 0, $width - 1, $height - 1, $boer_color); // 画干扰线 for ($i = 0; $i < 5; $i++) { $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagearc($im, mt_rand(-$width, $width), mt_rand(-$height, $height), mt_rand(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color); } // 画干扰点 for ($i = 0; $i < 50; $i++) { $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $font_color); } // 画验证码 imagefttext($im, $size, 0, 5, $size + 3, $text_color, 'c:\\WINDOWS\\Fonts\\simsun.ttc', $code); $_SESSION["VerifyCode"] = $code; //用session保存$code,方便验证 header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate"); header("Content-type: image/png;charset=utf8"); imagepng($im); imagedestroy($im); }
function generate_image($canvas) { global $captcha_font_path, $captcha_img_path; $rand = "X+"; $a1 = rand(0, 10); $a2 = rand(-10, 15); $rand .= $a1 . "=" . $a2; $ans = $a2 - $a1; for ($i = 0; $i < strlen($rand); $i++) { $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); imagefttext($canvas, 20, rand(-20, 20), 40 + 20 * $i, 50, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $rand[$i]); } $nme = $this->ucaptcha->get_filename(); imagepng($canvas); //imagepng($canvas,$captcha_img_path."/".$nme.".png"); $captcha[0] = $nme; $captcha[1] = $ans; return $captcha; }
function generate_image($canvas) { global $captcha_font_path, $captcha_img_path; $rand = "lim("; $a1 = rand(0, 10); $rand .= "{$a1} + (1/x))"; $ans = $a1; $hit = "x -> ∞"; for ($i = 0; $i < strlen($rand); $i++) { $color = imagecolorallocate($canvas, rand(100, 255), rand(100, 255), rand(100, 255)); imagefttext($canvas, 14, rand(-10, 10), 4 + 14 * $i, 50, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $rand[$i]); } imagefttext($canvas, 15, rand(-5, 5), 15, 75, $color, $captcha_font_path . "/LiberationMono-Bold.ttf", $hit); //$nme=$this->ucaptcha->get_filename(); imagepng($canvas); $captcha[0] = $nme; $captcha[1] = $ans; return $captcha; }