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

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

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-19 14:02:44 浏览: 评论:0 

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

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

Tags: php图片处理类 php生成缩略图

分享到: