Exemple #1
0
 /**
  * 生成验证码字符串,写入SESSION,将字符串图片返回给浏览器
  *
  * @param     $len
  * @param int $width
  * @param int $height
  * @param int $font_size
  */
 public static function generate($len, $width = 108, $height = 30, $font_size = 18)
 {
     $sizes = array('18' => array('width' => 25, 'height' => 25));
     $words = self::words($len);
     session_start();
     $session_key = 'captcha';
     $_SESSION[$session_key] = strtolower($words);
     $image = ImageManager::createWhiteImage($width, $height);
     $font_config = array('spacing' => -17, 'font' => '5.ttf');
     $font_path = dirname(__FILE__) . '/font/' . $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');
         ob_clean();
         imagepng($image);
         imagedestroy($image);
     }
     exit;
 }
/**
 * @deprecated 1.5.0
 */
function createDestImage($width, $height)
{
    Tools::displayAsDeprecated();
    return ImageManager::createWhiteImage($width, $height);
}
 /**
  * Cut image
  *
  * @param array $src_file Origin filename
  * @param string $dst_file Destination filename
  * @param integer $dst_width Desired width
  * @param integer $dst_height Desired height
  * @param string $file_type
  * @param int $dst_x
  * @param int $dst_y
  *
  * @return bool Operation result
  */
 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' => 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'] = 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 = ImageManager::write($file_type, $dest['ressource'], $dst_file);
     return $return;
 }