PHP實現圓角圖片

來源:互聯網
上載者:User

工作中用到,自己寫了一個,分享給有需要的人,前面是類定義,後面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 />?>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.