|
- <p><?php
- </p><p>class image{
- </p><p>private $res; //图像资源
- </p><p>private $srcpath;
- private $srcwidth;
-
- private $srchight;
-
- private $srctype;
- </p><p>private $logpath;
-
- private $logwidth;
-
- private $loghight;
-
- private $logtype;
- </p><p>//初始化得到图像信息
-
- public function __construct($srcpath,$logpath){
-
- //判断是否为文件
-
- if(!is_file($srcpath) || !is_file($logpath)){
-
- die('文件不存在');
-
- }
- </p><p>//获取图像地址
-
- $this->srcpath = $srcpath;
-
- $this->logpath = $logpath;
- </p><p>//取得目标图像大小,类型1 = GIF,2 = JPG,3 = PNG 6 = BMP
-
- list($sw,$sh,$stype) = getimagesize($srcpath);
-
- $this->srcwidth = $sw;
-
- $this->srchigth = $sh;
-
- $this->srctype = $stype;
- </p><p>//取得水印图像大小,类型
-
- list($lw,$lh,$ltype) = getimagesize($logpath);
-
- $this->logwidth = $lw;
-
- $this->loghight = $lh;
-
- $this->logtype = $ltype;
- </p><p>}
- </p><p>
- /************************************创建图像资源********************************/
-
- public function createimage(){
- </p><p>//图像地址关联图像类型
-
- $imgarr = array_combine( array($this->srcpath,$this->logpath),array($this->srctype,$this->logtype) );
- $res = array();
- </p><p>//创建对应类型的图像资源
-
- foreach($imgarr as $key=>$v){
-
- switch($v){
-
- case 1:
-
- $res[] = imagecreatefromgif($key);
- break;
-
- case 2:
-
- $res[] = imagecreatefromjpeg($key);
-
- break;
-
- case 3:
-
- $res[] = imagecreatefrompng($key);
- break;
-
- case 6:
-
- $res[] = imagecreatefromwbmp($key);
- break;
-
- default:
-
- return false;
-
- }
-
- }
- </p><p>//图像资源赋给属性
-
- $this->res = $res;
- </p><p>}
- </p><p>
- /************************************给图片加水印********************************/
-
- //$dx,$dy水印放置目标图片坐标
-
- //$sx,$sy水印开始部分坐标
-
- //$sw,$sh水印结束部分坐标
-
- //$pct水印透明度(100不透明)
-
- public function shuiyin($dx,$dy,$sx,$sy,$sw,$sh,$pct){
-
- $dstres = $this->res[0];
-
- $logres = $this->res[1]; </p><p>imagecopymergegray($dstres,$logres,$dx,$dy,$sx,$sy,$sw,$sh,$pct);
-
- return $dstres;
-
- }
- </p><p>
- /************************************等比例缩放图片********************************/
-
- //$res需缩小的图片资源
-
- //$width,$higth缩小尺寸
- </p><p>public function small($logres,$imw,$imh){
-
- $img = imagecreatetruecolor($imw,$imh); //创建画布
-
- $imgcolor = imagecolorallocate($img,100,100,100);
-
- imagefill($img,0,0,$imgcolor);
- </p><p>//判断图像来源,取得图像大小
-
- if(is_file($logres)){
-
- list($width,$hight,$type) = getimagesize($logres);
- switch($type){
-
- case 1:
-
- $src = imagecreatefromgif($logres);
- break;
-
- case 2:
-
- $src = imagecreatefromjpeg($logres);
-
- break;
-
- case 3:
-
- $src = imagecreatefrompng($logres);
- break;
-
- case 6:
-
- $src = imagecreatefromwbmp($logres);
- break;
-
- default:
-
- return false;
-
- }
- }
-
- else{
-
- $src = $logres;
-
- $width = imagesx($src);
-
- $hight = imagesy($src);
-
- }
- </p><p>//判断图像大小,进行缩放
-
- if($width==$imw && $hight==$imh){
-
- imagecopyresampled($img,$src,0,0,0,0,$imw,$imh,$width,$hight);
-
- }
-
- elseif($width>$hight){
-
- $bili = $imw/$width;
-
- $nw = $bili*$width;
-
- $nh = $imh*($hight/$width);
-
- imagecopyresampled($img,$src,0,($imh-$nh)/2,0,0,$nw,$nh,$width,$hight); //宽>高
-
- }else{
-
- $bili = $imh/$hight;
-
- $nw = $imw*($width/$hight);
-
- $nh = $bili*$hight;
-
- imagecopyresampled($img,$src,($imw-$nw)/2,0,0,0,$nw,$nh,$width,$hight); //高>宽
-
- }
- </p><p>//返回图像资源
-
- return $img;
-
- }
- </p><p>}
- </p><p> </p>
复制代码 |
|