本文章提供一款php教程中文汉字验证码生成程序,如果在图片片生成汉字,需要font文件和imagettftext函数,用到的时候大家再网上搜索吧。你要产生随机数,那有mt_rand函数;你还要用到session保存这个随机数;如果需要转成utf-8,需要iconv函数。
class simpleimage {
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == imagetype_jpeg ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == imagetype_gif ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == imagetype_png ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=imagetype_jpeg, $compression=75, $permissions=null) {
if( $image_type == imagetype_jpeg ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == imagetype_gif ) {
imagegif($this->image,$filename);
} elseif( $image_type == imagetype_png ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=imagetype_jpeg) {
if( $image_type == imagetype_jpeg ) {
imagejpeg($this->image);
} elseif( $image_type == imagetype_gif ) {
imagegif($this->image);
} elseif( $image_type == imagetype_png ) {
imagepng($this->image);
}
}
function getwidth() {
return imagesx($this->image);
}
function getheight() {
return imagesy($this->image);
}
function resizetoheight($height) {
$ratio = $height / $this->getheight();
$width = $this->getwidth() * $ratio;
$this->resize($width,$height);
}
function resizetowidth($width) {
$ratio = $width / $this->getwidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getwidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getwidth(), $this->getheight());
$this->image = $new_image;
}
}$newfile = upload_dir."/icons/".md5($_session['user']->email).".jpg";//上传文件保存的目录
$image = new simpleimage();
$image->load($_files['icons']['tmp_name']);//上传的临时文件名
$image->resizetowidth(80);设置宽度
$image->save($newfile);
?>