讲师中心 微信公众号
AI工具推荐 视频效率加速

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

千芳同学_3365

千芳同学_3365

发布时间:2016-05-23 16:41:04

|

1507人浏览过

|

来源于php中文网

原创

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

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

热门AI工具

更多
讯飞绘文

讯飞绘文是一款由科大讯飞推出的一站式 AIGC 内容运营平台。

豆包大模型

豆包大模型是一款由字节跳动推出的企业级大语言模型服务平台。

WorkBuddy

一款AI办公效率工具,主要用于腾讯云推出的AI原生桌面智能体工作台,适合需要提升相关任务效率的用户。

AionClaw
AionClaw Hot

AionClaw是一款面向办公、创作和编程任务的AI桌面智能体。

UpDream
UpDream Hot

一款AI视频创作工具,主要用于哔哩哔哩推出的自研AI视频创作工具,适合需要提升相关任务效率的用户。

Loomy
Loomy Hot

一款AI工具,主要用于科大讯飞发布的桌面级 AI 助理,比 OpenClaw 更易用、更安全!,适合需要提升相关任务效率的用户。

Lovart
Lovart Hot

一款面向视觉设计创作的AI设计平台,可通过智能体和画布工作流辅助制作海报、Logo、网页、PPT及其他视觉内容。

音述AI
音述AI Hot

一款AI音频处理工具,主要用于音述AI是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区,适合需要提升相关任务效率的用户。

DeepSeek

DeepSeek是一款面向对话、写作、编程和推理场景的AI大模型工具。

相关专题

更多
Buffalo框架数据库开发全教程
Buffalo框架数据库开发全教程

本专题围绕Buffalo框架数据库开发,讲解database.yml多环境配置、soda与fizz迁移生成回滚、模型结构体标签、增删改查与条件查询、一对多与多对多关联、数据校验、回调钩子、事务处理及原生SQL执行能力。

20

2026.09.23

Buffalo框架路由与请求处理实操指南
Buffalo框架路由与请求处理实操指南

本专题讲解Buffalo框架路由与请求处理机制,涵盖路由注册与分组、资源路由、Handler编写规范、Context上下文方法、参数绑定、中间件编写挂载、Session与Cookie读写、Flash消息及错误页面定制方法。

0

2026.09.23

Buffalo框架零基础入门教程
Buffalo框架零基础入门教程

本专题整理Buffalo框架入门内容,涵盖Go环境准备、buffalo CLI安装、新项目生成、目录结构说明、dev热加载启动、数据库连接配置与常见报错排查,帮助新手按约定优于配置的思路跑通第一个Buffalo框架应用。

20

2026.09.23

Conan创建软件包配方指南
Conan创建软件包配方指南

本专题介绍通过conanfile.py创建软件包的方法,讲解包名、版本、依赖和构建设置等基础信息,以及source、build、package、package_info等常用方法的作用及编写思路。

0

2026.09.22

Conan二进制包配置指南
Conan二进制包配置指南

本专题介绍Conan根据操作系统、编译器、架构和构建类型生成二进制包的方法,讲解Profile、Settings、Options及Package ID的作用,帮助管理不同平台和编译环境下的包版本。

20

2026.09.22

Conan私有仓库搭建教程
Conan私有仓库搭建教程

本专题系统的讲解Conan私有仓库的搭建流程,涵盖仓库服务部署、存储目录配置、用户认证、权限划分和远程地址添加,并介绍内部C++依赖包的上传、下载及版本维护方法。

20

2026.09.22

loomy官网入口地址合集
loomy官网入口地址合集

本专题汇总了 Loomy 桌面 AI 助理的官方入口地址合集及使用指南。提供 macOS 与 Windows 客户端下载 。Loomy 是讯飞推出的桌面级 AI 工作搭子,支持文件整理、数据分析、网页操作及通过飞书/钉钉远程操控电脑,助你高效完成本地办公任务 。

20

2026.09.22

NumPy常见函数使用方法
NumPy常见函数使用方法

本专题整理 NumPy 常见函数使用方法相关教程,覆盖函数大全、参数用法、数组运算、统计聚合、排序处理、where 条件筛选、linspace 创建数列等常用场景,帮助读者快速掌握 NumPy 函数调用思路和实际数据处理技巧。

0

2026.09.22

NumPy性能优化版本更新与常见报错排查
NumPy性能优化版本更新与常见报错排查

本专题整理 NumPy 性能优化、版本更新与常见报错排查相关教程,覆盖向量化计算、广播性能、内存布局、NumPy 2.0 升级、版本兼容冲突、安装导入报错、dtype 溢出、矩阵运算异常和 broadcasting 报错修复,帮助读者系统掌握 NumPy 性能调优与问题定位方法。

20

2026.09.22

热门下载

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

精品课程

更多
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn