php图片处理类,生成缩略图,增加水印,获取图片信息

本文章提供这款图片处理类,他可以做的事情是把图片生成缩略图,可以给图片增加水印以及获取图片信息,算是比较实用代码又简洁的函数,实例代码如下:

  1. class image
  2. {
  3. public $info=array();
  4. function __construct()
  5. {
  6. !extension_loaded('gd') && exit("www.111cn.net提示:服务器环境不支持gd库");
  7. return true;
  8. }
  9. function image()
  10. {
  11. $this->__construct();
  12. }
  13. function thumb($image,$thumb_width=300,$thumb_height=225)
  14. {
  15. $info=$this->info($image);
  16. $scale=min(1,min($thumb_width/$info['width'],$thumb_height/$info['height'])); //按比例缩放
  17. $thumb_width=intval($info['width']*$scale);
  18. $thumb_height=intval($info['height']*$scale);
  19. $createfunc='imagecreatefrom'.($info['type']=='jpg'?'jpeg':$info['type']);
  20. $im=$createfunc($image);
  21. $thumb_im=$info['type']!='gif' && function_exists('imagecreatetruecolor')?imagecreatetruecolor($thumb_width,$thumb_height):imagecreate($thumb_width,$thumb_height);
  22. imagecopyresampled($thumb_im,$im,0,0,0,0,$thumb_width,$thumb_height,$info['width'],$info['height']);
  23. if($info['type']=='gif' || $info['type']=='png')
  24. {
  25. $bgcolor=imagecolorallocate($thumb_im,0,255,0);
  26. imagecolortransparent($thumb_im,$bgcolor);
  27. }
  28. $imagefunc='image'.($info['type']=='jpg'?'jpeg':$info['type']);
  29. $thumbname='thumb_'.$info['name'].'.'.$info['type'];
  30. $imagefunc($thumb_im,$info['path'].$thumbname);
  31. imagedestroy($im);
  32. imagedestroy($thumb_im);
  33. return $info['path'].$thumbname;
  34. }
  35. function watermark($image,$pos=9,$watermarkimg='images/watermark.gif',$pct=65,$text='',$w_font=5,$w_color='#ff0000')
  36. {
  37. $imageinfo=$this->info($image);
  38. $source_w=$imageinfo['width'];
  39. $source_h=$imageinfo['height'];
  40. $imagecreatefunc='imagecreatefrom'.($imageinfo['type']=='jpg'?'jpeg':$imageinfo['type']);
  41. $im=$imagecreatefunc($image);
  42. if(!emptyempty($watermarkimg) && file_exists($watermarkimg)) //添加图片水印
  43. {
  44. $iswaterimage=true;
  45. $watermarkinfo=$this->info($watermarkimg);
  46. $width=$watermarkinfo['width'];
  47. $height=$watermarkinfo['height'];
  48. $watermarkcreatefunc='imagecreatefrom'.($watermarkinfo['type']=='jpg'?'jpeg':$watermarkinfo['type']);
  49. $watermark_im=$watermarkcreatefunc($watermarkimg);
  50. }
  51. else //添加文字水印
  52. {
  53. $iswaterimage=false;
  54. if(!emptyempty($w_color) && strlen($w_color)==7)
  55. {
  56. $r=hexdec(substr($w_color,1,2));
  57. $g=hexdec(substr($w_color,3,2));
  58. $b=hexdec(substr($w_color,5,2));
  59. }
  60. $temp = imagettfbbox(ceil($w_font*2.5), 0, 'fonts/alger.ttf', $text);
  61. $width = $temp[2] - $temp[6];
  62. $height = $temp[3] - $temp[7];
  63. unset($temp);
  64. }
  65. switch($pos)
  66. {
  67. case 0:
  68. $wx = mt_rand(0,($source_w - $width));
  69. $wy = mt_rand(0,($source_h - $height));
  70. break;
  71. case 1:
  72. $wx = 5;
  73. $wy = 5;
  74. break;
  75. case 2:
  76. $wx = ($source_w - $width) / 2;
  77. $wy = 5;
  78. break;
  79. case 3:
  80. $wx = $source_w - $width-5;
  81. $wy = 5;
  82. break;
  83. case 4:
  84. $wx = 5;
  85. $wy = ($source_h - $height) / 2;
  86. break;
  87. case 5:
  88. $wx = ($source_w - $width) / 2;
  89. $wy = ($source_h - $height) / 2;
  90. break;
  91. case 6:
  92. $wx = $source_w - $width-5;
  93. $wy = ($source_h - $height) / 2;
  94. break;
  95. case 7:
  96. $wx = 5;
  97. $wy = $source_h - $height-5;
  98. break;
  99. case 8:
  100. $wx = ($source_w - $width) / 2;
  101. $wy = $source_h - $height-5;
  102. break;
  103. default:
  104. $wx = $source_w - $width-5;
  105. $wy = $source_h - $height-5;
  106. break;
  107. }
  108. if($iswaterimage)
  109. {
  110. if($imageinfo['type'] == 'png') {
  111. imagecopy($im, $watermark_im, $wx, $wy, 0, 0, $width, $height);
  112. } else {
  113. imagecopymerge($im, $watermark_im, $wx, $wy, 0, 0, $width, $height, $pct);
  114. }
  115. }
  116. else
  117. {
  118. imagestring($im,$w_font,$wx,$wy,$text,imagecolorallocate($im,$r,$g,$b));
  119. }
  120. $imagefunc='image'.($imageinfo['type']=='jpg'?'jpeg':$imageinfo['type']);
  121. $imagefunc($im,$image);
  122. imagedestroy($im);
  123. return true;
  124. }
  125. function info($image)
  126. {//开源代码phpfensi.com
  127. $info=array();
  128. $info['size']=filesize($image);
  129. $imageinfo=getimagesize($image);
  130. $info['width']=$imageinfo[0];
  131. $info['height']=$imageinfo[1];
  132. $info['width_height']=$imageinfo[3];
  133. $info['mime']=$imageinfo['mime'];
  134. unset($imageinfo);
  135. $imageinfo=pathinfo($image);
  136. $info['path']=$imageinfo['dirname'].'/';
  137. $info['type']=strtolower($imageinfo['extension']); //图片类型,不含'.'
  138. $info['name']=$imageinfo['filename'];
  139. unset($imageinfo,$name);
  140. $this->info=$info;
  141. return $info;
  142. }
  143. }