Exemplo n.º 1
0
 /**
  * 生成验证码字符串,写入SESSION,将字符串图片返回给浏览器
  *
  * @param     $len
  * @param int $width
  * @param int $height
  * @param int $font_size
  */
 public static function generate($len, $width = 110, $height = 30, $font_size = 18)
 {
     $sizes = array('18' => array('width' => 25, 'height' => 25));
     $words = self::words($len);
     $session_key = 'captcha';
     //$_SESSION[$session_key] = strtolower($words);
     Tools_help::setSession($session_key, strtolower($words));
     $image = Files_ImageManager::createWhiteImage($width, $height);
     $font_config = array('spacing' => -17, 'font' => '5.ttf');
     $font_path = dirname(__FILE__) . '/' . $font_config['font'];
     $color = imagecolorallocate($image, mt_rand(0, 100), mt_rand(20, 120), mt_rand(50, 150));
     $rand = 0;
     $w = $sizes[$font_size]['width'] * $len;
     $h = $sizes[$font_size]['height'];
     $x = round(($width - $w) / 2);
     $y = round(($height + $h) / 2) - 6;
     $coors = imagettftext($image, $font_size, $rand, $x, $y, $color, $font_path, $words);
     if ($coors) {
         header("Cache-Control: no-cache, must-revalidate");
         header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
         header("Pragma: no-cache");
         header("Cache-control: private");
         header('Content-Type: image/png');
         imagepng($image);
         imagedestroy($image);
     }
     exit;
 }
Exemplo n.º 2
0
 /**
  * 剪切图片
  * @param $src_file
  * @param $dst_file
  * @param null $dst_width
  * @param null $dst_height
  * @param string $file_type
  * @param int $dst_x
  * @param int $dst_y
  * @return bool
  */
 public static function cut($src_file, $dst_file, $dst_width = null, $dst_height = null, $file_type = 'jpg', $dst_x = 0, $dst_y = 0)
 {
     if (!file_exists($src_file)) {
         return false;
     }
     // Source information
     $src_info = getimagesize($src_file);
     $src = array('width' => $src_info[0], 'height' => $src_info[1], 'ressource' => Files_ImageManager::create($src_info[2], $src_file));
     // Destination information
     $dest = array();
     $dest['x'] = $dst_x;
     $dest['y'] = $dst_y;
     $dest['width'] = !is_null($dst_width) ? $dst_width : $src['width'];
     $dest['height'] = !is_null($dst_height) ? $dst_height : $src['height'];
     $dest['ressource'] = Files_ImageManager::createWhiteImage($dest['width'], $dest['height']);
     $white = imagecolorallocate($dest['ressource'], 255, 255, 255);
     imagecopyresampled($dest['ressource'], $src['ressource'], 0, 0, $dest['x'], $dest['y'], $dest['width'], $dest['height'], $dest['width'], $dest['height']);
     imagecolortransparent($dest['ressource'], $white);
     $return = Files_ImageManager::write($file_type, $dest['ressource'], $dst_file);
     return $return;
 }