热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

phpgd类,phpGD库类

***GD库类*功能:水印缩略图验证码*classImageTool{protectedstatic$erroTxt;***getImgInfo*获取图片信息*par

/**

* GD库类

* 功能:水印 缩略图 验证码*/

classImageTool{protected static $erroTxt;/**

* getImgInfo

* 获取图片信息

* param filename

* return Array/False*/

protected static function getImgInfo($filename){if (!file_exists($filename)) {#code...

ImageTool::$erroTxt = $filename.‘图片不存在‘;return false;

}$imgInfo = getimagesize($filename);if(!$imgInfo){

ImageTool::$erroTxt = $filename.‘未知图片类型‘;return false;

}$info[‘width‘] = $imgInfo[0];$info[‘height‘] = $imgInfo[1];$info[‘ext‘] = strtolower( str_replace(‘/‘, ‘‘, strrchr($imgInfo[‘mime‘],‘/‘)) );return $info;

}/**

* water()

* 功能:图片加水印

* param : $dst_img 目标图片

* $water_img 水印图片

* $save 目标函数保存路径

* $pos 水印位置 上右下左 0 1 2 3 默认为2右下角

* $alpha透明度

* return*/

public static function water($dst_img,$water_img,$save=‘‘,$pos=‘2‘,$alpha=‘50‘){$dst_info = ImageTool::getImgInfo($dst_img);$water_info = ImageTool::getImgInfo($water_img);if(!$dst_info || !$water_info){return false;

}//判断水印图片是否大于目标图片

if ($dst_info[‘width‘]<&#61;$water_info[‘width‘] || $dst_info[‘height‘]<&#61;$water_info[‘height‘]) {#code...

ImageTool::$erroTxt&#61;‘水印图片大于待操作图片‘;return false;

}//生产对应画布函数

$createDstFun &#61; ‘imagecreatefrom‘.$dst_info[‘ext‘];$createWaterFun &#61; ‘imagecreatefrom‘.$water_info[‘ext‘];//return $createWaterFun;

if (!function_exists($createDstFun)) {#code...

ImageTool::$erroTxt&#61;$createDstFun.‘函数异常‘;return false;

}if (!function_exists($createWaterFun)) {#code...

ImageTool::$erroTxt&#61;$createWaterFun.‘函数异常‘;return false;

}//创建画布

// ob_clean();

$dst_im &#61; $createDstFun($dst_img);$water_im &#61; $createWaterFun($water_img);//画布函数操作

$src_x &#61; 0;$src_y &#61; 0;$src_w &#61; $water_info[‘width‘];$src_h &#61; $water_info[‘height‘];switch ($pos) {case ‘0‘:

#code...左上角

$dst_x &#61; 0;$dst_y &#61; 0;break;case ‘1‘:

#code...右上角

$dst_x &#61; $dst_info[‘width‘]-$water_info[‘width‘];$dst_y &#61; 0;break;case ‘3‘:

#code...左下角

$dst_x &#61; 0;$dst_y &#61; $dst_info[‘height‘]-$water_info[‘height‘];break;default:

#code...右下角

$dst_x &#61; $dst_info[‘width‘]-$water_info[‘width‘];$dst_y &#61; $dst_info[‘height‘]-$water_info[‘height‘];break;

}//return $pos;

$rs &#61; imagecopymerge ( $dst_im , $water_im , $dst_x , $dst_y , $src_x , $src_y , $src_w , $src_h , $alpha);if( !$rs){

ImageTool::$erroTxt&#61;‘水印添加失败‘;return false;

}$imgInputFun&#61;‘image‘.$dst_info[‘ext‘];if(!$save){$save &#61; $dst_img;unlink($dst_img);

}$imgInputFun($dst_im,$save);

imagedestroy($dst_im);

imagedestroy($water_im);return true;

}/**

* thumb()

* 缩略图

* param :

* $src_img 原图片路径

* $thumb_w,$thumb_h 缩略图宽高

* $save 缩略图保存路径*/

public static function thumb($src_img,$thumb_w&#61;‘200‘,$thumb_h&#61;‘200‘,$save&#61;‘‘){$thumbInfo &#61; ImageTool::getImgInfo($src_img);if(!$thumbInfo){return false;

}//创建画布

$dst_im &#61; imagecreatetruecolor($thumb_w,$thumb_h);//缩略图函数

$thumbFun &#61; ‘imagecreatefrom‘.$thumbInfo[‘ext‘];$thumb_im &#61; $thumbFun($src_img);//缩略图比例

$percent &#61; min( $thumb_w/$thumbInfo[‘width‘],$thumb_h/$thumbInfo[‘height‘] ); //小数

$bg &#61; imagecolorallocate($dst_im, 255, 255, 255);

imagefill($dst_im, 0, 0, $bg); //填充背景为白色

$dst_image &#61; $dst_im;$src_image &#61; $thumb_im;$src_x &#61; 0;$src_y &#61; 0;$dst_w &#61; $thumbInfo[‘width‘]*$percent; //图片粘贴到缩略图的宽

$dst_h &#61; $thumbInfo[‘height‘]*$percent; //图片粘贴到缩略图的高

$src_w &#61; $thumbInfo[‘width‘];$src_h &#61; $thumbInfo[‘height‘];$dst_x &#61; ($thumb_w - $dst_w)/2;$dst_y &#61; ($thumb_w - $dst_h)/2;$rs &#61; imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);if(!$rs){

ImageTool::$erroTxt&#61;‘生成缩略图失败‘;return false;

}$imgInputFun&#61;‘image‘.$thumbInfo[‘ext‘];if(!$save){//$path &#61; str_replace(‘D:/WWW/study/food/‘, ‘../‘, dirname($src_img)).‘/thumb_‘.basename($src_img);

$path &#61; dirname($src_img).‘/thumb_‘.basename($src_img);

}else{//$path &#61; str_replace(‘D:/WWW/study/food/‘, ‘../‘, dirname($src_img)).‘/‘.$save.‘_‘.basename($src_img);

$path &#61; dirname($src_img).‘/‘.$save.‘_‘.basename($src_img);

}$save &#61; $path;$imgInputFun($dst_im,$save);

imagedestroy($dst_im);return $path;

}/**

* captcha

* 生成验证码

* param : $width 验证码宽 $heigth 验证码高

* $lineNum 线条干扰码数目

* $alpha 线条干扰码透明度

* $length 验证码字符长度

* return String*/

public static function captchat($width&#61;‘70‘,$height&#61;‘25‘,$lineNum&#61;‘10‘,$length&#61;‘4‘,$alpha&#61;‘80‘){$capt_im &#61; imagecreatetruecolor($width, $height);$bak_im &#61; imagecreatetruecolor($width, $height);$bg &#61; imagecolorallocate($capt_im, 100, 100, 100);$color &#61; imagecolorallocate($capt_im, rand(0,255), rand(0,255), rand(0,255));

imagefill($capt_im, 0, 0, $bg);

imagefill($bak_im, 0, 0, $bg);//干扰线

for($i&#61;0;$i

imageline($capt_im, rand(0,$width), rand(0,$height), rand(0,$width), rand(0,$height), $lincolor);

}//干扰点

for($i&#61;0;$i<200;$i&#43;&#43;){$dotcolor &#61; imagecolorallocatealpha($capt_im, rand(0,255), rand(0,255), rand(0,255),$alpha);

imagesetpixel($capt_im,rand(0,$width), rand(0,$height), $dotcolor);

}$size &#61;18;$angle &#61; 0;$x &#61; 2;$y &#61; 22;$fontfile &#61; ‘verdana.ttf‘;$text &#61; ImageTool::randName($length);

imagettftext($capt_im, $size, $angle, $x, $y, $color, $fontfile, $text);//验证码扭曲

$range &#61; 4; //扭曲程度

// imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)

for($i&#61;0;$i

imagecopyresampled($bak_im, $capt_im, $i, $y, $i, 0, $i, $height, $i, $height);

}header("Content-type:image/png");

imagepng($bak_im);

imagedestroy($capt_im);

imagedestroy($bak_im);return true;

}/**

* 生成随机字符

* arg $n

* return string*/

protected static function randName($n&#61;6){ //$n 代表截取长度

$str &#61; ‘abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ0123456789‘;$str &#61; str_shuffle($str); //随机打乱一个字符串

$str &#61; substr($str,0,$n); //截取子字符串

return $str;

}/**

* getError()

* 获取错误信息

* param String

* return String*/

public static functiongetError(){return ImageTool::$erroTxt;

}

}//ImageTool::captchat();

?>



推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • PHP中的单例模式与静态变量的区别及使用方法
    本文介绍了PHP中的单例模式与静态变量的区别及使用方法。在PHP中,静态变量的存活周期仅仅是每次PHP的会话周期,与Java、C++不同。静态变量在PHP中的作用域仅限于当前文件内,在函数或类中可以传递变量。本文还通过示例代码解释了静态变量在函数和类中的使用方法,并说明了静态变量的生命周期与结构体的生命周期相关联。同时,本文还介绍了静态变量在类中的使用方法,并通过示例代码展示了如何在类中使用静态变量。 ... [详细]
  • 本文介绍了深入浅出Linux设备驱动编程的重要性,以及两种加载和删除Linux内核模块的方法。通过一个内核模块的例子,展示了模块的编译和加载过程,并讨论了模块对内核大小的控制。深入理解Linux设备驱动编程对于开发者来说非常重要。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
author-avatar
kicie569
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有