《PHP编程最快明白》第七讲:php图片验证码与缩略图

前端之家收集整理的这篇文章主要介绍了《PHP编程最快明白》第七讲:php图片验证码与缩略图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

实例22 图片验证的核心代码
<div class="codetitle"><a style="CURSOR: pointer" data="23627" class="copybut" id="copybut23627" onclick="doCopy('code23627')"> 代码如下:

<div class="codebody" id="code23627">
<?PHP
//header("content-type:image/png");
$num ='1234';
$imagewidth=60;
$imageheight=18; $numimage = imagecreate($imagewidth,$imageheight);
imagecolorallocate($numimage,240,240);
for($i=0;$i<strlen($num);$i++){
$x = mt_rand(1,8)+$imagewidth$i/4;
$y = mt_rand(1,$imageheight/4);
$color=imagecolorallocate($numimage,mt_rand(0,150),150));
imagestring($numimage,5,$x,$y,$num[$i],$color);
} for($i=0;$i<200;$i++){
$randcolor=imagecolorallocate($numimage,rand(200,255),255));
imagesetpixel($numimage,rand()%70,rand()%20,$randcolor);
}
imagepng($numimage);
imagedestroy($numimage);
?>

这个是输出4个验证码的例子,对于汉字,需要font文件和imagettftext函数,用到的时候大家再网上搜索吧。你要产生随机数,那有mt_rand函数;你还要用到session保存这个随机数;如果需要转成utf-8,需要iconv函数。 实例23 缩略图
<div class="codetitle"><a style="CURSOR: pointer" data="53317" class="copybut" id="copybut53317" onclick="doCopy('code53317')"> 代码如下:
<div class="codebody" id="code53317">
<?PHP
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,$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);
?>

猜你在找的PHP相关文章