PHP图像操作类

技术文章 2014年8月26日 4.38K

PHP图像操作类,类库中水印,英文数字验证码,缩略图功能

<?php

/*
*http://www.viphper.com
*图像操作类库
*类库中水印,英文数字验证码,缩略图功能
*
*/

class Image{
    //获取图片的详细信息
    public function getImageInfo($image){
        $info = getimagesize($image);
        if($info !== false){
            $type = strtolower(substr(image_type_to_extension($info[2]),1));
            $size = filesize($image);
            $ImageInfo = array(
                ‘type’=>$type,
                ‘size’=>$size,
                ‘width’=>$info[0],
                ‘height’=>$info[1],
                ‘mime’=>$info[‘mime’]
            );
        }
        return $ImageInfo;
    }
    //建立英文数字验证码
    public function buildImageVerify(){
        $image = imagecreate($width, $height);
        $r = Array(225, 255, 255, 223);
        $g = Array(225, 236, 237, 255);
        $b = Array(225, 236, 166, 125);
        $keys = mt_rand(0,3);
        $bg   = imagecolorallocate($image,$r[$keys],$g[$keys],$b[$keys]);//背景色
        $border = imagecolorallocate($image,100,100,100);
        $stringColor = imagecolorallocate($image, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
        for ($i = 0; $i < 25; $i++) {
        
            imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), $stringColor);
        }
        for($i=0;$i<4;$i++){
            $rand = dechex(mt_rand(0,15));
            $num.=$rand;
            imagestring($image,5,$i*10+5,mt_rand(1,8),$rand,$stringColor);
        }
        $_SESSION[‘verify’] = md5($num);
        $this->out($image,$type);
    }
    //生成缩略图
    public function thumb($image, $thumbName, $scale = 0.2){
    
        $info = $this->getImageInfo($image);
        $sWdith = $info[‘width’];
        $sHeight= $info[‘height’];
        $type   = $info[‘type’];
        $imageFun = ‘imagecreatefrom’.($type==’jpg’?’jpeg’:$type);
        $simg     = $imageFun($image);
        $thumbW   = $sWdith * $scale;
        $thumbH   = $sHeight * $scale;
        if($type !=’gif’ && function_exists(‘imagecreatetruecolor’)){
            $thumbImg = imagecreatetruecolor($thumbW,$thumbH);
        }else{
            $thumbImg = imagecreate($thumbW,$thumbH);
        }
        imagecopyresized($thumbImg,$simg,0,0,0,0,$thumbW,$thumbH,$sWdith,$sHeight);
        $outImage = “image”.($type==’jpg’?’jpeg’:$type);
        $outImage($thumbImg,$thumbName);
        imagedestroy($simg);
        imagedestroy($thumbImg);
        return $thumbName;
    }
    //生成水印
    public function water($source,$water,$alpha=60){
        if(!file_exists($source) && !file_exists($water)){
            return false;
        }
        $sInfo = $this->getImageInfo($source);
        $wInfo = $this->getImageInfo($water);
        $sType = $sInfo[‘type’];
        $wType = $wInfo[‘type’];
        $sFun  = “imagecreatefrom”.$sType;
        $sImage= $sFun($source);
        $wFun  = “imagecreatefrom”.$wType;
        $wImage= $wFun($water);
        imagealphablending($wImage, true);
        //图像位置,默认为右下角右对齐
        $posX =$sInfo[‘width’] – $wInfo[‘width’];
        $posY =$sInfo[‘height’] – $wInfo[‘height’];
        imagecopymerge($sImage,$wImage,$posX,$posY,0,0,$wInfo[‘width’],$wInfo[‘height’],$alpha);
        $ImageFun = ‘Image’ . $sInfo[‘type’];
        $ImageFun($sImage);
        imagedestroy($sImage);
        imagedestroy($wImage);
    }
    public function out($image, $type , $filename = ”){
        header(“Content-type:image/$type”);
        $imageOut = ‘image’.$type;
        if(empty($filename)){
            $imageOut($image);
        }else{
            $imageOut($image,$filename);
        }
        imagedestroy($image);
    }
}

?>

[download id=”555″ template=”image”]

 


关注微信公众号『PHP学习网

第一时间了解最新网络动态
关注博主不迷路~

PHP学习网:站内收集的部分资源来源于网络,若侵犯了您的合法权益,请联系我们删除!
分享到:
赞(0)

文章评论

您需要之后才可以评论
0点赞 0评论 收藏 QQ分享 微博分享

PHP学习网

PHP学习网