工作中用到,自己寫了一個,分享給有需要的人,前面是類定義,後面2行是調用。
優點:
不需要外部圖片
支援PNG透明
可自訂圓角半徑
不足:
只能指定一種透明色
<?php<br />class RoundedCorner {<br />private $_r;<br />private $_g;<br />private $_b;<br />private $_image_path;<br />private $_radius;</p><p>function __construct($image_path, $radius, $r = 255, $g = 0, $b = 0) {<br />$this->_image_path = $image_path;<br />$this->_radius = $radius;<br />$this->_r = (int)$r;<br />$this->_g = (int)$g;<br />$this->_b = (int)$b;<br />}</p><p>private function _get_lt_rounder_corner() {<br />$radius = $this->_radius;<br />$img = imagecreatetruecolor($radius, $radius);<br />$bgcolor = imagecolorallocate($img, $this->_r, $this->_g, $this->_b);<br />$fgcolor = imagecolorallocate($img, 0, 0, 0);<br />imagefill($img, 0, 0, $bgcolor);<br />imagefilledarc($img, $radius, $radius, $radius*2, $radius*2, 180, 270, $fgcolor, IMG_ARC_PIE);<br />imagecolortransparent($img, $fgcolor);<br />return $img;<br />}</p><p>private function _load_source_image() {<br />$ext = substr($this->_image_path, strrpos($this->_image_path, '.'));<br />if (empty($ext)) {<br />return false;<br />}<br />switch(strtolower($ext)) {<br />case '.jpg':<br />$img = @imagecreatefromjpeg($this->_image_path);<br />break;<br />case '.gif':<br />$img = @imagecreatefromgif($this->_image_path);<br />break;<br />case '.png':<br />$img = @imagecreatefrompng($this->_image_path);<br />break;<br />default:<br />return false;<br />}<br />return $img;</p><p>}</p><p>public function round_it() {<br />// load the source image<br />$src_image = $this->_load_source_image();<br />if ($src_image === false) {<br />die('Sorry, can/'t load the image');<br />}<br />$image_width = imagesx($src_image);<br />$image_height = imagesy($src_image);</p><p>// create a new image, with src_width, src_height, and fill it with transparent color<br />$image = imagecreatetruecolor($image_width, $image_height);<br />$trans_color = imagecolorallocate($image, $this->_r, $this->_g, $this->_b);<br />imagefill($image, 0, 0, $trans_color);</p><p>// then overwirte the source image to the new created image<br />imagecopymerge($image, $src_image, 0, 0, 0, 0, $image_width, $image_height, 100);</p><p>// then just copy all the rounded corner images to the 4 corners<br />$radius = $this->_radius;<br />// lt<br />$lt_corner = $this->_get_lt_rounder_corner();<br />imagecopymerge($image, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);<br />// lb<br />$lb_corner = imagerotate($lt_corner, 90, $trans_color);<br />imagecopymerge($image, $lb_corner, 0, $image_height - $radius, 0, 0, $radius, $radius, 100);<br />// rb<br />$rb_corner = imagerotate($lt_corner, 180, $trans_color);<br />imagecopymerge($image, $rb_corner, $image_width - $radius, $image_height - $radius, 0, 0, $radius, $radius, 100);<br />// rt<br />$rt_corner = imagerotate($lt_corner, 270, $trans_color);<br />imagecopymerge($image, $rt_corner, $image_width - $radius, 0, 0, 0, $radius, $radius, 100);</p><p>// set the transparency<br />imagecolortransparent($image, $trans_color);<br />// display it<br />header('Content-Type: image/png');<br />imagepng($image);</p><p>imagedestroy($src_image);<br />imagedestroy($image);<br />imagedestroy($lt_corner);<br />imagedestroy($lb_corner);<br />imagedestroy($rb_corner);<br />imagedestroy($rt_corner);<br />}<br />}<br />$rounder = new RoundedCorner('test.png', 20);<br />$rounder->round_it();<br />?>