php生成验证码,缩略图及水印图的类分享

这里分享三个PHP图片处理类:

1. 验证码类

class VerifyCode {
    private $charset = 'abcdefghjkmnpqrstuvwxy23456789'; 
    private $code;
    private $codelen = 4;
    private $width = 150;
    private $height = 50;
    private $img;

    public function __construct() {
        session_start();
        $this->code = '';
        for ($i = 0; $i < $this->codelen; $i++) {
            $this->code .= $this->charset[rand(0, strlen($this->charset) - 1)];
        }
        $_SESSION['verify_code'] = $this->code;
    }

    public function doimg() {
        $this->img = imagecreatetruecolor($this->width, $this->height);
        $color = imagecolorallocate($this->img, rand(150, 255), rand(150, 255), rand(150, 255));
        imagefilledrectangle($this->img, 0, 0, $this->width, $this->height, $color); 

        for ($i = 0; $i < 100; $i++) {
            $color = imagecolorallocate($this->img, rand(50, 200), rand(50, 200), rand(50, 200));
            imagesetpixel($this->img, rand(1, $this->width), rand(1, $this->height), $color); 
        }

        for ($i = 0; $i < strlen($this->code); $i++) {
            $fontcolor = imagecolorallocate($this->img, rand(0, 100), rand(0, 100), rand(0, 100));
            $x = 10 + $i * ($this->width / 4);
            imagestring($this->img, 5, $x, rand(1, 8), $this->code[$i], $fontcolor);
        }
        header('content-type:image/png');
        imagepng($this->img);
        imagedestroy($this->img);
    }
}

该类可以生成一个图片验证码,并将验证码保存在SESSION中。

2. 缩略图类

class Thumbnail {
    private $src;
    private $dist;
    private $width; 
    private $height;

    public function __construct($src, $dist, $width, $height) {
        $this->src = $src;
        $this->dist = $dist;
        $this->width = $width;
        $this->height = $height;
    }

    public function doimg() {
        list($src_w, $src_h, $src_type) = getimagesize($this->src);
        switch ($src_type) {
            case 1: $src_img = imagecreatefromgif($this->src); break;
            case 2: $src_img = imagecreatefromjpeg($this->src); break;
            case 3: $src_img = imagecreatefrompng($this->src); break;
        }
        $scale = min($this->width / $src_w, $this->height / $src_h);
        $dist_w = $src_w * $scale;
        $dist_h = $src_h * $scale;
        $dist_img = imagecreatetruecolor($dist_w, $dist_h);
        imagecopyresampled($dist_img, $src_img, 0, 0, 0, 0, $dist_w, $dist_h, $src_w, $src_h);
        switch ($src_type) {
            case 1: imagegif($dist_img, $this->dist); break;
            case 2: imagejpeg($dist_img, $this->dist); break;
            case 3: imagepng($dist_img, $this->dist); break;
        }
        imagedestroy($src_img);
        imagedestroy($dist_img);
    }
}  

该类可以根据给定的宽高生成图像缩略图

3. 水印类

class Watermark {
private $src;
private $dist;
private $mark;
private $pos;
private $alpha;    public function __construct($src, $dist, $mark, $pos = 9, $alpha = 50) {
$this->src = $src;
$this->dist = $dist;
$this->mark = $mark;
$this->pos = $pos;
$this->alpha = $alpha;
}    public function doimg() {
list($src_w, $src_h, $src_type) = getimagesize($this->src);
        switch ($src_type) {
            case 1: $src_img = imagecreatefromgif($this->src); break;
            case 2: $src_img = imagecreatefromjpeg($this->src); break;
            case 3: $src_img = imagecreatefrompng($this->src); break;
        }  

        list($mark_w, $mark_h, $mark_type) = getimagesize($this->mark);
        switch ($mark_type) {
            case 1: $mark_img = imagecreatefromgif($this->mark); break;
            case 2: $mark_img = imagecreatefromjpeg($this->mark); break;
            case 3: $mark_img = imagecreatefrompng($this->mark); break;
        } 

        $dist_img = imagecreatetruecolor($src_w, $src_h);
        imagecopy($dist_img, $src_img, 0, 0, 0, 0, $src_w, $src_h);
        $mark_x = $src_w - $mark_w;
        $mark_y = $src_h - $mark_h;
        imagecopymerge($dist_img, $mark_img, $mark_x, $mark_y, 0, 0, $mark_w, $mark_h, $this->alpha);
        imagepng($dist_img, $this->dist);
        imagedestroy($src_img);
        imagedestroy($dist_img);
        imagedestroy($mark_img);
    }
}

该水印类可以给图像添加水印,支持GIF,JPG和PNG格式,并可以设置水印位置和透明度。

这三个PHP图像处理类,可以实现验证码、缩略图和添加水印等功能,对PHP图像应用开发很有帮助。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发

请登录后发表评论