php 图片 圆角,PHP实现圆角图片 | 学步园

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

优点:

不需要外部图片

支持PNG透明

可自定义圆角半径

不足:

只能指定一种透明色

class RoundedCorner {

private $_r;

private $_g;

private $_b;

private $_image_path;

private $_radius;

function __construct($image_path, $radius, $r = 255, $g = 0, $b = 0) {

$this->_image_path = $image_path;

$this->_radius = $radius;

$this->_r = (int)$r;

$this->_g = (int)$g;

$this->_b = (int)$b;

}

private function _get_lt_rounder_corner() {

$radius = $this->_radius;

$img = imagecreatetruecolor($radius, $radius);

$bgcolor = imagecolorallocate($img, $this->_r, $this->_g, $this->_b);

$fgcolor = imagecolorallocate($img, 0, 0, 0);

imagefill($img, 0, 0, $bgcolor);

imagefilledarc($img, $radius, $radius, $radius*2, $radius*2, 180, 270, $fgcolor, IMG_ARC_PIE);

imagecolortransparent($img, $fgcolor);

return $img;

}

private function _load_source_image() {

$ext = substr($this->_image_path, strrpos($this->_image_path, '.'));

if (empty($ext)) {

return false;

}

switch(strtolower($ext)) {

case '.jpg':

$img = @imagecreatefromjpeg($this->_image_path);

break;

case '.gif':

$img = @imagecreatefromgif($this->_image_path);

break;

case '.png':

$img = @imagecreatefrompng($this->_image_path);

break;

default:

return false;

}

return $img;

}

public function round_it() {

// load the source image

$src_image = $this->_load_source_image();

if ($src_image === false) {

die('Sorry, can/'t load the image');

}

$image_width = imagesx($src_image);

$image_height = imagesy($src_image);

// create a new image, with src_width, src_height, and fill it with transparent color

$image = imagecreatetruecolor($image_width, $image_height);

$trans_color = imagecolorallocate($image, $this->_r, $this->_g, $this->_b);

imagefill($image, 0, 0, $trans_color);

// then overwirte the source image to the new created image

imagecopymerge($image, $src_image, 0, 0, 0, 0, $image_width, $image_height, 100);

// then just copy all the rounded corner images to the 4 corners

$radius = $this->_radius;

// lt

$lt_corner = $this->_get_lt_rounder_corner();

imagecopymerge($image, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);

// lb

$lb_corner = imagerotate($lt_corner, 90, $trans_color);

imagecopymerge($image, $lb_corner, 0, $image_height - $radius, 0, 0, $radius, $radius, 100);

// rb

$rb_corner = imagerotate($lt_corner, 180, $trans_color);

imagecopymerge($image, $rb_corner, $image_width - $radius, $image_height - $radius, 0, 0, $radius, $radius, 100);

// rt

$rt_corner = imagerotate($lt_corner, 270, $trans_color);

imagecopymerge($image, $rt_corner, $image_width - $radius, 0, 0, 0, $radius, $radius, 100);

// set the transparency

imagecolortransparent($image, $trans_color);

// display it

header('Content-Type: image/png');

imagepng($image);

imagedestroy($src_image);

imagedestroy($image);

imagedestroy($lt_corner);

imagedestroy($lb_corner);

imagedestroy($rb_corner);

imagedestroy($rt_corner);

}

}

$rounder = new RoundedCorner('test.png', 20);

$rounder->round_it();

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值