/** * +---------------------------------------------------------- * 图片上传的处理函数 * +---------------------------------------------------------- */ function create_captcha() { $word = $this->create_word(); // 把验证码字符串写入session $_SESSION['captcha'] = md5($word . DOU_SHELL); // 绘制基本框架 $im = imagecreatetruecolor($this->captcha_width, $this->captcha_height); $bg_color = imagecolorallocate($im, 235, 236, 237); imagefilledrectangle($im, 0, 0, $this->captcha_width, $this->captcha_height, $bg_color); $border_color = imagecolorallocate($im, 118, 151, 199); imagerectangle($im, 0, 0, $this->captcha_width - 1, $this->captcha_height - 1, $border_color); // 添加干扰 for ($i = 0; $i < 5; $i++) { $rand_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagearc($im, mt_rand(-$this->captcha_width, $this->captcha_width), mt_rand(-$this->captcha_height, $this->captcha_height), mt_rand(30, $this->captcha_width * 2), mt_rand(20, $this->captcha_height * 2), mt_rand(0, 360), mt_rand(0, 360), $rand_color); } for ($i = 0; $i < 50; $i++) { $rand_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagesetpixel($im, mt_rand(0, $this->captcha_width), mt_rand(0, $this->captcha_height), $rand_color); } // 生成验证码图片 $text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120)); imagestring($im, 6, 18, 5, $word, $text_color); // header header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate"); header("Content-type: image/png;charset=utf-8"); /* 绘图结束 */ imagepng($im); imagedestroy($im); return true; }
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); }
/** * Method to apply a filter to an image resource. * * @param array $options An array of options for the filter. * * @return void * * @throws InvalidArgumentException * @throws RuntimeException */ public function execute(array $options = array()) { // Verify that image filter support for PHP is available. if (!function_exists('imagefilter')) { throw new RuntimeException('The imagefilter function for PHP is not available.'); } if (empty($options)) { throw new InvalidArgumentException('No valid amount was given. Expected float.'); } $value = (int) array_shift($options); if ($value == 0) { $value = 128; } $width = imagesx($this->handle); $height = imagesy($this->handle); for ($x = 0; $x < $width; ++$x) { for ($y = 0; $y < $height; ++$y) { $index = imagecolorat($this->handle, $x, $y); $rgb = imagecolorsforindex($this->handle, $index); $r = $rgb['red']; $g = $rgb['green']; $b = $rgb['blue']; $a = $rgb['alpha']; $v = round(($r + $g + $b) / 3) >= $value ? 255 : 0; $color = imagecolorallocatealpha($this->handle, $v, $v, $v, $a); if ($color === false) { $color = imagecolorclosestalpha($this->handle, $v, $v, $v, $a); } imagesetpixel($this->handle, $x, $y, $color); } } }
function GetPartialImage($url) { $W = 150; $H = 130; $F = 80; $STEP = 1.0 / $F; $im = imagecreatefromjpeg($url); $dest = imagecreatetruecolor($W, $H); imagecopy($dest, $im, 0, 0, 35, 40, $W, $H); $a = 1; for( $y = $H - $F; $y < $H; $y++ ) { for ( $x = 0; $x < $W; $x++ ) { $i = imagecolorat($dest, $x, $y); $c = imagecolorsforindex($dest, $i); $c = imagecolorallocate($dest, a($c['red'], $a), a($c['green'], $a), a($c['blue'], $a) ); imagesetpixel($dest, $x, $y, $c); } $a -= $STEP; } header('Content-type: image/png'); imagepng($dest); imagedestroy($dest); imagedestroy($im); }
public static function ImageHue(&$image, $hue, $saturation) { $width = imagesx($image); $height = imagesy($image); for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $rgb = imagecolorat($image, $x, $y); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; $alpha = ($rgb & 0x7f000000) >> 24; $hsl = ColorUtils::rgb2hsl(array('r' => $r, 'g' => $g, 'b' => $b)); $h = $hsl['h'] / 360; $s = $hsl['s'] / 100; $h += $hue / 360; $s += $saturation / 100; if ($h > 1) { $h--; } if ($s > 1) { $s--; } $hsl['h'] = $h * 360; $hsl['s'] = $s * 100; $rgb = ColorUtils::hsl2rgb($hsl); imagesetpixel($image, $x, $y, imagecolorallocatealpha($image, $rgb['r'], $rgb['g'], $rgb['b'], $alpha)); } } }
function execute() { $gdimage =& $this->image->getImage(); $w = $this->image->getWidth(); $h = $this->image->getHeight(); $src_x = ceil($w); $src_y = ceil($h); $dst_x = $src_x; $dst_y = $src_y; // http://php.about.com/od/gdlibrary/ss/grayscale_gd.htm function yiq($r, $g, $b) { return $r * 0.299 + $g * 0.587 + $b * 0.114; } $dst_im = ImageCreateTrueColor($dst_x, $dst_y); ImageCopyResampled($dst_im, $gdimage, 0, 0, 0, 0, $dst_x, $dst_y, $src_x, $src_y); for ($c = 0; $c < 256; $c++) { $palette[$c] = imagecolorallocate($dst_im, $c, $c, $c); } for ($y = 0; $y < $src_y; $y++) { for ($x = 0; $x < $src_x; $x++) { $rgb = imagecolorat($dst_im, $x, $y); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; $gs = yiq($r, $g, $b); imagesetpixel($dst_im, $x, $y, $palette[$gs]); } } $gdimage = $dst_im; }
/** * * @brief 设置图片的干扰像素 * */ private function SetDisturbColor() { for ($i = 0; $i <= 128; $i++) { $this->mDisturbColor = imagecolorallocate($this->mCheckImage, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($this->mCheckImage, rand(2, 128), rand(2, 38), $this->mDisturbColor); } }
/** * 生成验证码 * @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 imagemergealpha($i) { //create a new image $s = imagecreatetruecolor(imagesx($i[0]), imagesy($i[1])); $back_color = imagecolorallocate($s, 0xa9, 0xb1, 0xd3); //merge all images imagealphablending($s, true); $z = $i; while ($d = each($z)) { imagecopy($s, $d[1], 0, 0, 0, 0, imagesx($d[1]), imagesy($d[1])); } //restore the transparency imagealphablending($s, false); $w = imagesx($s); $h = imagesy($s); for ($x = 0; $x < $w; $x++) { for ($y = 0; $y < $h; $y++) { $c = imagecolorat($s, $x, $y); $c = imagecolorsforindex($s, $c); $z = $i; $t = 0; while ($d = each($z)) { $ta = imagecolorat($d[1], $x, $y); $ta = imagecolorsforindex($d[1], $ta); $t += 127 - $ta['alpha']; } $t = $t > 127 ? 127 : $t; $t = 127 - $t; $c = imagecolorallocatealpha($s, $c['red'], $c['green'], $c['blue'], $t); imagesetpixel($s, $x, $y, $c); } } imagesavealpha($s, true); return $s; }
public function run($file) { $res = $this->open_image($file); if ($res != TRUE) { return FALSE; } $this->image_progressive = isset($this->settings['field_settings']['progressive_jpeg']) === TRUE && $this->settings['field_settings']['progressive_jpeg'] == 'yes' ? TRUE : FALSE; if (function_exists('imagefilter') === TRUE) { @imagefilter($this->EE->channel_images->image, IMG_FILTER_GRAYSCALE); } else { $img_width = imageSX($this->EE->channel_images->image); $img_height = imageSY($this->EE->channel_images->image); // convert to grayscale $palette = array(); for ($c = 0; $c < 256; $c++) { $palette[$c] = imagecolorallocate($this->EE->channel_images->image, $c, $c, $c); } for ($y = 0; $y < $img_height; $y++) { for ($x = 0; $x < $img_width; $x++) { $rgb = imagecolorat($this->EE->channel_images->image, $x, $y); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; $gs = $r * 0.299 + $g * 0.587 + $b * 0.114; imagesetpixel($this->EE->channel_images->image, $x, $y, $palette[$gs]); } } } $this->save_image($file); return TRUE; }
/** * Applies filter effects to the given image * * @param Image\Image $image The image to filter. * * @return Image\Image The filtered image. * * @throws FilterException if the image filter algorithm fails. */ public function applyFilter(Image\Image $image) { if ($this->level <= 0) { $gd = $image->getCore(); $width = imagesx($gd); $height = imagesy($gd); for ($x = 0; $x < $width; ++$x) { for ($y = 0; $y < $height; ++$y) { $rgba = imagecolorsforindex($gd, imagecolorat($gd, $x, $y)); $r = $rgba['red']; $g = $rgba['green']; $b = $rgba['blue']; $a = $rgba['alpha']; $level = $this->level * -1; $max = max($r, $g, $b); $avg = ($r + $g + $b) / 3; $amt = abs($max - $avg) * 2 / 255 * $level / 100; if ($r !== $max) { $r += ($max - $r) * $amt; } if ($g !== $max) { $g += ($max - $g) * $amt; } if ($b !== $max) { $b += ($max - $b) * $amt; } imagesetpixel($gd, $x, $y, imagecolorallocatealpha($gd, $r, $g, $b, $a)); } } $image->setCore($gd); } else { $image->filter(new SaturateFilter($this->level)); } return $image; }
/** * draw point on map * * @param Map $map */ public function draw(Map $map) { $image = $map->getImage(); $color = $this->_getDrawColor($image); $pointInPixels = $map->getPixelPointFromCoordinates($this->_coordinates['lon'], $this->_coordinates['lat']); imagesetpixel($image, $pointInPixels['x'], $pointInPixels['y'], $color); }
public function getVerify() { //创建画布 $img = imagecreatetruecolor($this->config['width'], $this->config['height']); //设置背景颜色 $bgColor = imagecolorallocate($img, 255, 255, 255); imagefill($img, 0, 0, $bgColor); $_x = ceil(($this->config['width'] - 20) / $this->config['lenght']); $code = ''; //写入验证码 for ($i = 0; $i < $this->config['lenght']; $i++) { $str = random(); $code .= $str; $x = 10 + $i * $_x; $fontSize = mt_rand($this->config['fontsize'] - 10, $this->config['fontsize']); $fontH = imagefontheight($this->config['fontsize']); $y = mt_rand($fontH + 10, $this->config['height'] - 5); $fontColor = imagecolorallocate($img, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200)); imagettftext($img, $fontSize, 0, $x, $y, $fontColor, $this->config['fontfile'], $str); } //增加干扰点 for ($i = 0; $i < $this->config['point']; $i++) { $pointColor = imagecolorallocate($img, rand(150, 200), rand(150, 200), rand(100, 200)); imagesetpixel($img, mt_rand(1, $this->config['width']), mt_rand(1, $this->config['height']), $pointColor); } //增加线干扰 for ($i = 0; $i < $this->config['line']; $i++) { $linColor = imagecolorallocate($img, rand(0, 200), rand(0, 200), rand(0, 200)); imageline($img, rand(0, $this->config['width']), rand(0, $this->config['height']), rand(0, $this->config['width']), rand(0, $this->config['height']), $linColor); } $_SESSION['Verify'] = md5(strtoupper($code)); header('Content-type: image/png'); imagepng($img); imagedestroy($img); }
function getCode($w, $h) { $im = imagecreate($w, $h); //imagecolorallocate($im, 14, 114, 180); // background color $red = imagecolorallocate($im, 255, 0, 0); $white = imagecolorallocate($im, 255, 255, 255); $num1 = rand(1, 20); $num2 = rand(1, 20); $_SESSION['helloweba_math'] = $num1 + $num2; $gray = imagecolorallocate($im, 118, 151, 199); $black = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //画背景 imagefilledrectangle($im, 0, 0, 100, 24, $black); //在画布上随机生成大量点,起干扰作用; for ($i = 0; $i < 80; $i++) { imagesetpixel($im, rand(0, $w), rand(0, $h), $gray); } imagestring($im, 5, 5, 4, $num1, $red); imagestring($im, 5, 30, 3, "+", $red); imagestring($im, 5, 45, 4, $num2, $red); imagestring($im, 5, 70, 3, "=", $red); imagestring($im, 5, 80, 2, "?", $white); header("Content-type: image/png"); imagepng($im); imagedestroy($im); }
/** * Applies the sepia filter to an image resource * * @param ImageResource $aResource */ public function applyFilter(ImageResource $aResource) { if ($this->offset === 0) { return; } $resource = $aResource->getResource(); $imagex = imagesx($resource); $imagey = imagesy($resource); for ($x = 0; $x < $imagex; ++$x) { for ($y = 0; $y < $imagey; ++$y) { $distx = rand($this->offset * -1, $this->offset); $disty = rand($this->offset * -1, $this->offset); if ($x + $distx >= $imagex) { continue; } if ($x + $distx < 0) { continue; } if ($y + $disty >= $imagey) { continue; } if ($y + $disty < 0) { continue; } $oldcol = imagecolorat($resource, $x, $y); $newcol = imagecolorat($resource, $x + $distx, $y + $disty); imagesetpixel($resource, $x, $y, $newcol); imagesetpixel($resource, $x + $distx, $y + $disty, $oldcol); } } }
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); }
public function index() { header("content-type:image/gif"); $this->load->library('session'); $width = 60; $height = 30; $img = imagecreatetruecolor($width, $height); $white = imagecolorallocate($img, 255, 255, 255); $color = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255)); $str = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $code = ""; for ($i = 0; $i < 5; $i++) { $code .= $str[rand(0, strlen($str) - 1)]; } $this->session->set_userdata(array('myCode' => strtolower($code))); imagefilledrectangle($img, 0, 0, 60, 30, $color); for ($i = 0; $i < 100; $i++) { imagesetpixel($img, rand(0, 60), rand(0, 30), imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255))); } for ($i = 0; $i < 10; $i++) { imageline($img, rand(0, 60), rand(0, 30), rand(0, 60), rand(0, 30), imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255))); } imagestring($img, 5, 10, 5, $code, $white); return imagegif($img); //imagedestroy($img); }
private function draw_dots() { $pixel_color = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255)); for ($i = 0; $i < $this->dots_no; $i++) { imagesetpixel($this->image, rand(0, $this->width), rand(0, $this->height), $pixel_color); } }
/** * Applies the sepia filter to an image resource * * @param ImageResource $aResource */ public function applyFilter(ImageResource $aResource) { if ($this->degrees === 0) { return; } $width = $aResource->getX(); $height = $aResource->getY(); // cache calculated colors in a map... $colorMap = array(); for ($x = 0; $x < $width; ++$x) { for ($y = 0; $y < $height; ++$y) { $color = ColorUtil::getColorAt($aResource, Coordinate::create($x, $y)); if (!isset($colorMap[$color->getColorIndex()])) { // calculate the new color $hsl = ColorUtil::rgb2hsl($color->getRed(), $color->getGreen(), $color->getBlue()); $hsl[0] += $this->degrees; $rgb = ColorUtil::hsl2rgb($hsl[0], $hsl[1], $hsl[2]); $newcol = imagecolorallocate($aResource->getResource(), $rgb[0], $rgb[1], $rgb[2]); $colorMap[$color->getColorIndex()] = $newcol; } else { $newcol = $colorMap[$color->getColorIndex()]; } imagesetpixel($aResource->getResource(), $x, $y, $newcol); } } $colorMap = null; }
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); }
public function _blend($opacity = 1, $fill = 1, $options = array()) { $opacity = min(max($opacity, 0), 1); if ($opacity === 0) { return $this->base->getImage(); } $destX = ($this->base->getWidth() - $this->top->getWidth()) / 2; $destY = ($this->base->getHeight() - $this->top->getHeight()) / 2; $w = $this->top->getWidth(); $h = $this->top->getHeight(); $baseImg = $this->base->getImage(); $overlayImg = $this->top->getImage(); for ($x = 0; $x < $w; ++$x) { for ($y = 0; $y < $h; ++$y) { // First get the colors for the base and top pixels. $baseColor = $this->normalisePixel($this->getColorAtPixel($baseImg, $x + $destX, $y + $destY, $this->base->getIsTrueColor())); $topColor = $this->normalisePixel($this->getColorAtPixel($overlayImg, $x, $y, $this->top->getIsTrueColor())); $destColor = $baseColor; foreach ($destColor as $key => &$color) { if ($color > 0.5) { $color = min($topColor[$key] + $color, 1); } else { $color = max($topColor[$key] + $color - 1, 0); } } if ($opacity !== 1) { $destColor = $this->opacityPixel($baseColor, $destColor, $opacity); } $destColor = $this->integerPixel($this->deNormalisePixel($destColor)); // Now that we have a valid color index, set the pixel to that color. imagesetpixel($baseImg, $x + $destX, $y + $destY, $this->getColorIndex($baseImg, $destColor)); } } return $baseImg; }
function create_image() { $image = imagecreatetruecolor(200, 50); $background_color = imagecolorallocate($image, 255, 255, 255); imagefilledrectangle($image, 0, 0, 200, 50, $background_color); $line_color = imagecolorallocate($image, 255, 0, 255); for ($i = 0; $i < 10; $i++) { imageline($image, 0, rand() % 50, 200, rand() % 25, $line_color); } $pixel_color = imagecolorallocate($image, 255, 0, 255); for ($i = 0; $i < 1000; $i++) { imagesetpixel($image, rand() % 200, rand() % 50, $pixel_color); } $letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; $len = strlen($letters); $letter = $letters[rand(0, $len - 1)]; $text_color = imagecolorallocate($image, 0, 0, 0); $word = ""; for ($i = 0; $i < 6; $i++) { $letter = $letters[rand(0, $len - 1)]; imagestring($image, 5, 5 + $i * 30, 20, $letter, $text_color); $word .= $letter; } $_SESSION['security_key'] = $word; imagepng($image); }
static function codeimage() { $image = imagecreatetruecolor(100, 30); $bgcolor = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $bgcolor); $code = ''; for ($i = 0; $i < 4; $i++) { $fontsize = 6; $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120)); $data = "abcdefghjkmnpqrstuvwxy3456789"; $fontcontent = substr($data, rand(1, strlen($data) - 1), 1); $code .= $fontcontent; $x = $i * 100 / 4 + rand(5, 10); $y = rand(5, 10); imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } for ($i = 0; $i < 200; $i++) { $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200)); imagesetpixel($image, rand(1, 99), rand(1, 29), $pointcolor); } for ($i = 0; $i < 2; $i++) { $linecolor = imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220)); imageline($image, rand(1, 99), rand(1, 29), rand(1, 99), rand(1, 29), $linecolor); } $return['code'] = $code; $return['image'] = $image; return $return; // header('content-type:image/png'); // imagepng($image); }
/** * Apply the transform to the sfImage object. * * @param sfImage * @return sfImage */ protected function transform(sfImage $image) { $resource = $image->getAdapter()->getHolder(); $resourcex = imagesx($resource); $resourcey = imagesy($resource); for ($x = 0; $x < $resourcex; ++$x) { for ($y = 0; $y < $resourcey; ++$y) { $distx = rand(-$this->scatter_factor, $this->scatter_factor); $disty = rand(-$this->scatter_factor, $this->scatter_factor); // keep inside the image boundaries if ($x + $distx >= $resourcex) { continue; } if ($x + $distx < 0) { continue; } if ($y + $disty >= $resourcey) { continue; } if ($y + $disty < 0) { continue; } $oldcol = imagecolorat($resource, $x, $y); $newcol = imagecolorat($resource, $x + $distx, $y + $disty); imagesetpixel($resource, $x, $y, $newcol); imagesetpixel($resource, $x + $distx, $y + $disty, $oldcol); } } return $image; }
/** * Find pixels coords and draw these on the current image * * @param integer $image Number of the image (to be used with $this->height) * @return boolean Success **/ function drawPixels($image) { $limit = 0; do { /** Select with limit */ $result = mysql_query(sprintf($this->query, $image * $this->height, ($image + 1) * $this->height - 1) . ' LIMIT ' . $limit . ',' . $this->limit); if ($result === false) { return $this->raiseError('Query failed: ' . mysql_error()); } $count = mysql_num_rows($result); while ($click = mysql_fetch_row($result)) { $x = (int) $click[0]; $y = (int) ($click[1] - $image * $this->height); if ($x < 0 || $x >= $this->width) { continue; } /** Apply a calculus for the step, with increases the speed of rendering : step = 3, then pixel is drawn at x = 2 (center of a 3x3 square) */ $x -= $x % $this->step - $this->startStep; $y -= $y % $this->step - $this->startStep; /** Add 1 to the current color of this pixel (color which represents the sum of clicks on this pixel) */ $color = imagecolorat($this->image, $x, $y) + 1; imagesetpixel($this->image, $x, $y, $color); $this->maxClicks = max($this->maxClicks, $color); if ($image === 0) { /** Looking for the maximum height of click */ $this->maxY = max($y, $this->maxY); } } /** Free resultset */ mysql_free_result($result); $limit += $this->limit; } while ($count === $this->limit); return true; }
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 getCode($num, $w, $h) { // 去掉了 0 1 O l 等 $str = "23456789abcdefghijkmnpqrstuvwxyz"; $code = ''; for ($i = 0; $i < $num; $i++) { $code .= $str[mt_rand(0, strlen($str) - 1)]; } //将生成的验证码写入session,备验证页面使用 $_SESSION["my_checkcode"] = $code; //创建图片,定义颜色值 Header("Content-type: image/PNG"); $im = imagecreate($w, $h); $black = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120)); $gray = imagecolorallocate($im, 118, 151, 199); $bgcolor = imagecolorallocate($im, 235, 236, 237); //画背景 imagefilledrectangle($im, 0, 0, $w, $h, $bgcolor); //画边框 imagerectangle($im, 0, 0, $w - 1, $h - 1, $gray); //imagefill($im, 0, 0, $bgcolor); //在画布上随机生成大量点,起干扰作用; for ($i = 0; $i < 80; $i++) { imagesetpixel($im, rand(0, $w), rand(0, $h), $black); } //将字符随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成 $strx = rand(5, 10); for ($i = 0; $i < $num; $i++) { $strpos = rand(1, 6); imagestring($im, 20, $strx, $strpos, substr($code, $i, 1), $black); $strx += $w / 5; } imagepng($im); imagedestroy($im); }
function imgcode($nums, $width, $high) { //去除了數字0和1 字母小寫O和L,為了避免辨識不清楚 //$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMOPQRSTUBWXYZ"; $str = "0123456789"; $code = ''; for ($i = 0; $i < $nums; $i++) { $code .= $str[mt_rand(0, strlen($str) - 1)]; } $_SESSION['captcha'] = $code; //建立圖示,設置寬度及高度與顏色等等條件 $image = imagecreate($width, $high); $black = imagecolorallocate($image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200)); $border_color = imagecolorallocate($image, 21, 106, 235); $background_color = imagecolorallocate($image, 235, 236, 237); //建立圖示背景 imagefilledrectangle($image, 0, 0, $width, $high, $background_color); //建立圖示邊框 imagerectangle($image, 0, 0, $width - 1, $high - 1, $border_color); //在圖示布上隨機產生大量躁點 for ($i = 0; $i < 80; $i++) { imagesetpixel($image, rand(0, $width), rand(0, $high), $black); } $strx = rand(3, 8); for ($i = 0; $i < $nums; $i++) { $strpos = rand(1, 6); imagestring($image, 5, $strx, $strpos, substr($code, $i, 1), $black); $strx += rand(10, 30); } imagepng($image); imagedestroy($image); }
/** * Returns a mask * * @param \WideImage\Image $image * @return \WideImage\Image */ public function execute($image) { $width = $image->getWidth(); $height = $image->getHeight(); $mask = TrueColorImage::create($width, $height); $mask->setTransparentColor(-1); $mask->alphaBlending(false); $mask->saveAlpha(false); for ($i = 0; $i <= 255; $i++) { $greyscale[$i] = ImageColorAllocate($mask->getHandle(), $i, $i, $i); } imagefilledrectangle($mask->getHandle(), 0, 0, $width, $height, $greyscale[255]); $transparentColor = $image->getTransparentColor(); $alphaToGreyRatio = 255 / 127; for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $color = $image->getColorAt($x, $y); if ($color == $transparentColor) { $rgba['alpha'] = 127; } else { $rgba = $image->getColorRGB($color); } imagesetpixel($mask->getHandle(), $x, $y, $greyscale[255 - round($rgba['alpha'] * $alphaToGreyRatio)]); } } return $mask; }
function create_captcha($width = 60, $height = 32) { session_start(); //生成验证码图片 Header("Content-type: image/PNG"); $im = imagecreate($width, $height); // width and height of image $back = ImageColorAllocate($im, 245, 245, 245); // specify background color imagefill($im, 0, 0, $back); // fill the background color into image $vcodes = ""; srand((double) microtime() * 1000000); //生成4位数字 for ($i = 0; $i < 4; $i++) { $font = ImageColorAllocate($im, rand(100, 255), rand(0, 100), rand(100, 255)); // 生成随机颜色 $authnum = rand(1, 9); $vcodes .= $authnum; imagestring($im, 5, 2 + $i * 10, 1, $authnum, $font); } for ($i = 0; $i < 100; $i++) { // interuppting $randcolor = ImageColorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($im, rand() % 70, rand() % 30, $randcolor); // 画像素点函数 } ImagePNG($im); ImageDestroy($im); $_SESSION['captcha'] = $vcodes; }