PHP将图片处理成圆角

栏目: PHP · 发布时间: 8年前

内容简介:PHP将图片处理成圆角

文章摘要:

上一篇文章,我说了关于 PHP 把文字画在图片上的换行方法,这篇说说项目中图片圆角的处理

我们可能在很多项目中,需要对图片进行圆角处理,例如HTML5中,例如Android中;

这里我们说说用PHP对图片进行圆角处理的方法;

圆角处理的方法有很多,这里我就只说说我的项目中是怎么对处理圆角的;

效果图如下:

PHP将图片处理成圆角

乐萌汉字卡片

如图可见,不论是插图,还是文字这背景,我们都做了圆角处理,下面我看看在PHP中,我们是怎么实现的吧。

这里我先说说实现的思路,我们是先画出直角的图,然后再用一个圆角去覆盖在直角上面,这样看起来就是一个圆角了。

一、背景图圆角处理

方法调用:

//整个图,也就是白色背景 $im = imagecreatetruecolor(750, 3000); $bgcolor = imagecolorallocate($im, 255, 255, 255); imagefill($im, 0, 0, $bgcolor);  //生成汉字的背景矩形 $image_width = 694;//圆角淡色背景的宽694px $image_height = 368;//圆角淡色背景的高368px //矩形上面加圆角 $radius = 10;//圆角的像素,值越大越圆 $dst_x = 28;//距离白色大背景左边的距离 $y = 40;//距离白色大背景顶端的距离  //这里调用函数,绘制淡色的圆角背景, imagebackgroundmycard($im, $dst_x, $y, $image_width, $image_height, $radius);

以上是调用函数的说明,下面我们给出函数方法:

方法实现:

    /**  * 画一个带圆角的背景图  * @param $im  底图  * @param $dst_x 画出的图的(0,0)位于底图的x轴位置  * @param $dst_y 画出的图的(0,0)位于底图的y轴位置  * @param $image_w 画的图的宽  * @param $image_h 画的图的高  * @param $radius 圆角的值  */ function imagebackgroundmycard($im, $dst_x, $dst_y, $image_w, $image_h, $radius) {     $resource = imagecreatetruecolor($image_w, $image_h);     $bgcolor = imagecolorallocate($resource, 0xef, 0xef, 0xe1);//该图的背景色      imagefill($resource, 0, 0, $bgcolor);     $lt_corner = get_lt_rounder_corner($radius, 255, 255, 255);//圆角的背景色      // lt(左上角)     imagecopymerge($resource, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);     // lb(左下角)     $lb_corner = imagerotate($lt_corner, 90, 0);     imagecopymerge($resource, $lb_corner, 0, $image_h - $radius, 0, 0, $radius, $radius, 100);     // rb(右上角)     $rb_corner = imagerotate($lt_corner, 180, 0);     imagecopymerge($resource, $rb_corner, $image_w - $radius, $image_h - $radius, 0, 0, $radius, $radius, 100);     // rt(右下角)     $rt_corner = imagerotate($lt_corner, 270, 0);     imagecopymerge($resource, $rt_corner, $image_w - $radius, 0, 0, 0, $radius, $radius, 100);      imagecopy($im, $resource, $dst_x, $dst_y, 0, 0, $image_w, $image_h); }

上面函数方法依赖的函数:

/** 画圆角  * @param $radius 圆角位置  * @param $color_r 色值0-255  * @param $color_g 色值0-255  * @param $color_b 色值0-255  * @return resource 返回圆角  */ function get_lt_rounder_corner($radius, $color_r, $color_g, $color_b) {     // 创建一个正方形的图像     $img = imagecreatetruecolor($radius, $radius);     // 图像的背景     $bgcolor = imagecolorallocate($img, $color_r, $color_g, $color_b);     $fgcolor = imagecolorallocate($img, 0, 0, 0);     imagefill($img, 0, 0, $bgcolor);     // $radius,$radius:以图像的右下角开始画弧     // $radius*2, $radius*2:已宽度、高度画弧     // 180, 270:指定了角度的起始和结束点     // fgcolor:指定颜色     imagefilledarc($img, $radius, $radius, $radius * 2, $radius * 2, 180, 270, $fgcolor, IMG_ARC_PIE);     // 将弧角图片的颜色设置为透明     imagecolortransparent($img, $fgcolor);     return $img; }

最后输出图片:

讲浅色背景圆角处理的已经完成了,如果你想看看最后的效果,只要做下面一部,把图片输出就可以了。

//生成图片 imagepng($im, "test.png"); imagedestroy($im);

二、插图圆角处理:

和上面背景圆角处理完全相同的思路:就是对插图的直角进行覆盖,我就不多说了,下面附上插图圆角处理的的代码。

//这里我们吧准备好的插图画到背景图上,此时还是直角的 $filename="img/test_1.png"//图片资源目录 $img = imagecreatefrompng($filename); //第一个参数是上面已经用过的大的背景图,也就我们的画板, //第二个参数:上面这个目录拿到的capy用的资源文件了 //第三个单数距离大卡片左边的距离 //第三个单数距离大卡片上边的距离 //第三第四是资源图片开始拷贝的位置,这里我是从左上角开始copy的,所以是0和0; //第五第六个参数是图片拷过去的大小 imagecopy($im, $img, 100, $y, 0, 0, 560, 288);  //画圆角 $lt_corner = get_lt_rounder_corner($radius, 0xef, 0xef, 0xe1); //圆角的背景色 myradus($im, 100, $y, $lt_corner, $radius, 288, 560);

上面是调用的方法,这里的get_lt_rounder_corner 是一个自定义的函数,上面背景处理中已经列出该函数的具体实现,这里不再重复,下面给出myradus函数的具体实现:

/**  * @param $im  大的背景图,也是我们的画板  * @param $lt_corner 我们画的圆角  * @param $radius  圆角的程度  * @param $image_h 图片的高  * @param $image_w 图片的宽  */ function myradus($im, $lift, $top, $lt_corner, $radius, $image_h, $image_w) { /// lt(左上角)     imagecopymerge($im, $lt_corner, $lift, $top, 0, 0, $radius, $radius, 100); // lb(左下角)     $lb_corner = imagerotate($lt_corner, 90, 0);     imagecopymerge($im, $lb_corner, $lift, $image_h - $radius + $top, 0, 0, $radius, $radius, 100); // rb(右上角)     $rb_corner = imagerotate($lt_corner, 180, 0);     imagecopymerge($im, $rb_corner, $image_w + $lift - $radius, $image_h + $top - $radius, 0, 0, $radius, $radius, 100); // rt(右下角)     $rt_corner = imagerotate($lt_corner, 270, 0);     imagecopymerge($im, $rt_corner, $image_w - $radius + $lift, $top, 0, 0, $radius, $radius, 100); }

是不是觉得下面这个代码已经写过了呢?是的,上面有一样的代码。

这样我们就实现的背景的圆角处理,也实现了图片的圆角处理。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Four

The Four

Scott Galloway / Portfolio / 2017-10-3 / USD 28.00

NEW YORK TIMES BESTSELLER USA TODAY BESTSELLER Amazon, Apple, Facebook, and Google are the four most influential companies on the planet. Just about everyone thinks they know how they got there.......一起来看看 《The Four》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具