php图片水印和缩略图的功能实现

星辰君_4023

星辰君_4023

2016-06-16

1198人浏览

原创

跳至 [1]

[全屏预览]

<?php header('Content-Type:text/html;charset=utf-8');
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//给图片添加水印
Class Water{
    //开启水印
    private $watermark_on = '1';
    
    public $water_img;
    
    //水印位置
    public $pos = 1;    
    
    //压缩比
    public $pct = 80;
    
    //透明度
    public $quality = 80;
    
    public $text = '乐趣网zlblog.sinaapp.com';
    
    public $size = 12;
    
    public $color = '#000000';
    
    public $font = 'font.ttf';
    
    //thumb的制作
    //默认缩略图功能开启
    private $thumb_on = 1;
    //生成缩略图的方式
    public $thumb_type = 1;
    //生成缩略图的宽度
    public $thumb_width;
    //生成缩略图的高度
    public $thumb_height;
    //生成缩略图的后缀名
    public $thumb_fix = '_dq';

    //缩略图函数处理
    public function thumb( $img,$outfile='',$t_type='',$t_w='',$t_h='' ){
        //验证图片是否符合要求
        if(!$this->check($img) || !$this->thumb_on) return FALSE;
        
        //定义缩略图的初始值
        $t_type = $t_type ? $t_type : $this->thumb_type;
        $t_w = $t_w ? $t_w : $this->thumb_width;
        $t_h = $t_h ? $t_h : $this->thumb_height;
        
        //获取到原图的信息
        $img_info = getimagesize($img);
        $img_w = $img_info[0];
        $img_h = $img_info[1];
        //取得图像类型的文件后缀
        $img_type = image_type_to_extension($img_info[2]);
        //获取到相关尺寸
        $thumb_size = $this->thumb_size($img_w,$img_h,$t_w,$t_h,$t_type);
        //确定原始图像类型
        //利用自定义函数来实现图片类型的确定
        $func = "imagecreatefrom".substr($img_type, 1);
        $res_img = $func($img);
        
        //缩略图资源            编辑图片资源moon
        if( $img_type == '.gif' || $img_type == '.png' ){
            $res_thumb = imagecreate($thumb_size[0], $thumb_size[1]);
            $color = imagecolorallocate($res_thumb, 255, 0, 0);
        }else{
            $res_thumb = imagecreatetruecolor($thumb_size[0], $thumb_size[1]);
        }
        
        //制作缩略图
        if(function_exists( "imagecopyresampled" ) ){
            imagecopyresampled($res_thumb, $res_img, 0, 0, 0, 0, $thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
        }else{
            imagecopyresized($res_thumb, $res_img, 0, 0, 0, 0, $thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
        }
        //处理透明色
        if( $img_type =='.gif' || $img_type == '.png' ){
            imagecolortransparent($res_thumb,$color);
        }
        
        //配置输出文件名
        $outfile = $outfile ? $outfile : $outfile.substr($img,0,strripos($img,'.')).$this->thumb_fix.$img_type;
        
        //文件的保存输出
        $func = "image".substr($img_type, 1);
        $func($res_thumb,$outfile);
        if(isset($res_thumb)) imagedestroy ($res_thumb);
        if(isset($res_img)) imagedestroy ($res_img);
        return $outfile;
    } 

    public function watermark( $img,$pos='',$out_img='',$water_img='',$text='' ){
        if(!$this->check($img) || !$this->watermark_on) return false;
        
        $water_img = $water_img ? $water_img : $this->water_img;
        //水印的开启状态
        $waterimg_on = $this->check($water_img) ? 1 : 0;
        //判断是否在原图上操作
        $out_img = $out_img ? $out_img : $img;
        //判断水印的位置
        $pos = $pos ? $pos : $this->pos;
        //水印文字
        $text = $text ? $text : $this->text;
        
        
        $img_info = getimagesize($img);
        $img_w = $img_info[0];
        $img_h = $img_info[1];
        //判断水印图片的类型
        
        
        if( $waterimg_on ){
            $w_info = getimagesize($water_img);
            $w_w = $w_info[0];
            $w_h = $w_info[1];
            if ( $img_w color)!=7 ) return FALSE;
            $text_info = imagettfbbox($this->size, 0, $this->font, $text);
            $w_w = $text_info[2] - $text_info[6];
            $w_h = $text_info[3] - $text_info[7];
        }
        
        //建立原图资源
        
        switch ( $img_info[2] ){
            case 1:
                $res_img = imagecreatefromgif($img);
                break;
            case 2:
                $res_img = imagecreatefromjpeg($img);
                break;
            case 3:
                $res_img = imagecreatefrompng($img);
                break;
        }
        //确定水印的位置
        switch ( $pos ){
            case 1:
                $x = $y =25;
                break;
            case 2:
                $x = ($img_w - $w_w)/2; 
                $y = 25;
                break;
            case 3:
                $x = $img_w - $w_w;
                $y = 25;
                break;
            case 4:
                $x = 25;
                $y = ($img_h - $w_h)/2;
                break;
            case 5:
                $x = ($img_w - $w_w)/2; 
                $y = ($img_h - $w_h)/2;
                break;
            case 6:
                $x = $img_w - $w_w;
                $y = ($img_h - $w_h)/2;
                break;
            case 7:
                $x = 25;
                $y = $img_h - $w_h;
                break;
            case 8:
                $x = ($img_w - $w_w)/2;
                $y = $img_h - $w_h;
                break;
            case 9:
                $x = $img_w - $w_w;
                $y = $img_h - $w_h;
                break;
            default :
                $x = mt_rand(25, $img_w - $w_w);
                $y = mt_rand(25, $img_h - $w_h);
        }
        
        //写入图片资源
        if( $waterimg_on ){
            imagecopymerge($res_img, $w_img, $x, $y, 0, 0, $w_w, $w_h, $this->pct);  
    }else{
        $r = hexdec(substr($this->color, 1,2));
        $g = hexdec(substr($this->color, 3,2));
        $b = hexdec(substr($this->color, 5,2));
        $color = imagecolorallocate($res_img, $r, $g, $b);
        imagettftext($res_img, $this->size, 0, $x, $y, $color, $this->font, $text);    
    }
    
    //生成图片类型
    switch ( $img_info[2] ){
        case 1:
            imagecreatefromgif($res_img,$out_img);
            break;
        case 2:
            //imagecreatefromjpeg($res_img,$out_img);
            imagejpeg($res_img,$out_img);
            break;
        case 3:
            imagepng($res_img,$out_img);
            break;
    }
    if(isset($res_img)) imagedestroy ($res_img);
    if(isset($w_img))   imagedestroy($w_img);
    return TRUE;
}   
    //验证图片是否存在
        private function check($img){
            $type = array('.jpg','.jpeg','.png','.gif');
            $img_type = strtolower(strrchr($img, '.'));
            return extension_loaded('gd') && file_exists($img) && in_array($img_type, $type);
        } 
        
        //获取缩略图的相关比例
        //获取到图片的处理类型
        private function thumb_size( $img_w,$img_h,$t_w,$t_h,$t_type){
            //定义缩略图尺寸
            $w = $t_w;
            $h = $t_h;
            
            //定义图片的原始尺寸
            $cut_w = $img_w;
            $cut_h = $img_h;
            
            //当要目标图像小于缩略图的尺寸时;
            if( $img_w 0 ){
                    switch ( $t_type ){
                        //当宽度固定时
                        case 1:
                            $h = $t_w/$img_w*$img_h;
                            break;
                        //高度固定时
                        case 2:
                            $w = $t_h/$img_h*$img_w;
                            break;
                        //宽度固定,高度裁切
                        case 3:
                            $cut_h = $img_w/$t_w*$t_h;
                            break;
                        //高度固定,宽度裁切
                        case 4:
                            $cut_w = $img_h/$t_h*$t_w;
                            break;
                        //等比例缩放
                        default :
                            if( ($img_w/$t_w) > ($img_h/$t_h) ){
                              $h = $t_w/$img_w*$t_h;
                            }elseif( ($img_w/$t_w) 
    				    				            	            	
            	        	
			<style type="text/css">
				.code_report{float:right}
				.code_report div{ float: left; margin-left: 5px; background: url("/img/ask-icon.gif") no-repeat; padding: 6px 0 6px 15px; padding-left: 15px; height: 16px;}
				.code_report a{ background: url("/img/ask-icon.gif") 0 -160px no-repeat;padding-left: 20px; line-height: 15px;height: 16px; color: #333; text-decoration: none; display:inline-block; zoom:1; vertical-align: middle;}
				.code_report em{ height: 28px; line-height:28px; width: 14px; display: inline-block; float: left; background: url("/img/ask-icon.gif") top right;}
            </style>

php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!

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万人学习