PHP图像处理类库及演示分享

近期需要做一些图像处理方面的学习和研究,首要任务就是选择一套合适的图像处理类库。于是参考其他类库自己编写了一个简单的,仅仅实现了指定文字内容创建图片(不支持中文)、创建验证码图片、创建缩略图,有需要的小伙伴可以参考下。

简单写了一个PHP的图像处理类库,虽然功能比较少,但是目前也没用到太高级的,以后用到了再填吧,或者哪位给点建议加上什么功能,或者有什么需求可以跟我说,我有时间加上,如果哪位对这个类库进行了扩展的话,还麻烦拿出来大家分享一下,代码现在是能用就行,考虑的东西不是很多,有什么更好的建议请告诉我,谢谢

Img.php

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: MCtion
  5. * Date: 2015/5/14 0014
  6. * Time: 15:36
  7. * 简单的图像类库,本类中所有相对路径均基于网站根目录,如需修改,则修改常量__WEBROOT__即可
  8. * 功能:指定文字内容创建图片(不支持中文)、创建验证码图片、创建缩略图、其他功能待续
  9. * 方法:
  10. * Style(array $Options) 设置图片样式,每次设定前将重置为默认样式
  11. * Create_Img_Png() 创建PNG图像,相关属性由Style指定
  12. * Create_Img_Jpeg() 创建JPEG图像,相关属性由Style指定
  13. * Create_Verify() 创建验证码图像,相关属性由Style指定
  14. * Get_Verify() 获得创建的验证码值,MD5加密
  15. * Load_Img(string $FilePath) 加载图像,创建图像源,供其他方法调用源,FilePath为图像相对路径
  16. * Create_Thumb(string $FileName,string $FilePath) 创建由Load_Img()加载的图像的缩略图,FileName为保存后的图像前缀,FilePath为保存图像的相对路径,不包含文件名(例:/Uploads/images/Thumb/)
  17. */
  18. if(!defined("__WEBROOT__")) define("__WEBROOT__",$_SERVER['DOCUMENT_ROOT']);
  19. class Img {
  20. protected $_Img; //图片源
  21. protected $_FileImg; //加载的图片源
  22. protected $_FileInfo; //加载的图片的信息数组
  23. protected $_PicInfo; //加载的图片的宽高等信息数组
  24. protected $_Rand = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ1234567890'; //随机因子
  25. protected $_Code = ''; //验证码
  26. public $Width = 300;//图片默认宽
  27. public $Height = 80; //图片默认高
  28. public $BackgroundColor = "000000";
  29. public $Font = "/phps/Public/Font/ARIALNB.TTF"; //默认字体
  30. public $FontSize = 16; //默认字体字号
  31. public $FontColor = "ffffff"; //默认字体颜色
  32. public $Content = "Test Word";
  33. public $Align = "left";
  34. public $Codes = 4; //验证码个数
  35. public $Line = 6; //干扰线条的个数
  36. public $LoadErr = ''; //错误信息
  37. //public function __construct(){}
  38. /** 设置图片属性
  39. * @param array $Options 属性数组
  40. * @return $this 返回对象
  41. */
  42. public function Style($Options){
  43. $this -> _Re_Set();
  44. foreach($Options as $K=>$V){
  45. if(in_array($K,array('Width','Height','BackgroundColor','Font','FontSize','FontColor','Content','Align','Codes','Line','Snow'))){
  46. if($K == "BackgroundColor" || $K == "FontColor"){
  47. if(preg_match("#([a-zA-Z0-9]{6})#",$V)) $this -> $K = $V;
  48. }else{
  49. $this -> $K = $V;
  50. }
  51. }
  52. }
  53. return $this;
  54. }
  55. /**
  56. * 重置属性,不提供外部访问
  57. */
  58. protected function _Re_Set(){
  59. $this -> Width = 100;
  60. $this -> Height = 30;
  61. $this -> BackgroundColor = "000000";
  62. $this -> Font = "/phps/Public/Font/ARIALNB.TTF";
  63. $this -> FontSize = 16;
  64. $this -> FontColor = "ffffff";
  65. $this -> Align = "left";
  66. $this -> Codes =4;
  67. $this -> Line = 6;
  68. }
  69. /**
  70. * 创建图像源、添加背景、创建图像
  71. * @param bool $BGC 指定是否创建背景色及矩形块
  72. */
  73. protected function _Create_Img_GB($BGC = True){
  74. $this -> _Img = imagecreatetruecolor($this -> Width,$this -> Height); //创建背景源
  75. if($BGC){
  76. preg_match("#([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})#",$this -> BackgroundColor,$ColorArr); //将颜色值分隔成三组16位进制数
  77. $Color = imagecolorallocate($this -> _Img,hexdec($ColorArr[1]),hexdec($ColorArr[2]),hexdec($ColorArr[3])); //给Img图像源添加背景色
  78. imagefilledrectangle($this -> _Img,0,$this -> Height,$this -> Width,0,$Color); //创建图像
  79. }
  80. }
  81. /**
  82. * 创建随机验证码
  83. */
  84. protected function _Create_Code(){
  85. $Len = strlen($this -> _Rand) - 1;
  86. for($i = 0;$i < $this -> Codes;$i++){
  87. $this -> _Code .= $this -> _Rand[mt_rand(0,$Len)];
  88. }
  89. }
  90. /**
  91. * 向图像中写入字符串,暂不支持中文
  92. */
  93. protected function _Write_Text(){
  94. $FontWidth = imagefontwidth($this -> FontSize); //获取字号的一个字符的宽度
  95. preg_match_all('/(.)/us', $this -> Content, $TextArr); //将内容分隔成数组一遍统计个数
  96. $FontHeight = imagefontheight($this -> FontSize); //获取字号的高度
  97. $X = ceil(($this -> Width - ($FontWidth * count($TextArr[0]))) / 2); //设置X轴距左边距的距离
  98. $Y = ceil(($this -> Height + $FontHeight) / 2); //设置Y轴距上边距的距离
  99. preg_match("#([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})#",$this -> FontColor,$ColorArr);
  100. $Color = imagecolorallocate($this -> _Img,hexdec($ColorArr[1]),hexdec($ColorArr[2]),hexdec($ColorArr[3])); //设置文字颜色
  101. imagettftext($this -> _Img,$this -> FontSize,0,$X,$Y,$Color,__WEBROOT__.$this -> Font,$this -> Content); //写入内容
  102. }
  103. /**
  104. * 向图像中写入验证码
  105. */
  106. protected function _Write_Code(){
  107. $_X = $this -> Width / $this -> Codes; //设置宽高比
  108. for($i = 0;$i < $this -> Codes;$i++){ //循环Codes次,每次生成一位验证码值
  109. $Color = imagecolorallocate($this -> _Img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); //随机生成验证码值的颜色
  110. imagettftext($this -> _Img,$this -> FontSize,mt_rand(-30,30),$_X*$i+mt_rand(1,5),$this -> Height / 1.3,$Color,__WEBROOT__.$this -> Font,$this -> _Code[$i]); //生成一位验证码值
  111. }
  112. }
  113. /**
  114. * 向图像中写入干扰线条
  115. */
  116. protected function _Write_Line() { //生成干扰线条
  117. for ($i=0;$i < $this -> Line;$i++) {
  118. $Color = imagecolorallocate($this -> _Img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
  119. imageline($this -> _Img,mt_rand(0,$this -> Width),mt_rand(0,$this -> Height),mt_rand(0,$this -> Width),mt_rand(0,$this -> Height),$Color);
  120. }
  121. }
  122. /**
  123. * 设置图像类型为JPEG
  124. */
  125. protected function _Img_Jpeg(){
  126. header('Content-type:image/jpeg');
  127. imagejpeg($this -> _Img);
  128. imagedestroy($this -> _Img);
  129. }
  130. /**
  131. * 设置图像类型为PNG
  132. */
  133. protected function _Img_Png(){
  134. header('Content-type:image/png');
  135. imagepng($this -> _Img);
  136. imagedestroy($this -> _Img);
  137. }
  138. /**
  139. * 创建JPEG的字符串图像
  140. */
  141. public function Create_Img_Jpg(){
  142. $this -> _Create_Img_GB(True);
  143. $this -> _Write_Text();
  144. $this -> _Img_Jpeg();
  145. }
  146. /**
  147. * 创建PNG的字符串图像
  148. */
  149. public function Create_Img_Png(){
  150. $this -> _Create_Img_GB(True);
  151. $this -> _Write_Text();
  152. $this -> _Img_Png();
  153. }
  154. /**
  155. * 创建验证码的PNG图像
  156. */
  157. public function Create_Verify(){
  158. $this -> BackgroundColor = '';
  159. for($I = 0;$I < 3;$I++){
  160. $this -> BackgroundColor .= dechex(mt_rand(20,155));
  161. }
  162. $this -> _Create_Img_GB(True);
  163. $this -> _Create_Code();
  164. $this -> _Write_Line();
  165. $this -> _Write_Code();
  166. $this -> _Img_Png();
  167. }
  168. /**
  169. * 外部获取MD5加密后的验证码
  170. * @return string
  171. */
  172. public function Get_Verify(){
  173. return md5($this -> _Code);
  174. }
  175. /**
  176. * 加载一个图像文件,并获取图像相关信息
  177. * @param string $FilePath 图像相对路径地址
  178. * @return $this|bool 成功返回对象,否则返回FALSE
  179. */
  180. public function Load_Img($FilePath){
  181. $FilePath = __WEBROOT__.$FilePath;
  182. if(!is_file($FilePath)){
  183. $this -> LoadErr = "路径错误,文件不存在";
  184. Return False;
  185. }
  186. $this -> _PicInfo = getimagesize($FilePath);
  187. $this -> _FileInfo = pathinfo($FilePath);
  188. switch($this -> _PicInfo[2]){
  189. case 1:$this ->_FileImg = imagecreatefromgif($FilePath);break;
  190. case 2:$this ->_FileImg = imagecreatefromjpeg($FilePath);break;
  191. case 3:$this ->_FileImg = imagecreatefrompng($FilePath);break;
  192. default:$this -> LoadErr = "类型错误,不支持的图片类型";Return False;
  193. }
  194. Return True;
  195. }
  196. /**
  197. * 创建缩略图
  198. * @param string $FileName 保存的图片名称前缀
  199. * @param string $FilePath 保存图片的相对路径
  200. * @return mixed 返回生成的图片的信息数组
  201. */
  202. public function Create_Thumb($FileName,$FilePath){
  203. $SavePath = __WEBROOT__.$FilePath;
  204. if(!file_exists($SavePath)){
  205. mkdir($SavePath,0777,true);
  206. }
  207. $FileName = $FileName.date("YmdHis").rand(100,999).'.'.$this -> _FileInfo['extension'];
  208. $FilePath = $FilePath.$FileName;
  209. $SavePath = $SavePath.$FileName;
  210. $this -> _Create_Img_GB(False);
  211. imagecopyresampled($this -> _Img,$this -> _FileImg,0,0,0,0,$this -> Width,$this -> Height,$this -> _PicInfo[0],$this -> _PicInfo[1]);
  212. switch($this -> _PicInfo[2]){
  213. case 1:imagegif($this -> _Img,$SavePath);break;
  214. case 2:imagejpeg($this -> _Img,$SavePath);break;
  215. case 3:imagepng($this -> _Img,$SavePath);break;
  216. }
  217. $FIleInfo['FileName'] = $FileName;
  218. $FIleInfo['FilePath'] = $FilePath;
  219. Return $FIleInfo;
  220. }
  221. }

使用示例

  1. $Img = new Img();
  2. $Options['Width'] = 300;
  3. $Options['Height'] = 100;
  4. $Options['Content'] = "Test Create Img";
  5. $Options['FontColor'] = "FF0000";
  6. $Options['BackgroundColor'] = "AAAAAA";
  7. $Img -> Style($Options) -> Create_Img_Jpg();
  8. if($Img -> Load_Img("/Public/images/ad1.png")){
  9. $FileInfo = $Img -> Style(array('Width'=>30,'Height'=>30)) -> Create_Thumb("Thumb","/Uploads/images/");
  10. var_dump($FileInfo);
  11. }else{
  12. die("加载图像失败,".$Img -> LoadErr);
  13. }

以上所述就是本文的全部内容了,希望大家能够喜欢。