好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

PHP图像操作类

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

<?php

/*
*http://HdhCmsTestviphper测试数据
*图像操作类库
*类库中水印,英文数字验证码,缩略图功能
*
*/

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);
?? ?}
}

?>

class="paydown">  

查看更多关于PHP图像操作类的详细内容...

  阅读:1489次