当前位置:编程学习 > php >>

PHP实现图片圆角处理的编程代码

工作中用到,自己写了一个,分享给有需要的人,前面是类定义,后面2行是调用。

优点:不需要外部图片 支持PNG透明 可自定义圆角半径

不足:只能指定一种透明色

  1. <?php  
  2. class RoundedCorner {  
  3.     private $_r;  
  4.     private $_g;  
  5.     private $_b;  
  6.     private $_image_path;  
  7.     private $_radius;  
  8.       
  9.     function __construct($image_path$radius$r = 255, $g = 0, $b = 0) {  
  10.         $this->_image_path = $image_path;  
  11.         $this->_radius = $radius;  
  12.         $this->_r = (int)$r;  
  13.         $this->_g = (int)$g;  
  14.         $this->_b = (int)$b;  
  15.     }  
  16.       
  17.     private function _get_lt_rounder_corner() {  
  18.         $radius = $this->_radius;  
  19.         $img = imagecreatetruecolor($radius$radius);  
  20.         $bgcolor = imagecolorallocate($img$this->_r, $this->_g, $this->_b);  
  21.         $fgcolor = imagecolorallocate($img, 0, 0, 0);  
  22.         imagefill($img, 0, 0, $bgcolor);  
  23.         imagefilledarc($img$radius$radius$radius*2, $radius*2, 180, 270, $fgcolor, IMG_ARC_PIE);  
  24.         imagecolortransparent($img$fgcolor);  
  25.         return $img;  
  26.     }  
  27.       
  28.     private function _load_source_image() {  
  29.         $ext = substr($this->_image_path, strrpos($this->_image_path, '.'));  
  30.         if (emptyempty($ext)) {  
  31.             return false;     
  32.         }  
  33.         switch(strtolower($ext)) {  
  34.             case '.jpg':  
  35.                 $img = @imagecreatefromjpeg($this->_image_path);  
  36.                 break;  
  37.             case '.gif':  
  38.                 $img = @imagecreatefromgif($this->_image_path);  
  39.                 break;  
  40.             case '.png':  
  41.                 $img = @imagecreatefrompng($this->_image_path);  
  42.                 break;  
  43.             default:  
  44.                 return false;  
  45.         }  
  46.         return $img;  
  47.           
  48.     }  
  49.       
  50.     public function round_it() {  
  51.         // load the source image   
  52.         $src_image = $this->_load_source_image();  
  53.         if ($src_image === false) {  
  54.             die('Sorry, can/'t load the image');   
  55.         }  
  56.         $image_width = imagesx($src_image);  
  57.         $image_height = imagesy($src_image);  
  58.           
  59.         // create a new image, with src_width, src_height, and fill it with transparent color   
  60.         $image = imagecreatetruecolor($image_width$image_height);  
  61.         $trans_color = imagecolorallocate($image$this->_r, $this->_g, $this->_b);  
  62.         imagefill($image, 0, 0, $trans_color);  
  63.           
  64.         // then overwirte the source image to the new created image   
  65.         imagecopymerge($image$src_image, 0, 0, 0, 0, $image_width$image_height, 100);  
  66.           
  67.         // then just copy all the rounded corner images to the 4 corners   
  68.         $radius = $this->_radius;  
  69.         // lt   
  70.         $lt_corner = $this->_get_lt_rounder_corner();  
  71.         imagecopymerge($image$lt_corner, 0, 0, 0, 0, $radius$radius, 100);  
  72.         // lb   
  73.         $lb_corner = imagerotate($lt_corner, 90, $trans_color);  
  74.         imagecopymerge($image$lb_corner, 0, $image_height - $radius, 0, 0, $radius$radius, 100);  
  75.         // rb   
  76.         $rb_corner = imagerotate($lt_corner, 180, $trans_color);  
  77.         imagecopymerge($image$rb_corner$image_width - $radius$image_height - $radius, 0, 0, $radius$radius, 100);  
  78.         // rt   
  79.         $rt_corner = imagerotate($lt_corner, 270, $trans_color);  
  80.         imagecopymerge($image$rt_corner$image_width - $radius, 0, 0, 0, $radius$radius, 100);  
  81.           
  82.         // set the transparency   
  83.         imagecolortransparent($image$trans_color);  
  84.         // display it   
  85.         header('Content-Type: image/png');  
  86.         imagepng($image);  
  87.           
  88.         imagedestroy($src_image);  
  89.         imagedestroy($image);  
  90.         imagedestroy($lt_corner);  
  91.         imagedestroy($lb_corner);  
  92.         imagedestroy($rb_corner);  
  93.         imagedestroy($rt_corner);  
  94.     }  
  95. }  
  96. $rounder = new RoundedCorner('test.png', 20);  
  97. $rounder->round_it();  
  98. ?>  

如果你不愿意编程,可以使用:带圆角边框的图片头像在线制作http://www.zhaoxi.org/h/20.htm

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,