当前位置:首页 > PHP教程 > php图像处理 > 列表

php常用图片处理类

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-14 21:37:12 浏览: 评论:0 

这篇文章主要介绍了php常用图片处理类,方便大家进行学习研究,感兴趣的小伙伴们可以参考一下,本文为大家分享的php常用图片处理类,供大家参考学习,具体内容如下。

  1. <?php 
  2. /*已知问题:1.在图片缩放功能中,使用imagecreatetruecolor函数创建画布,并使用透明处理算法,但PNG格式的图片无法透明。用imagecreate函数创建画布可以解决这个问题,但是缩放出来的图片色数太少了 
  3. * 
  4. * 
  5. *type值: 
  6. * (1):代表使用图片缩放功能,此时,$value1代表缩放后图片的宽度,$value2代表缩放后图片的高度 
  7. * (2):代表使用图片裁剪功能,此时,$value1代表裁剪开始点的坐标,例:从原点开始即是“0,0”前面是x轴后面是y轴,中间用,分隔,$value2代表裁剪的宽度和高度,同样也是“20,20”的形式使用 
  8. * (3):代表使用加图片水印功能,此时,$value1代表水印图片的文件名,$value2代表水印在图片中的位置,有10值个可以选,1代表左上,2代表左中,3代表左右,4代表中左,5代表中中,6代表中右,7代表下做,8代表下中,9代表下右,0代表随机位置 
  9. * 
  10. */ 
  11. class image{ 
  12. private $types; //使用的功能编号,1为图片缩放功能 2为图片裁剪功能 3,为图片加图片水印功能 
  13. private $imgtype;//图片的格式 
  14. private $image; //图片资源 
  15. private $width;//图片宽度 
  16. private $height;//图片高度 
  17. private $value1;//根据所传type值的不同,$value1分别代表不同的值 
  18. private $value2;//根据所传type值的不同,$value2分别代表不同的值 
  19. private $endaddress;//输出后的地址+文件名 
  20. function __construct($imageaddress, $types, $value1="", $value2="", $endaddress){ 
  21. $this->types=$types; 
  22. $this->image=$this->imagesources($imageaddress); 
  23. $this->width=$this->imagesizex(); 
  24. $this->height=$this->imagesizey(); 
  25. $this->value1=$value1; 
  26. $this->value2=$value2; 
  27. $this->endaddress=$endaddress; 
  28. } 
  29. function outimage(){ //根据传入type值的不同,输出不同的功能 
  30. switch($this->types){ 
  31. case 1: 
  32. $this->scaling(); 
  33. break; 
  34. case 2: 
  35. $this->clipping(); 
  36. break; 
  37. case 3: 
  38. $this->imagewater(); 
  39. break; 
  40. default: 
  41. return false; 
  42. } 
  43. } 
  44. private function imagewater(){ //http://www.hzhuti.com 加图片水印功能 
  45. //用函数获取水印文件的长和宽 
  46. $imagearrs=$this->getimagearr($this->value1); 
  47. //调用函数计算出水印加载的位置 
  48. $positionarr=$this->position($this->value2, $imagearrs[0], $imagearrs[1]); 
  49. //加水印 
  50. imagecopy($this->image, $this->imagesources($this->value1), $positionarr[0], $positionarr[1], 0, 0, $imagearrs[0], $imagearrs[1]); 
  51. //调用输出方法保存 
  52. $this->output($this->image); 
  53. } 
  54. private function clipping(){ //图片裁剪功能 
  55. //将传进来的值分别赋给变量 
  56. list($src_x, $src_y)=explode(",", $this->value1); 
  57. list($dst_w, $dst_h)=explode(",", $this->value2); 
  58. if($this->width < $src_x+$dst_w || $this->height < $src_y+$dst_h){ //这个判断就是限制不能截取到图片外面去 
  59. return false; 
  60. } 
  61. //创建新的画布资源 
  62. $newimg=imagecreatetruecolor($dst_w, $dst_h); 
  63. //进行裁剪 
  64. imagecopyresampled($newimg, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $dst_w, $dst_h); 
  65. //调用输出方法保存 
  66. $this->output($newimg); 
  67. } 
  68. private function scaling(){ //图片缩放功能 
  69. //获取等比缩放的宽和高 
  70. $this-> proimagesize(); 
  71. //根据参数进行缩放,并调用输出函数保存处理后的文件 
  72. $this->output($this->imagescaling()); 
  73. } 
  74. private function imagesources($imgad){ //获取图片类型并打开图像资源 
  75. $imagearray=$this->getimagearr($imgad); 
  76. switch($imagearray[2]){ 
  77. case 1://gif 
  78. $this->imgtype=1; 
  79. $img=imagecreatefromgif($imgad); 
  80. break; 
  81. case 2://jpeg 
  82. $this->imgtype=2; 
  83. $img=imagecreatefromjpeg($imgad); 
  84. break; 
  85. case 3://png 
  86. $this->imgtype=3; 
  87. $img=imagecreatefrompng($imgad); 
  88. break; 
  89. default: 
  90. return false; 
  91. } 
  92. return $img; 
  93. } 
  94. private function imagesizex(){ //获得图片宽度 
  95. return imagesx($this->image); 
  96. } 
  97. private function imagesizey(){ //获取图片高度 
  98. return imagesy($this->image); 
  99. } 
  100. private function proimagesize(){ //计算等比缩放的图片的宽和高 
  101. if($this->value1 && ($this->width < $this->height)) { //等比缩放算法 
  102. $this->value1=round(($this->value2/ $this->height)*$this->width); 
  103. }else{ 
  104. $this->value2=round(($this->value1/ $this->width) * $this->height); 
  105. } 
  106. } 
  107. private function imagescaling(){//图像缩放功能,返回处理后的图像资源 
  108. $newimg=imagecreatetruecolor($this->value1, $this->value2); 
  109. $tran=imagecolortransparent($this->image);//处理透明算法 
  110. if($tran >= 0 && $tran < imagecolorstotal($this->image)){ 
  111. $tranarr=imagecolorsforindex($this->image, $tran); 
  112. $newcolor=imagecolorallocate($newimg, $tranarr['red'], $tranarr['green'], $tranarr['blue']); 
  113. imagefill($newimg, 0, 0, $newcolor); 
  114. imagecolortransparent($newimg, $newcolor); 
  115. } 
  116. imagecopyresampled($newimg, $this->image, 0, 0, 0, 0, $this->value1, $this->value2, $this->width, $this->height); 
  117. return $newimg; 
  118. } 
  119. private function output($image){//输出图像 
  120. switch($this->imgtype){ 
  121. case 1: 
  122. imagegif($image, $this->endaddress); 
  123. break; 
  124. case 2: 
  125. imagejpeg($image, $this->endaddress); 
  126. break; 
  127. case 3: 
  128. imagepng($image, $this->endaddress); 
  129. break; 
  130. default: 
  131. return false; 
  132. } 
  133. } 
  134. private function getimagearr($imagesou){//返回图像属性数组方法 
  135. return getimagesize($imagesou); 
  136. } 
  137. private function position($num, $width, $height){//根据传入的数字返回一个位置的坐标,$width和$height分别代表插入图像的宽和高 
  138. switch($num){ 
  139. case 1: 
  140. $positionarr[0]=0; 
  141. $positionarr[1]=0; 
  142. break; 
  143. case 2: 
  144. $positionarr[0]=($this->width-$width)/2; 
  145. $positionarr[1]=0; 
  146. break; 
  147. case 3: 
  148. $positionarr[0]=$this->width-$width; 
  149. $positionarr[1]=0; 
  150. break; 
  151. case 4: 
  152. $positionarr[0]=0; 
  153. $positionarr[1]=($this->height-$height)/2; 
  154. break; 
  155. case 5: 
  156. $positionarr[0]=($this->width-$width)/2; 
  157. $positionarr[1]=($this->height-$height)/2; 
  158. break; 
  159. case 6: 
  160. $positionarr[0]=$this->width-$width; 
  161. $positionarr[1]=($this->height-$height)/2; 
  162. break; 
  163. case 7: 
  164. $positionarr[0]=0; 
  165. $positionarr[1]=$this->height-$height; 
  166. break; 
  167. case 8: 
  168. $positionarr[0]=($this->width-$width)/2; 
  169. $positionarr[1]=$this->height-$height; 
  170. break; 
  171. case 9: 
  172. $positionarr[0]=$this->width-$width; 
  173. $positionarr[1]=$this->height-$height; 
  174. break; 
  175. case 0: 
  176. $positionarr[0]=rand(0, $this->width-$width); 
  177. $positionarr[1]=rand(0, $this->height-$height); 
  178. break; 
  179. } 
  180. return $positionarr; 
  181. } 
  182. function __destruct(){ 
  183. imagedestroy($this->image); 
  184. } 
  185. } 
  186. ?> 

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

Tags: php图片处理类

分享到: