对图片进行水印添加以及生成缩率图

千芳同学_3365

千芳同学_3365

2016-05-23

1507人浏览

原创

2个方法
1 给图片进行水印添加
2生成一个新的缩率图

php代码

<?php  
    class Image{
        //水印配置项
        private $waterOn;
        private $waterImg;
        private $waterPos;
        private $waterPct;
        private $waterText;
        private $waterFont;
        private $waterTextSize;
        private $waterTextColor;
        private $qua;
        //缩略图配置项
        private $thumbWidth;
        private $thumbHeight;
        private $thumbType;
        private $thumbEndfix;
        //构造函数
        public function __construct(){
            $this->waterOn=C("WATER_ON");
            $this->waterImg=C("WATER_IMG");
            $this->waterPos=C("WATER_POS");
            $this->waterPct=C("WATER_PCT");
            $this->waterText=C("WATER_TEXT");
            $this->waterFont=C("WATER_FONT");
            $this->waterTextSize=C("WATER_TEXT_SIZE");
            $this->waterTextColor=C("WATER_TEXT_COLOR");
            $this->qua=C("WATER_QUA");
            //缩率图
            $this->thumbWidth=C("THUMB_WIDTH");
            $this->thumbHeight=C("THUMB_HEIGHT");
            $this->thumbType=C("THUMB_TYPE");
            $this->thumbEndFix=C("THUMB_ENDFIX");
            }
        /*
        *验证图片是否合法
        */ 
        private function check($img){
            return is_file($img)&&getimagesize($img)&&extension_loaded("gd");
            }
        /*
        *缩率图
        *@param string  $img         原图
        *@param string  $outFile     缩率之后存储的图片
        *@param int     $thumbWidth  缩率图宽度
        *@param int     $thumbHeight 缩率图高度
        *@param int     $thumbType   那种方式进行缩略处理
        */ 
        public function thumb($img,$outFile="",$thumbWidth="",$thumbHeight="",$thumbType=""){
            if(!$this->check($img)){
                return false;
                }
            //缩率图处理方式   
            $thumbType=$thumbType?$thumbType:$this->thumbType;
            //缩率图宽度
            $thumbWidth=$thumbWidth?$thumbWidth:$this->thumbWidth;   
            //缩率图高度
            $thumbHeight=$thumbHeight?$thumbHeight:$this->thumbHeight;
            //获取原图信息
            $imgInfo=getimagesize($img);
            //原图宽度
            $imgWidth=$imgInfo[0];
            //原图高度
            $imgHeight=$imgInfo[1];
            //获得原图类型
            $imgtype=image_type_to_extension($imgInfo[2]);
            //根据不同的缩略处理方式,获得尺寸(原图和缩略图相应的尺寸)
            $thumb_size=$this->thumbsize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType);
            //创建原图
            $func="imagecreatefrom".substr($imgtype,1);//变量函数
            $resImg=$func($img);
            //创建缩率图画布
            if($imgtype==".gif"){
                $res_thumb=imagecreate($thumb_size[2],$thumb_size[3]);
                }else{
                    $res_thumb=imagecreatetruecolor($thumb_size[2],$thumb_size[3]);
                    }
            imagecopyresized($res_thumb,$resImg,0,0,0,0,$thumb_size[2],$thumb_size[3],$thumb_size[0],$thumb_size[1]);
            $fileInfo=pathinfo($img);//文件信息
            $outFile=$outFile?$outFile:$fileInfo['filename'].$this->thumbEndFix.$fileInfo['extension'];//文件名称
            $outFile=$fileInfo["dirname"]."/".$outFile;//加上目录
            $func="image".substr($imgtype,1);
            $func($res_thumb,$outFile); 
            return $outFile;
            }   
        private function thumbSize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType){
            //缩率图尺寸
            $w=$thumbWidth;
            $h=$thumbHeight;
            //原图尺寸
            $img_w=$imgWidth;
            $img_h=$imgHeight;
            switch($thumbType){
                case 1:
                    //宽度固定,高度自增
                    $h=$w/$imgWidth*$imgHeight;
                    break;
                case 2://高度固定,宽度自   
                    $w=$h/$imgHeight*$imgWidth;
                    break;
                case 3:
                    if($imgHeight/$thumbHeight>$imgWidth/$thumbWidth){
                        $img_h=$imgWidth/$thumbWidth*$thumbHeight;
                        }else{
                            $img_w=$imgHeight/$thumbHeight*$thumbWidth;
                            }
                }
                return array($img_w,$img_h,$w,$h);
            }
        /*
        *@param string  $img     原图
        *@param string  $outImg  加完水印后生成的图
        *@param int     $pos     水印位置
        *@param int     $pct     透明度
        *@param text    $text    水印文字
        *@param string  $waterImg水印图片       
        */ 
        public function water($img,$outImg=null,$pos="",$pct="",$text="",$waterImg="",$textColor=""){
            if(!$this->check($img)){
                return false;
                }
            //加完水印后生成的图
            $outImg=$outImg?$outImg:$img;
            //水印位置
            $pos=$pos?$pos:$this->waterPos;
            //透明度
            $pct=$pct?$pct:$this->waterPct;
            //水印文字
            $text=$text?$text:$this->waterText;
            //水印图片
            $waterImg=$waterImg?$waterImg:$this->waterImg;
            //验证水印图片
            $waterImgOn=$this->check($waterImg); 
            //水印文字颜色
            $textColor=$textColor?$textColor:$this->waterTextColor;
            //原图信息
            $imgInfo=getimagesize($img);
            //原图宽度
            $imgWidth=$imgInfo[0];
            //原图高度
            $imgHeight=$imgInfo[1];
            switch($imgInfo[2]){
                case 1:
                    $resImg=imagecreatefromgif($img);
                    break;
                case 2:
                    $resImg=imagecreatefromjpeg($img);
                    break;
                case 3:
                    $resImg=imagecreatefrompng($img);
                    break;      
                }
            if($waterImgOn){//水印图片有效
                //水印信息
                $waterInfo=getimagesize($waterImg);
                //水印宽度
                $waterWidth=$waterInfo[0];
                //水印高度
                $waterHeight=$waterInfo[1];
                //根据不同的情况创建不同的类型 gif jpeg png
                $w_img=null;
                switch($waterInfo[2]){
                    case 1:
                        $w_img=imagecreatefromgif($waterImg);
                        break;
                    case 2:
                        $w_img=imagecreatefromjpeg($waterImg);
                        break;
                    case 3:
                        $w_img=imagecreatefrompng($waterImg);       
                    }
                }else{//水印图片失效,使用文字水印
                    if(empty($text)||strlen($textColor)!==7){
                        return false;
                        }
                    //获得文字水印盒子信息
                    $textInfo=imagettfbbox($this->waterTextSize,0,$this->waterFont,$text);
                    //文字信息宽度
                    $textWidth=$textInfo[2]-$textInfo[6];
                    //文字信息高度    
                    $textHeight=$textInfo[3]-$textInfo[7];
                    }
                //水印位置
                $x=$y=20;
                switch($pos){
                    case 1:
                        break;
                    case 2:
                        $x=($imgWidth-$waterWidth)/2;
                        break;
                    case 3:
                        $y=$imgWidth-$waterWidth-10;
                        break;
                    case 4:
                        $x=($imgHeight-$waterHeight)/2;
                        break;
                    case 5:
                        $x=($imgWidth-$waterWidth)/2;
                        $y=($imgHeight-$waterHeight)/2; 
                        break;
                    case 6:
                        $x=$imgWidth-$waterWidth-10;
                        $y=($imgHeight-$waterHeight)/2;
                        break;
                    case 7:
                        $x=$imgHeight-$waterHeight-10;
                        break;
                    case 8:
                        $x=($imgWidth-$waterWidth)/2;
                        $y=$imgHeight-$waterHeight-10;
                        break;
                    case 9:
                        $x=$imgWidth-$waterWidth-10;
                        $y=$imgHeight-$waterHeight-10;      
                        break;
                    default:
                        $x=mt_rand(20,$imgWidth-$waterWidth);
                        $y=mt_rand(20,$imgHeight-$waterHeight);         
                    }   
                if($waterImgOn){//当水印图片有效时,以图片形式加水印
                    if($waterInfo[2]==3){
                        imagecopy($resImg,$w_img,$x,$y,0,0,$waterWidth,$waterHeight);
                        }else{
                            imagecopymerge($resImg,$w_img,$x,$y,0,0,$waterInfo,$waterHeight,$pct);
                            }
                    }else{//水印图片失效,以文字水印加
                        $red=hexdec(substr($this->waterTextColor,1,2));
                        $greem=hexdec(substr($this->waterTextColor,3,2));
                        $blue=hexdec(substr($this->waterTextColor,5,2));
                        $color=imagecolorallocate($resImg,$red,$greem,$blue);
                        imagettftext($resImg,$this->waterTextSize,0,$x,$y,$color,$this->waterFont,$text);
                        }
                //输出图片      
                switch($imgInfo[2]){
                        case 1:
                            imagegif($resImg,$outImg);
                            break;
                        case 2:
                            imagejpeg($resImg,$outImg);
                            break;
                        case 3:
                            imagepng($resImg,$outImg);
                            break;      
                    }   
                //垃圾回收
                if(isset($resImg)){
                    imagedestroy($resImg);
                    }
                if(isset($w_img)){
                    imagedestroy($w_img);
                    }   
                return true;                
            }   
        }
?>
<?php  
return array(
    //水印处理
    "WATER_ON"=>1,//水印开关
    "WATER_IMG"=>"./data/logo.png",//水印图片
    "WATER_POS"=>9,//水印位置
    "WATER_PCT"=>80,//水印透明度
    "WATER_TEXT"=>"http://www.caoxiaobin.cn",
    "WATER_FONT"=>"./data/simsunb.ttf",//水印字体
    "WATER_TEXT_COLOR"=>"#333333",//文字颜色 16进制表示
    "WATER_TEXT_SIZE"=>16,//文字大小
    "WATER_QUA"=>80,//图片压缩比
    //缩略图
    "THUMB_WIDTH"=>150,//缩率图宽度
    "THUMB_HEIGHT"=>150,//缩略图高度
    "THUMB_TYPE"=>1,//缩略图处理  1宽度固定,高度自增 2高度固定,宽度自增 //缩略图尺寸不变,对原图进行裁切
    "THUMB_ENDFIX"=>"_thmub"//缩略图后缀
     
);
?>
/*
 * 不区分大小写的数据键检测
 */
function array_key_exists_d($key,$arr){
    $_key=strtolower($key);
    foreach ($arr as $k=>$v){
        if($_key==strtolower($k)){
            return true;
        }
    }
}
/*
 * 递归更改数组的KEY(键名)
 * @param array;
 * @stat int 0小写 1大写
 */
function array_change_key_case_d($arr,$stat=0){
    $func=$stat?"strtoupper":"strtolower";
    $_newArr=array();
    if(!is_array($arr)||empty($arr)){
        return $_newArr;
    }
    foreach($arr as $k=>$v){
        $_k=$func($k);//通过变量函数转换KEY大小写
        $_newArr[$_k]= is_array($v)?array_change_key_case_d($v):$v;
    }
    return $_newArr;
}
/*
 * 读取与设置配置项
 * @param $name void 配置项名称,如果不填写返回所有配置项
 * @param $value void 配置项的值
 * @param $value 值 false null 只取$name值
 */
function C($name=null,$value=null){
    static $config=array();//静态变量$config存储所有配置项
    if(is_null($name)){
        return $config;
    }
    //如果$name为数组
    if(is_array($name)){
        return $config=array_merge($config,array_change_key_case_d($name,1));
    }
    //$name为字符串 2种情况 $value无值表示获得配置项的值,有值表示更改配置项
    if(is_string($name)){
        $name=  strtoupper($name);
        //获得配置项的值
        if(is_null($value)){
          return  array_key_exists_d($name,$config)?$config[$name]:null;
        }else{
            //设置值
            $config[$name]=$value;
            return true;
        }
    }
}
PHP速学视频免费教程(入门到精通)
PHP速学视频免费教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Tailwind CSS 响应式布局怎么写
Tailwind CSS 响应式布局怎么写

Tailwind CSS 响应式布局通常从移动端样式写起,再用 sm、md、lg 等前缀调整不同屏幕宽度下的网格、间距和导航。本专题整理断点配置、Grid 与 Flex 自适应布局及常见溢出问题,帮助你逐步完成多设备适配。

2026.11.03

0

19

Tailwind CSS 常用类名怎么记
Tailwind CSS 常用类名怎么记

Tailwind CSS 类名很多,先按布局、间距、文字、颜色和状态分组记忆,再通过官方文档检索具体写法。本专题结合类名排序、动态拼接失效、自定义实用类等问题,帮助你建立常用类名的速查方法。

2026.11.03

0

12

码上飞入口地址合集
码上飞入口地址合集

本专题汇总了码上飞(CodeFlying)的官方入口地址合集,涵盖官网主站及网页版在线使用入口,支持注册登录后直接在线体验零代码AI应用生成。同时整理了APP端下载渠道与小程序入口,助你快速访问平台,通过自然语言描述需求即可生成应用、小程序或网站。

2026.11.03

0

10

Uvicorn 报错与问题解决教程
Uvicorn 报错与问题解决教程

本专题汇总了Uvicorn服务器常见问题与报错解决的教程合集,涵盖启动失败、端口占用、模块导入错误、事件循环冲突及热重载失效等典型故障的排查思路。同时整理了多进程与Worker配置、SSL/HTTPS证书报错、跨域与代理配置等实战解决方案,助你快速定位并解决Uvicorn运行中的各类问题。

2026.11.02

20

14

Uvicorn使用教程
Uvicorn使用教程

本专题汇总了Uvicorn使用教程与启动运行指南,涵盖其作为Python ASGI服务器的定位与特点,以及命令行启动(uvicorn main:app --reload)、参数配置(host/port/workers)、程序内嵌运行与Gunicorn结合部署等核心用法。同时整理了热重载调试、日志配置与生产环境部署优化等技巧,助你快速掌握Uvicorn的运行与配置。

2026.11.02

20

17

Uvicorn安装配置教程
Uvicorn安装配置教程

本专题汇总了Uvicorn的安装配置教程与部署指南,涵盖通过pip安装Uvicorn、启动FastAPI/Starlette应用、开发模式热重载(--reload)及多进程工作模式配置等核心操作。同时整理了结合Gunicorn的生产部署方案、HTTPS与反向代理配置及性能调优技巧,助你快速完成Uvicorn服务的安装与上线部署。

2026.11.02

20

17

Vite 项目如何打包和部署
Vite 项目如何打包和部署

Vite项目打包部署专题汇集生产构建、产物预览、输出目录、资源路径、Nginx与GitHub Pages部署及常见白屏和路由404问题的已审核文章,帮助读者完成上线流程。

2026.10.28

120

16

Vite 如何搭配 Vue.js
Vite 如何搭配 Vue.js

Vite搭配Vue.js专题汇集创建Vue 3项目、配置开发环境、路径别名、Vue插件、代理、多环境构建与调试的已审核文章,帮助读者完成从初始化到日常开发的配置。

2026.10.28

100

15

Vite 项目目录怎么看
Vite 项目目录怎么看

Vite项目目录专题汇集入口文件、src源码、public静态资源、路径别名、项目根目录和构建输出目录相关的已审核文章,帮助读者理解各文件夹的用途与引用方式。

2026.10.28

300

10

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
热门推荐
/
最新课程
phpStudy极速入门视频教程
phpStudy极速入门视频教程

共6课时 | 54.7万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时 | 134.1万人学习